Code: Select all
void MyFrame::OnPlay(wxCommandEvent& event) {
char fileName[_MAX_PATH] = "F:/famitracker/karisuma.wav";
size_t size;
short formatType;
short channel;
size_t sampleRate;
size_t byteRate;
short bytesPerSample;
short bitsPerSample;
size_t dataSize;
unsigned char* wavData = nullptr;
ALCdevice* device;
ALCcontext* context;
ALuint buffer;
ALuint source;
ALsizei frequency;
ALenum format;
ALfloat sourcePos[] = {0.0, 0.0, 0.0};
ALfloat sourceVel[] = {0.0, 0.0, 0.0};
ALfloat listenerPos[] = {0.0, 0.0, 0.0};
ALfloat listenerVel[] = {0.0, 0.0, 0.0};
ALfloat listenerOri[] = {0.0, 0.0, -1.0, 0.0, 1.0, 0.0};
ALfloat volume;
ALint loop;
device = alcOpenDevice(NULL);
if (!device) {
wxMessageBox("ERROR: NO device");
}
context = alcCreateContext(device, NULL);
if (!alcMakeContextCurrent(context)) {
wxMessageBox("ERROR: NO context");
}
if (loadWavFile(fileName, &size, &formatType, &channel,
&sampleRate, &byteRate, &bytesPerSample, &bitsPerSample,
&dataSize, &wavData)) {
alGenBuffers(1, &buffer);
alGenSources(1, &source);
if (bitsPerSample == 16) {
if (channel == 2)
format = AL_FORMAT_STEREO16;
else if (channel == 1)
format = AL_FORMAT_MONO16;
else {
wxMessageBox("ERROR: WEIRD NUMBER\n");
}
}
else if (bitsPerSample == 8) {
if (channel == 2)
format = AL_FORMAT_STEREO8;
else if (channel == 1)
format = AL_FORMAT_MONO8;
else {
wxMessageBox("ERROR: WEIRD NUMBER\n");
}
}
else {
wxMessageBox("ERROR: WEIRD NUMBER\n");
}
frequency = sampleRate;
volume = (float)0.5;
loop = AL_FALSE;
alBufferData(buffer, format, wavData, dataSize, frequency);
alListenerfv(AL_POSITION, listenerPos);
alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri);
alSourcei(source, AL_BUFFER, buffer);
alSourcef(source, AL_PITCH, 1.0);
alSourcef(source, AL_GAIN, volume);
alSourcefv(source, AL_POSITION, sourcePos);
alSourcefv(source, AL_VELOCITY, sourceVel);
alSourcei(source, AL_LOOPING, loop);
alSourcePlay(source);
if (wavData) {
delete wavData;
wavData = nullptr;
}
alDeleteBuffers(1, &buffer);
alDeleteSources(1, &source);
alcMakeContextCurrent(nullptr);
alcDestroyContext(context);
alcCloseDevice(device);
}
}
Code: Select all
int loadWavFile(char fileName[], size_t* size, short* formatType, short* channel,
size_t* sampleRate, size_t* byteRate, short* bytesPerSample, short* bitsPerSample,
size_t* dataSize, unsigned char** wavData) {
FILE* wavFile = nullptr;
char type[4];
int fileNameSize = strlen(fileName);
for (int i = 0; i < fileNameSize; i++) {
if (fileName[i] == '\\')
fileName[i] = '/';
}
wavFile = fopen(fileName, "rb");
if (wavFile) {
fread(type, 1, 4, wavFile);
if (type[0] == 'R' && type[1] == 'I' && type[2] == 'F' && type[3] == 'F') {
fseek(wavFile, 8, SEEK_SET);
fread(type, 1, 4, wavFile);
if (type[0] == 'W' && type[1] == 'A' && type[2] == 'V' && type[3] == 'E') {
fread(type, 1, 4, wavFile);
if (type[0] == 'f' && type[1] == 'm' && type[2] == 't' && type[3] == ' ') {
fread(size, 4, 1, wavFile);
fread(formatType, 2, 1, wavFile);
fread(channel, 2, 1, wavFile);
fread(sampleRate, 4, 1, wavFile);
fread(byteRate, 4, 1, wavFile);
fread(bytesPerSample, 2, 1, wavFile);
fread(bitsPerSample, 2, 1, wavFile);
fread(type, 1, 4, wavFile);
if (type[0] == 'd' && type[1] == 'a' && type[2] == 't' && type[3] == 'a') {
fread(dataSize, 4, 1, wavFile);
*wavData = new unsigned char[*dataSize];
fread(*wavData, 1, *dataSize, wavFile);
}
else {
wxMessageBox("ERROR: CANNOT FIND data HEAD\n");
fclose(wavFile);
return 0;
}
}
else {
wxMessageBox("ERROR: NOT fmt\n");
fclose(wavFile);
return 0;
}
}
else {
wxMessageBox("ERROR: NOT WAVE\n");
fclose(wavFile);
return 0;
}
}
else {
wxMessageBox("ERROR: NOT RIFF\n");
fclose(wavFile);
return 0;
}
}
else {
wxMessageBox("ERROR: CANNOT OPEN FILE\n", "warning", wxICON_EXCLAMATION);
//fclose(wavFile);
return 0;
}
wxMessageBox("File loaded\n");
fclose(wavFile);
return 1;
}
Any suggestions??