forked from itsfuad/Computer-Graphics-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.h
More file actions
197 lines (162 loc) · 6.04 KB
/
Copy pathaudio.h
File metadata and controls
197 lines (162 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#pragma once
#include <AL/al.h>
#include <AL/alc.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <vector>
#include <cstring>
#include <algorithm>
#include "path.h"
class AudioManager {
private:
ALCdevice* device = nullptr;
ALCcontext* context = nullptr;
std::unordered_map<std::string, ALuint> buffers;
std::unordered_map<std::string, ALuint> sources;
bool isInitialized = false;
void cleanupSource(const std::string& name) {
auto it = sources.find(name);
if (it != sources.end()) {
alSourceStop(it->second);
alDeleteSources(1, &it->second);
sources.erase(it);
}
}
public:
bool initialize() {
if (isInitialized) return true;
device = alcOpenDevice(nullptr);
if (!device) {
std::cerr << "Failed to open audio device\n";
return false;
}
context = alcCreateContext(device, nullptr);
if (!context) {
std::cerr << "Failed to create audio context\n";
alcCloseDevice(device);
return false;
}
alcMakeContextCurrent(context);
isInitialized = true;
std::cerr << "AudioManager initialized.\n";
return true;
}
bool loadSound(const std::string& name, const std::string& filePath) {
if (!isInitialized) return false;
std::ifstream file(resolvePath(filePath), std::ios::binary);
if (!file) {
std::cerr << "Failed to open sound file: " << filePath << "\n";
return false;
}
char riffHeader[4], waveHeader[4];
int chunkSize;
file.read(riffHeader, 4);
file.read(reinterpret_cast<char*>(&chunkSize), 4);
file.read(waveHeader, 4);
if (std::strncmp(riffHeader, "RIFF", 4) != 0 || std::strncmp(waveHeader, "WAVE", 4) != 0) {
std::cerr << "Invalid WAV file header: " << filePath << "\n";
return false;
}
short audioFormat = 0, numChannels = 0, bitsPerSample = 0;
int sampleRate = 0, dataSize = 0;
ALenum format = 0;
std::vector<char> audioData;
while (file && !file.eof()) {
char chunkId[4];
int chunkLen = 0;
file.read(chunkId, 4);
file.read(reinterpret_cast<char*>(&chunkLen), 4);
if (std::strncmp(chunkId, "fmt ", 4) == 0) {
std::vector<char> fmt(chunkLen);
file.read(fmt.data(), chunkLen);
audioFormat = *reinterpret_cast<short*>(&fmt[0]);
numChannels = *reinterpret_cast<short*>(&fmt[2]);
sampleRate = *reinterpret_cast<int*>(&fmt[4]);
bitsPerSample = *reinterpret_cast<short*>(&fmt[14]);
if (audioFormat != 1) {
std::cerr << "Unsupported WAV format (not PCM): " << audioFormat << "\n";
return false;
}
if (numChannels == 1 && bitsPerSample == 8) format = AL_FORMAT_MONO8;
else if (numChannels == 1 && bitsPerSample == 16) format = AL_FORMAT_MONO16;
else if (numChannels == 2 && bitsPerSample == 8) format = AL_FORMAT_STEREO8;
else if (numChannels == 2 && bitsPerSample == 16) format = AL_FORMAT_STEREO16;
else {
std::cerr << "Unsupported format (channels=" << numChannels
<< ", bits=" << bitsPerSample << ")\n";
return false;
}
} else if (std::strncmp(chunkId, "data", 4) == 0) {
dataSize = chunkLen;
audioData.resize(dataSize);
file.read(audioData.data(), dataSize);
break;
} else {
file.seekg(chunkLen, std::ios::cur);
}
}
if (audioData.empty()) {
std::cerr << "No audio data found in: " << filePath << "\n";
return false;
}
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, format, audioData.data(), dataSize, sampleRate);
ALenum err = alGetError();
if (err != AL_NO_ERROR) {
std::cerr << "OpenAL error buffering data: " << alGetString(err) << "\n";
alDeleteBuffers(1, &buffer);
return false;
}
buffers[name] = buffer;
std::cerr << "Sound loaded: " << name << "\n";
return true;
}
void playSound(const std::string& name, bool loop = false) {
if (!isInitialized || buffers.find(name) == buffers.end()) return;
cleanupSource(name);
ALuint source;
alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffers[name]);
alSourcei(source, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
alSourcef(source, AL_GAIN, 0.5f);
alSourcePlay(source);
sources[name] = source;
}
void stopSound(const std::string& name) {
if (!isInitialized) return;
cleanupSource(name);
}
void setVolume(const std::string& name, float volume) {
if (!isInitialized || sources.find(name) == sources.end()) return;
alSourcef(sources[name], AL_GAIN, std::max(0.0f, std::min(1.0f, volume)));
}
bool isPlaying(const std::string& name) {
if (!isInitialized || sources.find(name) == sources.end()) return false;
ALint state;
alGetSourcei(sources[name], AL_SOURCE_STATE, &state);
return state == AL_PLAYING;
}
void cleanup() {
if (!isInitialized) return;
for (auto& [_, source] : sources) {
alSourceStop(source);
alDeleteSources(1, &source);
}
sources.clear();
for (auto& [_, buffer] : buffers) {
alDeleteBuffers(1, &buffer);
}
buffers.clear();
alcMakeContextCurrent(nullptr);
alcDestroyContext(context);
alcCloseDevice(device);
isInitialized = false;
std::cout << "AudioManager cleaned up.\n";
}
~AudioManager() {
cleanup();
}
};