audio - C++ OpenAL will not play WAV file -
so have written method meant accept file path .wav file, go through creation of buffers , sources, , play sound. apparently, no sound play when run program. none of error checks trigger, there's nothing wrong file. have scoured google past few hours , have come empty handed, wanted ask here. here code:
file* fp = null; fp = fopen("audiofile.wav", "rb"); char type[4]; dword size, chunksize; short formattype, channels; dword samplerate, avgbytespersec; short bytespersample, bitspersample; dword datasize; fread(type, sizeof(char), 4, fp); if (type[0] != 'r' || type[1] != 'i' || type[2] != 'f' || type[3] != 'f') fatalerror("openal error: no riff"); fread(&size, sizeof(dword), 1, fp); fread(type, sizeof(char), 4, fp); if (type[0] != 'w' || type[1] != 'a' || type[2] != 'v' || type[3] != 'e') fatalerror("openal error: not wave file"); fread(type, sizeof(char), 4, fp); if (type[0] != 'f' || type[1] != 'm' || type[2] != 't' || type[3] != ' ') fatalerror("openal error: not fmt"); fread(&chunksize, sizeof(dword), 1, fp); fread(&formattype, sizeof(short), 1, fp); fread(&channels, sizeof(short), 1, fp); fread(&samplerate, sizeof(dword), 1, fp); fread(&avgbytespersec, sizeof(dword), 1, fp); fread(&bytespersample, sizeof(short), 1, fp); fread(&bitspersample, sizeof(short), 1, fp); fread(type, sizeof(char), 4, fp); if (type[0] != 'd' || type[1] != 'a' || type[2] != 't' || type[3] != 'a') fatalerror("openal error: missing data"); fread(&datasize, sizeof(dword), 1, fp); alcdevice* device; alccontext* context; device = alcopendevice(null); if (!device) fatalerror("openal error: no sound device detected"); context = alccreatecontext(device, null); alcmakecontextcurrent(context); if (!context) fatalerror("openal error: no sound context"); unsigned char* buf = new unsigned char[datasize]; fread(buf, sizeof(byte), datasize, fp); aluint source; aluint buffer; aluint frequency = samplerate; alenum format = 0; algenbuffers(1, &buffer); algensources(1, &source); if (bitspersample == 8) { if (channels == 1) format = al_format_mono8; else if (channels == 2) format = al_format_stereo8; } else if (bitspersample == 16) { if (channels == 1) format = al_format_mono16; else if (channels == 2) format = al_format_stereo16; } albufferdata(buffer, format, buf, datasize, frequency); alfloat sourcepos[] = { 0.0f, 0.0f, 0.0f }; alfloat sourcevel[] = { 0.0f, 0.0f, 0.0f }; alfloat listenerpos[] = { 0.0f, 0.0f, 0.0f }; alfloat listenervel[] = { 0.0f, 0.0f, 0.0f }; alfloat listenerori[] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f }; // listener allistenerfv(al_position, listenerpos); allistenerfv(al_velocity, listenervel); allistenerfv(al_orientation, listenerori); // source alsourcei(source, al_buffer, buffer); alsourcef(source, al_pitch, 1.0f); alsourcef(source, al_gain, 1.0f); alsourcefv(source, al_position, sourcepos); alsourcefv(source, al_velocity, sourcevel); alsourcei(source, al_looping, al_false); alsourceplay(source); fclose(fp); delete[] buf; aldeletesources(1, &source); aldeletebuffers(1, &buffer); alcmakecontextcurrent(null); alcdestroycontext(context); alcclosedevice(device);
Comments
Post a Comment