5 #define Function _Function
7 #define WIN32_LEAN_AND_MEAN
8 #define MessageBox _MessageBox
10 #define String _String
26 public void AudioSetBalance(double percent)
29 streamingSound.balance = percent;
32 static HWAVEOUT hWaveOut;
35 public void OpenMixer()
38 WAVEFORMATEX waveFormat = { 0 };
39 waveFormat.nSamplesPerSec = 44100; //(cur_stream && cur_stream->audio_st && cur_stream->audio_st->codec) ? cur_stream->audio_st->codec->sample_rate : 44100;
40 waveFormat.wBitsPerSample = 16;
41 waveFormat.nChannels = 2;
42 waveFormat.cbSize = 0;
43 waveFormat.wFormatTag = WAVE_FORMAT_PCM;
44 waveFormat.nBlockAlign = (waveFormat.wBitsPerSample >> 3) * waveFormat.nChannels;
45 waveFormat.nAvgBytesPerSec = waveFormat.nBlockAlign * waveFormat.nSamplesPerSec;
47 waveOutOpen(&hWaveOut, WAVE_MAPPER, &waveFormat, 0, 0, CALLBACK_NULL);
48 mixerOpen((HMIXER *)&hmx, (uint)hWaveOut, 0, 0, MIXER_OBJECTF_HWAVEOUT);
51 public void CloseMixer()
53 mixerClose((HMIXER)hmx);
54 waveOutClose(hWaveOut);
57 public bool AudioSetVolume(VolumeControl type, double percent)
60 if(type == application)
64 streamingSound.volume = percent;
72 // waveOutSetVolume((HWAVEOUT)0, (uint)(percent * 0xFFFF) | ((uint)(percent * 0xFFFF) << 16));
74 uMixerLine.cbStruct = sizeof(MIXERLINE);
75 uMixerLine.dwComponentType = (type == pcm) ? MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT : MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
76 if(mixerGetLineInfo(hmx, &uMixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE) == MMSYSERR_NOERROR)
78 MIXERLINECONTROLS uMixerLineControls;
79 MIXERCONTROL mixerControl;
81 uMixerLineControls.cbStruct = sizeof(MIXERLINECONTROLS);
82 uMixerLineControls.dwLineID = uMixerLine.dwLineID;
83 uMixerLineControls.dwControlID = MIXERCONTROL_CONTROLTYPE_VOLUME;
84 uMixerLineControls.cControls = 1;
85 uMixerLineControls.cbmxctrl = sizeof(MIXERCONTROL);
86 uMixerLineControls.pamxctrl = &mixerControl;
87 if(mixerGetLineControls(hmx, &uMixerLineControls, MIXER_GETLINECONTROLSF_ONEBYTYPE) == MMSYSERR_NOERROR)
89 MIXERCONTROLDETAILS uDetails;
92 uDetails.cMultipleItems = 0;
93 uDetails.dwControlID = mixerControl.dwControlID;
94 uDetails.cbStruct = sizeof(MIXERCONTROLDETAILS);
95 uDetails.cbDetails = sizeof(uint);
96 uDetails.paDetails = &value;
97 uDetails.cChannels = 1;
98 value = (uint)(percent * mixerControl.Bounds.lMaximum);
99 mixerSetControlDetails(hmx, &uDetails, MIXER_SETCONTROLDETAILSF_VALUE);
107 public bool AudioGetVolume(VolumeControl type, double * percent)
110 if(type == application)
114 *percent = streamingSound.volume;
120 MIXERLINE uMixerLine;
122 uMixerLine.cbStruct = sizeof(MIXERLINE);
123 uMixerLine.dwComponentType = (type == pcm) ? MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT : MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
124 if(mixerGetLineInfo(hmx, &uMixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE) == MMSYSERR_NOERROR)
126 MIXERLINECONTROLS uMixerLineControls;
127 MIXERCONTROL mixerControl;
129 uMixerLineControls.cbStruct = sizeof(MIXERLINECONTROLS);
130 uMixerLineControls.dwLineID = uMixerLine.dwLineID;
131 uMixerLineControls.dwControlID = MIXERCONTROL_CONTROLTYPE_VOLUME;
132 uMixerLineControls.cControls = 1;
133 uMixerLineControls.cbmxctrl = sizeof(MIXERCONTROL);
135 uMixerLineControls.pamxctrl = &mixerControl;
136 if(mixerGetLineControls(hmx, &uMixerLineControls, MIXER_GETLINECONTROLSF_ONEBYTYPE) == MMSYSERR_NOERROR)
138 MIXERCONTROLDETAILS uDetails;
141 uDetails.cMultipleItems = 0;
142 uDetails.dwControlID = mixerControl.dwControlID;
143 uDetails.cbStruct = sizeof(MIXERCONTROLDETAILS);
144 uDetails.cbDetails = sizeof(uint);
145 uDetails.paDetails = &value;
146 uDetails.cChannels = 1;
147 mixerGetControlDetails(hmx, &uDetails, MIXER_SETCONTROLDETAILSF_VALUE);
148 *percent = (double)value / mixerControl.Bounds.lMaximum;
156 public int OpenAudio(AudioSpec wanted, AudioSpec result)
158 #define NUM_PLAY_NOTIFICATIONS 16
159 uint nBlockAlign, dwNotifySize;
161 delete streamingSound;
163 g_pSoundManager.Initialize(wanted.windowHandle);
164 g_pSoundManager.SetPrimaryBufferFormat(wanted.channels, wanted.freq, wanted.bits);
168 nBlockAlign = wanted.bits / 8 * wanted.channels;
169 // dwNotifySize = 16384 / NUM_PLAY_NOTIFICATIONS; //wanted.freq * 3 * nBlockAlign / NUM_PLAY_NOTIFICATIONS;
170 dwNotifySize = wanted.freq / 2 / NUM_PLAY_NOTIFICATIONS; //wanted.freq * 3 * nBlockAlign / NUM_PLAY_NOTIFICATIONS;
171 dwNotifySize -= dwNotifySize % nBlockAlign;
173 result.size = dwNotifySize * NUM_PLAY_NOTIFICATIONS;
175 g_pSoundManager.InitStreaming(&streamingSound, NUM_PLAY_NOTIFICATIONS, dwNotifySize, dSoundThread.g_hNotificationEvent,
176 wanted.callback, wanted.userdata, wanted.channels, wanted.freq, wanted.bits);
179 streamingSound.volume = wanted.volume;
180 dSoundThread.bDone = false;
182 //dSoundThread.Create();
188 public void PauseAudio(int value)
193 dSoundThread.Create();
197 public void CloseAudio()
201 dSoundThread.bDone = true;
202 SetEvent(dSoundThread.g_hNotificationEvent);
205 delete streamingSound;
208 static bool PlayBuffer( bool bLooped )
214 if(streamingSound.Reset())
217 pDSB = streamingSound.GetBuffer( 0 );
219 SetEvent(dSoundThread.g_hNotificationEvent);
223 static class StreamingSound
225 IDirectSoundBuffer ** apDSBuffer;
226 IDirectSoundBuffer * pDSB;
230 uint dwCreationFlags;
234 uint dwNextWriteOffset;
235 bool bFillNextNotificationWithSilence;
236 void (*callback)(void * data, void * buffer, int len);
240 public property double volume
244 int vol = (int)(20 * log10(value) * 100);
245 vol = Max(-10000, vol);
246 IDirectSoundBuffer_SetVolume(apDSBuffer[0], vol);
255 public property double balance
259 int pan = -2173 + (int)(value * 2173 * 2);
260 IDirectSoundBuffer_SetPan(apDSBuffer[0], pan);
264 property uint dwNumBuffers
268 dwNumBuffers = value;
269 apDSBuffer = new0 IDirectSoundBuffer *[dwNumBuffers];
273 property IDirectSoundBuffer ** apDSBuffer
280 for( i=0; i<dwNumBuffers; i++ )
281 apDSBuffer[i] = value[i];
289 for( i=0; i<dwNumBuffers; i++ )
293 IDirectSoundBuffer_Release( apDSBuffer[i] );
298 delete( apDSBuffer );
301 int RestoreBuffer( IDirectSoundBuffer * pDSB, bool* pbWasRestored )
307 return CO_E_NOTINITIALIZED;
309 *pbWasRestored = false;
311 if( FAILED( hr = IDirectSoundBuffer_GetStatus(pDSB, &dwStatus) ) )
312 return 1; //DXUT_ERR( L"GetStatus", hr );
314 if( dwStatus & DSBSTATUS_BUFFERLOST )
316 // Since the app could have just been activated, then
317 // DirectSound may not be giving us control yet, so
318 // the restoring the buffer may fail.
319 // If it does, sleep until DirectSound gives us control.
322 hr = IDirectSoundBuffer_Restore(pDSB);
323 if( hr == DSERR_BUFFERLOST )
326 while( ( hr = IDirectSoundBuffer_Restore(pDSB) ) == DSERR_BUFFERLOST );
328 if( pbWasRestored != null )
329 *pbWasRestored = true;
335 return S_OK; //FALSE;
339 IDirectSoundBuffer * GetFreeBuffer()
345 for( i=0; i<dwNumBuffers; i++ )
350 IDirectSoundBuffer_GetStatus(apDSBuffer[i], &dwStatus );
351 if ( ( dwStatus & DSBSTATUS_PLAYING ) == 0 )
356 if( i != dwNumBuffers )
357 return apDSBuffer[ i ];
359 return apDSBuffer[ rand() % dwNumBuffers ];
362 IDirectSoundBuffer * GetBuffer( uint dwIndex )
364 if( apDSBuffer == null )
366 if( dwIndex >= dwNumBuffers )
369 return apDSBuffer[dwIndex];
372 int Play( uint dwPriority, uint dwFlags, uint lVolume, uint lFrequency, uint lPan )
377 //IDirectSoundBuffer * pDSB;
379 // For Streaming Sound
380 dwFlags |= DSBPLAY_LOOPING;
382 if( apDSBuffer == null )
383 return CO_E_NOTINITIALIZED;
385 pDSB = GetFreeBuffer();
388 return 1; //DXUT_ERR( L"GetFreeBuffer", E_FAIL );
390 // Restore the buffer if it was lost
391 if( FAILED( hr = RestoreBuffer( pDSB, &bRestored ) ) )
392 return 1; //DXUT_ERR( L"RestoreBuffer", hr );
396 // The buffer was restored, so we need to fill it with new data
397 if( FAILED( hr = FillBufferWithSound( pDSB, false ) ) )
398 return 1; //DXUT_ERR( L"FillBufferWithSound", hr );
401 if( dwCreationFlags & DSBCAPS_CTRLVOLUME )
403 //IDirectSoundBuffer_SetVolume(pDSB, lVolume);
406 if( lFrequency != -1 &&
407 (dwCreationFlags & DSBCAPS_CTRLFREQUENCY) )
409 IDirectSoundBuffer_SetFrequency(pDSB, lFrequency);
412 if( dwCreationFlags & DSBCAPS_CTRLPAN )
414 //IDirectSoundBuffer_SetPan(pDSB, lPan);
416 result = IDirectSoundBuffer_Play(pDSB, 0, dwPriority, dwFlags);
424 if( apDSBuffer == null )
425 return CO_E_NOTINITIALIZED;
427 for( i=0; i<dwNumBuffers; i++ )
428 hr |= IDirectSoundBuffer_Stop(apDSBuffer[i]);
433 bool HandleWaveStreamNotification( )
435 uint dwCurrentPlayPos;
437 uint dwBytesWrittenToBuffer;
438 void * pDSLockedBuffer = null;
439 void * pDSLockedBuffer2 = null;
440 uint dwDSLockedBufferSize;
441 uint dwDSLockedBufferSize2;
442 uint playCursor, writeCursor;
453 // Figure out how much data has been played so far. When we have played
454 // past the end of the file, we will either need to start filling the
455 // buffer with silence or starting reading from the beginning of the file,
456 // depending if the user wants to loop the sound
457 if(IDirectSoundBuffer_GetCurrentPosition(apDSBuffer[0], &dwCurrentPlayPos, null))
461 playCursor = (dwCurrentPlayPos / dwNotifySize);
462 writeCursor = dwNextWriteOffset;
464 //printf("Play Cursor : %d, Write Cursor %d\n", playCursor, writeCursor);
465 if(writeCursor > playCursor)
468 size = (dwDSBufferSize / dwNotifySize) - start;
469 for(c = start; c<start + size; c++)
473 //printf("Writing %d - %d\n", c, c);
474 IDirectSoundBuffer_Lock(apDSBuffer[0], c * dwNotifySize, 1 * dwNotifySize, &pDSLockedBuffer, &dwDSLockedBufferSize, &pDSLockedBuffer2, &dwDSLockedBufferSize2, 0L);
475 //printf("In callback...");
476 callback(data, pDSLockedBuffer, dwDSLockedBufferSize);
478 IDirectSoundBuffer_Unlock(apDSBuffer[0], pDSLockedBuffer, dwDSLockedBufferSize, null, 0 );
479 dwNextWriteOffset = 0;
486 for(c = start; c<start + size; c++)
490 //printf("Writing %d - %d\n", c, c);
491 IDirectSoundBuffer_Lock(apDSBuffer[0], c * dwNotifySize, 1 * dwNotifySize, &pDSLockedBuffer, &dwDSLockedBufferSize, &pDSLockedBuffer2, &dwDSLockedBufferSize2, 0L);
492 //printf("In callback...");
493 callback(data, pDSLockedBuffer, dwDSLockedBufferSize);
495 IDirectSoundBuffer_Unlock(apDSBuffer[0], pDSLockedBuffer, dwDSLockedBufferSize, null, 0 );
496 dwNextWriteOffset = (playCursor) % (dwDSBufferSize / dwNotifySize);
503 size = playCursor - writeCursor;
504 for(c = start; c<start + size; c++)
508 //printf("Writing %d - %d\n", c, c);
509 IDirectSoundBuffer_Lock(apDSBuffer[0], c * dwNotifySize, 1 * dwNotifySize, &pDSLockedBuffer, &dwDSLockedBufferSize, &pDSLockedBuffer2, &dwDSLockedBufferSize2, 0L);
510 //printf("In callback...");
511 callback(data, pDSLockedBuffer, dwDSLockedBufferSize);
513 IDirectSoundBuffer_Unlock(apDSBuffer[0], pDSLockedBuffer, dwDSLockedBufferSize, null, 0 );
514 dwNextWriteOffset = (playCursor) % (dwDSBufferSize / dwNotifySize);
517 //printf("Next Offset: %d\n", dwNextWriteOffset);
533 dwNextWriteOffset = 0;
534 bFillNextNotificationWithSilence = false;
536 // Restore the buffer if it was lost
537 if(RestoreBuffer( apDSBuffer[0], &bRestored ))
542 // The buffer was restored, so we need to fill it with new data
543 if(FillBufferWithSound( apDSBuffer[0], false))
547 result = IDirectSoundBuffer_SetCurrentPosition(apDSBuffer[0], 0L );
551 int FillBufferWithSound( IDirectSoundBuffer * pDSB, bool bRepeatWavIfBufferLarger )
554 void* pDSLockedBuffer = null; // Pointer to locked buffer memory
555 uint dwDSLockedBufferSize = 0; // Size of the locked DirectSound buffer
556 uint dwWavDataRead = 0; // Amount of data read from the wav file
559 return CO_E_NOTINITIALIZED;
561 // Make sure we have focus, and we didn't just switch in from
562 // an app which had a DirectSound device
563 if(RestoreBuffer( pDSB, null ))
566 // Lock the buffer down
567 if(IDirectSoundBuffer_Lock(pDSB, 0, dwDSBufferSize, &pDSLockedBuffer, &dwDSLockedBufferSize, null, null, 0L ))
570 callback(data, pDSLockedBuffer, dwDSLockedBufferSize);
572 dwWavDataRead = dwDSLockedBufferSize;
574 printf("Filling 16 spots\n");
575 IDirectSoundBuffer_Unlock(pDSB, pDSLockedBuffer, dwDSLockedBufferSize, null, 0 );
577 //dwNextWriteOffset = dwNotifySize*2;
582 static class CSoundManager
589 IDirectSound_Release( pDS );
592 int Initialize( void * hWnd )
598 IDirectSound_Release( pDS );
602 // Create IDirectSound using the primary sound device
603 if( FAILED( hr = DirectSoundCreate8( null, &pDS, null ) ) )
604 return 1; //DXUT_ERR( L"DirectSoundCreate8", hr );
606 // Set DirectSound coop level
607 if( FAILED( hr = IDirectSound_SetCooperativeLevel(pDS, hWnd, DSSCL_PRIORITY ) ) )
608 return 1; //DXUT_ERR( L"SetCooperativeLevel", hr );
614 int SetPrimaryBufferFormat( uint dwPrimaryChannels, uint dwPrimaryFreq, uint dwPrimaryBitRate )
617 IDirectSoundBuffer * pDSBPrimary = null;
622 return CO_E_NOTINITIALIZED;
624 // Get the primary buffer
625 ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
626 dsbd.dwSize = sizeof(DSBUFFERDESC);
627 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME;
628 dsbd.dwBufferBytes = 0;
629 dsbd.lpwfxFormat = null;
631 if( FAILED( hr = IDirectSound_CreateSoundBuffer(pDS, &dsbd, &pDSBPrimary, null ) ) )
632 return 1; // DXUT_ERR( L"CreateSoundBuffer", hr );
634 ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
635 wfx.wFormatTag = (WORD) WAVE_FORMAT_PCM;
636 wfx.nChannels = (WORD) dwPrimaryChannels;
637 wfx.nSamplesPerSec = (uint) dwPrimaryFreq;
638 wfx.wBitsPerSample = (WORD) dwPrimaryBitRate;
639 wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels);
640 wfx.nAvgBytesPerSec = (uint) (wfx.nSamplesPerSec * wfx.nBlockAlign);
642 if( FAILED( hr = IDirectSoundBuffer_SetFormat(pDSBPrimary, &wfx) ) )
643 return 1; //DXUT_ERR( L"SetFormat", hr );
647 IDirectSoundBuffer_Release( pDSBPrimary );
654 bool InitStreaming(StreamingSound * ppStreamingSound, uint dwNotifyCount, uint dwNotifySize, void * hNotifyEvent,
655 void (*callback)(void * data, void * buffer, int len), void * data, int nChannels, int freq,int bits)
658 IDirectSoundBuffer * pDSBuffer = null;
659 uint dwDSBufferSize = 0;
660 DSBPOSITIONNOTIFY * aPosNotify = null;
661 IDirectSoundNotify * pDSNotify = null;
667 if(!ppStreamingSound || !hNotifyEvent)
670 dwDSBufferSize = dwNotifySize * dwNotifyCount;
672 ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
673 dsbd.dwSize= sizeof(DSBUFFERDESC);
674 dsbd.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME;
675 dsbd.dwBufferBytes = dwDSBufferSize;
676 dsbd.guid3DAlgorithm = DS3DALG_DEFAULT;
677 dsbd.lpwfxFormat = new0 WAVEFORMATEX[1];
678 dsbd.lpwfxFormat->wFormatTag = WAVE_FORMAT_PCM;
679 dsbd.lpwfxFormat->nChannels = (uint16)nChannels;
680 dsbd.lpwfxFormat->nSamplesPerSec = freq;
681 dsbd.lpwfxFormat->wBitsPerSample = (uint16)bits;
682 dsbd.lpwfxFormat->nBlockAlign = dsbd.lpwfxFormat->nChannels * dsbd.lpwfxFormat->wBitsPerSample / 8;
683 dsbd.lpwfxFormat->nAvgBytesPerSec = dsbd.lpwfxFormat->nSamplesPerSec * dsbd.lpwfxFormat->nBlockAlign;
685 if(IDirectSound_CreateSoundBuffer(pDS, &dsbd, &pDSBuffer, null))
687 delete dsbd.lpwfxFormat;
690 delete dsbd.lpwfxFormat;
692 if(IDirectSoundBuffer_QueryInterface(pDSBuffer, &IID_IDirectSoundNotify, (void**)&pDSNotify))
698 aPosNotify = new DSBPOSITIONNOTIFY[ dwNotifyCount ];
699 if( aPosNotify == null )
702 for( i = 0; i < dwNotifyCount; i++ )
704 aPosNotify[i].dwOffset = (dwNotifySize * i) + dwNotifySize - 1;
705 aPosNotify[i].hEventNotify = hNotifyEvent;
708 if(IDirectSoundNotify_SetNotificationPositions(pDSNotify, dwNotifyCount, aPosNotify ))
711 IDirectSoundNotify_Release(pDSNotify);
712 delete( aPosNotify );
717 IDirectSoundNotify_Release(pDSNotify);
720 *ppStreamingSound = StreamingSound
722 dwCreationFlags = DSBCAPS_CTRLPAN|DSBCAPS_CTRLVOLUME, dwNumBuffers = 1, dwNotifySize = dwNotifySize, dwDSBufferSize = dwDSBufferSize, apDSBuffer = &pDSBuffer,
724 callback = callback, data = data
726 // ppStreamingSound->FillBufferWithSound( pDSBuffer, false );
731 static DSoundThread dSoundThread { };
733 static CSoundManager g_pSoundManager { };
734 static StreamingSound streamingSound;
736 static class DSoundThread : Thread
738 void * g_hNotificationEvent;
742 g_hNotificationEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
746 CloseHandle( g_hNotificationEvent );
751 SetPriority(timeCritical);
754 WaitForSingleObject(g_hNotificationEvent, INFINITE);
759 streamingSound.FillBufferWithSound( streamingSound.GetBuffer( 0 ), false );
760 streamingSound.Play( 0, 0, 0, -1, 0);
763 else if(!streamingSound.HandleWaveStreamNotification())