int WaveLoadFile( TCHAR*pszFileName, // (IN) UINT *cbSize, // (OUT) WAVEFORMATEX **ppwfxInfo, // (OUT) BYTE **ppbData // (OUT) ) { HMMIO hmmioIn; MMCKINFO ckInRiff; MMCKINFO ckIn; int nError; UINT cbActualRead; *ppbData = NULL; *ppwfxInfo = NULL; *cbSize = 0; if ((nError = WaveOpenFile(pszFileName, &hmmioIn, ppwfxInfo, &ckInRiff)) != 0) { goto ERROR_LOADING; } if ((nError = WaveStartDataRead(&hmmioIn, &ckIn, &ckInRiff)) != 0) { goto ERROR_LOADING; } // Ok, size of wave data is in ckIn, allocate that buffer. if ((*ppbData = (BYTE *)GlobalAlloc(GMEM_FIXED, ckIn.cksize)) == NULL) { nError = ER_MEM; goto ERROR_LOADING; } if ((nError = WaveReadFile(hmmioIn, ckIn.cksize, *ppbData, &ckIn, &cbActualRead)) != 0) { goto ERROR_LOADING; } *cbSize = cbActualRead; goto DONE_LOADING; ERROR_LOADING: if (*ppbData != NULL) { GlobalFree(*ppbData); *ppbData = NULL; } if (*ppwfxInfo != NULL) { GlobalFree(*ppwfxInfo); *ppwfxInfo = NULL; } DONE_LOADING: // Close the wave file. if (hmmioIn != NULL) { mmioClose(hmmioIn, 0); hmmioIn = NULL; } return(nError); }
//================================================================================================================ bool Sound::LoadSound(char* filename) { char* path = new char[strlen(m_gd->m_music_path.c_str()) + 1]; strcpy(path, m_gd->m_music_path.c_str()); strcat(path, "\\"); strcat(path, filename); WAVEFORMATEX *pwfx; HMMIO hmmio; MMCKINFO mmckinfo; MMCKINFO mmckinfoParent; TCHAR szName[512]; int path_len = strlen(m_gd->m_music_path.c_str()) + 1; int filename_len = strlen(filename) + 1; int size = path_len + filename_len; ZShadeSandboxGlobal::Convert::ConvertCharPointerToTChar(path, szName, size); //Open the file if (WaveOpenFile(szName, &hmmio, &pwfx, &mmckinfoParent) != 0) { ZShadeMessageCenter::MsgBoxError(NULL, "LoadSound: failed to open wave file"); return false; } //Read the file if (WaveStartDataRead(&hmmio, &mmckinfo, &mmckinfoParent) != 0) { ZShadeMessageCenter::MsgBoxError(NULL, "LoadSound: failed to read wave file"); return false; } DSBUFFERDESC dsbdesc; //Get buffer Info memset( &dsbdesc, 0, sizeof(DSBUFFERDESC) ); dsbdesc.dwSize = sizeof(DSBUFFERDESC); dsbdesc.dwFlags = DSBCAPS_STATIC; dsbdesc.dwBufferBytes = mmckinfo.cksize; dsbdesc.lpwfxFormat = pwfx; //Create the buffer if (FAILED(m_DirectSound->CreateSoundBuffer(&dsbdesc, &m_primaryBuffer, NULL))) { ZShadeMessageCenter::MsgBoxError(NULL, "LoadSound: failed to create wave sound buffer"); WaveCloseReadFile(&hmmio, &pwfx); return false; } LPVOID lpvAudio1; DWORD dwBytes1; //Lock the buffer if (FAILED(m_primaryBuffer->Lock(0, 0, &lpvAudio1, &dwBytes1, NULL, NULL, DSBLOCK_ENTIREBUFFER))) { ZShadeMessageCenter::MsgBoxError(NULL, "LoadSound: failed to lock wave sound buffer"); WaveCloseReadFile(&hmmio, &pwfx); return false; } UINT cbBytesRead; if (WaveReadFile(hmmio, dwBytes1, (BYTE*)lpvAudio1, &mmckinfo, &cbBytesRead)) { ZShadeMessageCenter::MsgBoxError(NULL, "LoadSound: failed to read wave sound buffer"); WaveCloseReadFile(&hmmio, &pwfx); return false; } //Unlock the buffer m_primaryBuffer->Unlock(lpvAudio1, dwBytes1, NULL, 0); //Close the file WaveCloseReadFile(&hmmio, &pwfx); m_soundLoaded = true; return true; }
//----------------------------------------------------------------------------- // Name: Read() // Desc: Reads a wave file into a pointer and returns how much read // using m_ckIn to determine where to start reading from //----------------------------------------------------------------------------- HRESULT CWaveSoundRead::Read( UINT nSizeToRead, BYTE* pbData, UINT* pnSizeRead ) { return WaveReadFile( m_hmmioIn, nSizeToRead, pbData, &m_ckIn, pnSizeRead ); }
int main(int argc, char **argv) { ALboolean enumeration; const ALCchar *devices; const ALCchar *defaultDeviceName = argv[1]; int ret; #ifdef LIBAUDIO WaveInfo *wave; #endif char *bufferData; ALCdevice *device; ALvoid *data; ALCcontext *context; ALsizei size, freq; ALenum format; ALuint buffer, source; ALfloat listenerOri[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f }; ALboolean loop = AL_FALSE; ALCenum error; ALint source_state; enumeration = alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT"); if (enumeration == AL_FALSE) fprintf(stderr, "enumeration extension not available\n"); list_audio_devices(alcGetString(NULL, ALC_DEVICE_SPECIFIER)); if (!defaultDeviceName) defaultDeviceName = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); device = alcOpenDevice(defaultDeviceName); if (!device) { fprintf(stderr, "unable to open default device\n"); return -1; } fprintf(stdout, "Device: %s\n", alcGetString(device, ALC_DEVICE_SPECIFIER)); alGetError(); context = alcCreateContext(device, NULL); if (!alcMakeContextCurrent(context)) { fprintf(stderr, "failed to make default context\n"); return -1; } TEST_ERROR("make default context"); /* set orientation */ alListener3f(AL_POSITION, 0, 0, 1.0f); TEST_ERROR("listener position"); alListener3f(AL_VELOCITY, 0, 0, 0); TEST_ERROR("listener velocity"); alListenerfv(AL_ORIENTATION, listenerOri); TEST_ERROR("listener orientation"); alGenSources((ALuint)1, &source); TEST_ERROR("source generation"); alSourcef(source, AL_PITCH, 1); TEST_ERROR("source pitch"); alSourcef(source, AL_GAIN, 1); TEST_ERROR("source gain"); alSource3f(source, AL_POSITION, 0, 0, 0); TEST_ERROR("source position"); alSource3f(source, AL_VELOCITY, 0, 0, 0); TEST_ERROR("source velocity"); alSourcei(source, AL_LOOPING, AL_FALSE); TEST_ERROR("source looping"); alGenBuffers(1, &buffer); TEST_ERROR("buffer generation"); #ifdef LIBAUDIO /* load data */ wave = WaveOpenFileForReading("test.wav"); if (!wave) { fprintf(stderr, "failed to read wave file\n"); return -1; } ret = WaveSeekFile(0, wave); if (ret) { fprintf(stderr, "failed to seek wave file\n"); return -1; } bufferData = malloc(wave->dataSize); if (!bufferData) { perror("malloc"); return -1; } ret = WaveReadFile(bufferData, wave->dataSize, wave); if (ret != wave->dataSize) { fprintf(stderr, "short read: %d, want: %d\n", ret, wave->dataSize); return -1; } alBufferData(buffer, to_al_format(wave->channels, wave->bitsPerSample), bufferData, wave->dataSize, wave->sampleRate); TEST_ERROR("failed to load buffer data"); #else alutLoadWAVFile("test.wav", &format, &data, &size, &freq, &loop); TEST_ERROR("loading wav file"); alBufferData(buffer, format, data, size, freq); TEST_ERROR("buffer copy"); #endif alSourcei(source, AL_BUFFER, buffer); TEST_ERROR("buffer binding"); alSourcePlay(source); TEST_ERROR("source playing"); alGetSourcei(source, AL_SOURCE_STATE, &source_state); TEST_ERROR("source state get"); while (source_state == AL_PLAYING) { alGetSourcei(source, AL_SOURCE_STATE, &source_state); TEST_ERROR("source state get"); } /* exit context */ alDeleteSources(1, &source); alDeleteBuffers(1, &buffer); device = alcGetContextsDevice(context); alcMakeContextCurrent(NULL); alcDestroyContext(context); alcCloseDevice(device); return 0; }