-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSound.h
187 lines (148 loc) · 4.68 KB
/
Sound.h
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
#ifndef SOUND_H
#define SOUND_H
#include <iostream>
#include <fstream>
#include <string.h>
#include <AL/al.h>
#include <AL/alc.h>
class Sound {
public:
Sound(char* filename) {
alGenSources(1, &(this->source));
alSourcef(this->source, AL_PITCH, 1);
alSourcef(this->source, AL_GAIN, 1);
alSource3f(this->source, AL_POSITION, 0, 0, 0);
alSource3f(this->source, AL_VELOCITY, 0, 0, 0);
alSourcei(this->source, AL_LOOPING, AL_FALSE);
std::fstream file(filename, std::fstream::in | std::fstream::binary);
char xbuffer[5];
xbuffer[4] = '\0';
file.read(xbuffer, 4);
if (strcmp(xbuffer, "RIFF") != 0) {
alDeleteSources(1, &(this->source));
std::cout << "Not a wav file!" << std::endl;
return;
}
file.read(xbuffer, 4);
file.read(xbuffer, 4);
if (strcmp(xbuffer, "WAVE") != 0) {
alDeleteSources(1, &(this->source));
std::cout << "Not a wav file!" << std::endl;
return;
}
file.read(xbuffer, 4);
if (strcmp(xbuffer, "fmt ") != 0) {
alDeleteSources(1, &(this->source));
std::cout << "Invalid WAV file!" << std::endl;
return;
}
file.read(xbuffer, 4);
file.read(xbuffer, 2);
this->audioFormat = BytesToShort(xbuffer);
if (this->audioFormat != 1) {
alDeleteSources(1, &(this->source));
std::cout << "File not in PCM! Cannot read it!" << std::endl;
return;
}
file.read(xbuffer, 2);
this->channels = BytesToShort(xbuffer);
file.read(xbuffer, 4);
this->sampleRate = BytesToInt(xbuffer);
file.read(xbuffer, 4);
this->byteRate = BytesToInt(xbuffer);
file.read(xbuffer, 2);
file.read(xbuffer, 2);
this->bitsPerSample = BytesToShort(xbuffer);
if (this->bitsPerSample != 8 && this->bitsPerSample != 16) {
alDeleteSources(1, &(this->source));
std::cout << "File is not written as 8-bit or 16-bit!" << std::endl;
return;
}
file.read(xbuffer, 4);
if (strcmp(xbuffer, "data") != 0) {
alDeleteSources(1, &(this->source));
std::cout << "Invalid WAV file!" << std::endl;
return;
}
file.read(xbuffer, 4);
this->audioSize = BytesToInt(xbuffer);
this->audioData = (unsigned char*)malloc(audioSize);
file.read(reinterpret_cast<char*>(audioData), audioSize);
file.close();
if (channels == 1) {
if (this->bitsPerSample == 8) this->format = AL_FORMAT_MONO8;
else this->format = AL_FORMAT_MONO16;
}
else {
if (this->bitsPerSample == 8) this->format = AL_FORMAT_STEREO8;
else this->format = AL_FORMAT_STEREO16;
}
alGenBuffers(1, &(this->buffer));
alBufferData(this->buffer, this->format, this->audioData, this->audioSize, this->sampleRate);
alSourcei(this->source, AL_BUFFER, this->buffer);
}
void dealloc() {
free(this->audioData);
alDeleteSources(1, &(this->source));
alDeleteBuffers(1, &(this->buffer));
}
void play() {
alSourcePlay(this->source);
std::cout << "playing" << std::endl;
}
void pause() {
alSourcePause(this->source);
}
void setPitch(float val) {
alSourcef(source, AL_PITCH, val);
}
void setGain(float val) {
alSourcef(source, AL_GAIN, val);
}
void setPosition(float* position) {
this->setPosition(position[0], position[1], position[2]);
}
void setPosition(float x, float y, float z) {
alSource3f(source, AL_POSITION, x, y, z);
}
void setVelocity(float* velocity) {
this->setPosition(velocity[0], velocity[1], velocity[2]);
}
void setVelocity(float x, float y, float z) {
alSource3f(source, AL_VELOCITY, x, y, z);
}
void isLooping(bool state) {
if (state) alSourcei(this->source, AL_LOOPING, AL_TRUE);
else alSourcei(this->source, AL_LOOPING, AL_FALSE);
}
unsigned short getAudioFormat(){ return this->audioFormat; }
unsigned short getChannels() { return this->channels; }
unsigned int getSampleRate() { return this->sampleRate; }
unsigned int getByteRate() { return this->byteRate; }
unsigned int getBitsPerSample() { return this->bitsPerSample; }
unsigned int getAudioSize() { return this->audioSize; }
unsigned char* getAudioData() { return this->audioData; }
private:
ALuint source, buffer;
ALenum format;
unsigned short audioFormat, channels, blockAlign;
unsigned int sampleRate, byteRate, bitsPerSample, audioSize;
unsigned char* audioData;
unsigned short BytesToShort(char* buffer) {
unsigned short out = 0, byte0, byte1;
byte0 = (unsigned short)(buffer[0]) & 0x00ff;
byte1 = (unsigned short)(buffer[1] << 8) & 0xff00;
out += byte0 | byte1;
return out;
}
unsigned int BytesToInt(char* buffer) {
unsigned int out = 0, byte0, byte1, byte2, byte3;
byte0 = (unsigned int)(buffer[0]) & 0x000000ff;
byte1 = (unsigned int)(buffer[1] << 8) & 0x0000ff00;
byte2 = (unsigned int)(buffer[2] << 16) & 0x00ff0000;
byte3 = (unsigned int)(buffer[3] << 24) & 0xff000000;
out += byte0 | byte1 | byte2 | byte3;
return out;
}
};
#endif