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()
37 WAVEFORMATEX waveFormat = { 0 };
38 waveFormat.nSamplesPerSec = 44100; //(cur_stream && cur_stream->audio_st && cur_stream->audio_st->codec) ? cur_stream->audio_st->codec->sample_rate : 44100;
39 waveFormat.wBitsPerSample = 16;
40 waveFormat.nChannels = 2;
41 waveFormat.cbSize = 0;
42 waveFormat.wFormatTag = WAVE_FORMAT_PCM;
43 waveFormat.nBlockAlign = (uint16)((waveFormat.wBitsPerSample >> 3) * waveFormat.nChannels);
44 waveFormat.nAvgBytesPerSec = waveFormat.nBlockAlign * waveFormat.nSamplesPerSec;
46 waveOutOpen(&hWaveOut, WAVE_MAPPER, &waveFormat, 0, 0, CALLBACK_NULL);
47 mixerOpen((HMIXER *)&hmx, (uint)(uintptr)hWaveOut, 0, 0, MIXER_OBJECTF_HWAVEOUT);
50 public void CloseMixer()
52 mixerClose((HMIXER)hmx);
53 waveOutClose(hWaveOut);
56 public bool AudioSetVolume(VolumeControl type, double percent)
59 if(type == application)
63 streamingSound.volume = percent;
71 // waveOutSetVolume((HWAVEOUT)0, (uint)(percent * 0xFFFF) | ((uint)(percent * 0xFFFF) << 16));
73 uMixerLine.cbStruct = sizeof(MIXERLINE);
74 uMixerLine.dwComponentType = (type == pcm) ? MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT : MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
75 if(mixerGetLineInfo(hmx, &uMixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE) == MMSYSERR_NOERROR)
77 MIXERLINECONTROLS uMixerLineControls;
78 MIXERCONTROL mixerControl;
80 uMixerLineControls.cbStruct = sizeof(MIXERLINECONTROLS);
81 uMixerLineControls.dwLineID = uMixerLine.dwLineID;
82 uMixerLineControls.dwControlID = MIXERCONTROL_CONTROLTYPE_VOLUME;
83 uMixerLineControls.cControls = 1;
84 uMixerLineControls.cbmxctrl = sizeof(MIXERCONTROL);
85 uMixerLineControls.pamxctrl = &mixerControl;
86 if(mixerGetLineControls(hmx, &uMixerLineControls, MIXER_GETLINECONTROLSF_ONEBYTYPE) == MMSYSERR_NOERROR)
88 MIXERCONTROLDETAILS uDetails;
91 uDetails.cMultipleItems = 0;
92 uDetails.dwControlID = mixerControl.dwControlID;
93 uDetails.cbStruct = sizeof(MIXERCONTROLDETAILS);
94 uDetails.cbDetails = sizeof(uint);
95 uDetails.paDetails = &value;
96 uDetails.cChannels = 1;
97 value = (uint)(percent * mixerControl.Bounds.lMaximum);
98 mixerSetControlDetails(hmx, &uDetails, MIXER_SETCONTROLDETAILSF_VALUE);
106 public bool AudioGetVolume(VolumeControl type, double * percent)
109 if(type == application)
113 *percent = streamingSound.volume;
119 MIXERLINE uMixerLine;
121 uMixerLine.cbStruct = sizeof(MIXERLINE);
122 uMixerLine.dwComponentType = (type == pcm) ? MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT : MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
123 if(mixerGetLineInfo(hmx, &uMixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE) == MMSYSERR_NOERROR)
125 MIXERLINECONTROLS uMixerLineControls;
126 MIXERCONTROL mixerControl;
128 uMixerLineControls.cbStruct = sizeof(MIXERLINECONTROLS);
129 uMixerLineControls.dwLineID = uMixerLine.dwLineID;
130 uMixerLineControls.dwControlID = MIXERCONTROL_CONTROLTYPE_VOLUME;
131 uMixerLineControls.cControls = 1;
132 uMixerLineControls.cbmxctrl = sizeof(MIXERCONTROL);
134 uMixerLineControls.pamxctrl = &mixerControl;
135 if(mixerGetLineControls(hmx, &uMixerLineControls, MIXER_GETLINECONTROLSF_ONEBYTYPE) == MMSYSERR_NOERROR)
137 MIXERCONTROLDETAILS uDetails;
140 uDetails.cMultipleItems = 0;
141 uDetails.dwControlID = mixerControl.dwControlID;
142 uDetails.cbStruct = sizeof(MIXERCONTROLDETAILS);
143 uDetails.cbDetails = sizeof(uint);
144 uDetails.paDetails = &value;
145 uDetails.cChannels = 1;
146 mixerGetControlDetails(hmx, &uDetails, MIXER_SETCONTROLDETAILSF_VALUE);
147 *percent = (double)value / mixerControl.Bounds.lMaximum;
155 public int OpenAudio(AudioSpec wanted, AudioSpec result)
157 #define NUM_PLAY_NOTIFICATIONS 16
158 uint nBlockAlign, dwNotifySize;
160 delete streamingSound;
162 g_pSoundManager.Initialize(wanted.windowHandle);
163 g_pSoundManager.SetPrimaryBufferFormat(wanted.channels, wanted.freq, wanted.bits);
167 nBlockAlign = wanted.bits / 8 * wanted.channels;
168 // dwNotifySize = 16384 / NUM_PLAY_NOTIFICATIONS; //wanted.freq * 3 * nBlockAlign / NUM_PLAY_NOTIFICATIONS;
169 dwNotifySize = wanted.freq / 2 / NUM_PLAY_NOTIFICATIONS; //wanted.freq * 3 * nBlockAlign / NUM_PLAY_NOTIFICATIONS;
170 dwNotifySize -= dwNotifySize % nBlockAlign;
172 result.size = dwNotifySize * NUM_PLAY_NOTIFICATIONS;
174 g_pSoundManager.InitStreaming(&streamingSound, NUM_PLAY_NOTIFICATIONS, dwNotifySize, dSoundThread.g_hNotificationEvent,
175 (void *)wanted.callback, wanted.userdata, wanted.channels, wanted.freq, wanted.bits);
178 streamingSound.volume = wanted.volume;
179 dSoundThread.bDone = false;
181 //dSoundThread.Create();
187 public void PauseAudio(bool value)
191 dSoundThread.bDone = true;
192 SetEvent(dSoundThread.g_hNotificationEvent);
198 dSoundThread.Create();
202 public void CloseAudio()
206 dSoundThread.bDone = true;
207 SetEvent(dSoundThread.g_hNotificationEvent);
210 delete streamingSound;
213 static bool PlayBuffer( bool bLooped )
219 if(streamingSound.Reset())
222 //pDSB = streamingSound.GetBuffer( 0 );
224 SetEvent(dSoundThread.g_hNotificationEvent);
228 static class StreamingSound
230 IDirectSoundBuffer ** apDSBuffer;
231 IDirectSoundBuffer * pDSB;
235 uint dwCreationFlags;
239 uint dwNextWriteOffset;
240 bool bFillNextNotificationWithSilence;
241 void (*callback)(void * data, void * buffer, int len);
245 public property double volume
249 int vol = (int)(20 * log10(value) * 100);
250 vol = Max(-10000, vol);
251 IDirectSoundBuffer_SetVolume(apDSBuffer[0], vol);
260 public property double balance
264 int pan = -2173 + (int)(value * 2173 * 2);
265 IDirectSoundBuffer_SetPan(apDSBuffer[0], pan);
269 property uint dwNumBuffers
273 dwNumBuffers = value;
274 apDSBuffer = new0 IDirectSoundBuffer *[dwNumBuffers];
278 property IDirectSoundBuffer ** apDSBuffer
285 for( i=0; i<dwNumBuffers; i++ )
286 apDSBuffer[i] = value[i];
294 for( i=0; i<dwNumBuffers; i++ )
298 IDirectSoundBuffer_Release( apDSBuffer[i] );
303 delete( apDSBuffer );
306 int RestoreBuffer( IDirectSoundBuffer * pDSB, bool* pbWasRestored )
312 return CO_E_NOTINITIALIZED;
314 *pbWasRestored = false;
316 if( FAILED( hr = IDirectSoundBuffer_GetStatus(pDSB, &dwStatus) ) )
317 return 1; //DXUT_ERR( L"GetStatus", hr );
319 if( dwStatus & DSBSTATUS_BUFFERLOST )
321 // Since the app could have just been activated, then
322 // DirectSound may not be giving us control yet, so
323 // the restoring the buffer may fail.
324 // If it does, sleep until DirectSound gives us control.
327 hr = IDirectSoundBuffer_Restore(pDSB);
328 if( hr == DSERR_BUFFERLOST )
331 while( ( hr = IDirectSoundBuffer_Restore(pDSB) ) == DSERR_BUFFERLOST );
333 if( pbWasRestored != null )
334 *pbWasRestored = true;
340 return S_OK; //FALSE;
344 IDirectSoundBuffer * GetFreeBuffer()
350 for( i=0; i<dwNumBuffers; i++ )
355 IDirectSoundBuffer_GetStatus(apDSBuffer[i], &dwStatus );
356 if ( ( dwStatus & DSBSTATUS_PLAYING ) == 0 )
361 if( i != dwNumBuffers )
362 return apDSBuffer[ i ];
364 return apDSBuffer[ rand() % dwNumBuffers ];
367 IDirectSoundBuffer * GetBuffer( uint dwIndex )
369 if( apDSBuffer == null )
371 if( dwIndex >= dwNumBuffers )
374 return apDSBuffer[dwIndex];
377 int Play( uint dwPriority, uint dwFlags, uint lVolume, uint lFrequency, uint lPan )
382 //IDirectSoundBuffer * pDSB;
384 // For Streaming Sound
385 dwFlags |= DSBPLAY_LOOPING;
387 if( apDSBuffer == null )
388 return CO_E_NOTINITIALIZED;
390 pDSB = GetFreeBuffer();
393 return 1; //DXUT_ERR( L"GetFreeBuffer", E_FAIL );
395 // Restore the buffer if it was lost
396 if( FAILED( hr = RestoreBuffer( pDSB, &bRestored ) ) )
397 return 1; //DXUT_ERR( L"RestoreBuffer", hr );
401 // The buffer was restored, so we need to fill it with new data
402 if( FAILED( hr = FillBufferWithSound( pDSB, false ) ) )
403 return 1; //DXUT_ERR( L"FillBufferWithSound", hr );
406 if( dwCreationFlags & DSBCAPS_CTRLVOLUME )
408 //IDirectSoundBuffer_SetVolume(pDSB, lVolume);
411 if( lFrequency != -1 &&
412 (dwCreationFlags & DSBCAPS_CTRLFREQUENCY) )
414 IDirectSoundBuffer_SetFrequency(pDSB, lFrequency);
417 if( dwCreationFlags & DSBCAPS_CTRLPAN )
419 //IDirectSoundBuffer_SetPan(pDSB, lPan);
421 result = IDirectSoundBuffer_Play(pDSB, 0, dwPriority, dwFlags);
430 if( apDSBuffer == null )
431 return CO_E_NOTINITIALIZED;
433 for( i=0; i<dwNumBuffers; i++ )
434 hr |= IDirectSoundBuffer_Stop(apDSBuffer[i]);
440 bool HandleWaveStreamNotification( )
442 DWORD dwCurrentPlayPos;
443 void * pDSLockedBuffer = null;
444 void * pDSLockedBuffer2 = null;
445 DWORD dwDSLockedBufferSize;
446 DWORD dwDSLockedBufferSize2;
447 uint playCursor, writeCursor;
456 // Figure out how much data has been played so far. When we have played
457 // past the end of the file, we will either need to start filling the
458 // buffer with silence or starting reading from the beginning of the file,
459 // depending if the user wants to loop the sound
460 if(IDirectSoundBuffer_GetCurrentPosition(apDSBuffer[0], &dwCurrentPlayPos, null))
464 playCursor = (dwCurrentPlayPos / dwNotifySize);
465 writeCursor = dwNextWriteOffset;
467 //printf("Play Cursor : %d, Write Cursor %d\n", playCursor, writeCursor);
468 if(writeCursor > playCursor)
471 size = (dwDSBufferSize / dwNotifySize) - start;
472 for(c = start; c<start + size; c++)
476 //printf("Writing %d - %d\n", c, c);
477 IDirectSoundBuffer_Lock(apDSBuffer[0], c * dwNotifySize, 1 * dwNotifySize, &pDSLockedBuffer, &dwDSLockedBufferSize, &pDSLockedBuffer2, &dwDSLockedBufferSize2, 0L);
478 //printf("In callback...");
479 callback(data, pDSLockedBuffer, dwDSLockedBufferSize);
481 IDirectSoundBuffer_Unlock(apDSBuffer[0], pDSLockedBuffer, dwDSLockedBufferSize, null, 0 );
482 dwNextWriteOffset = 0;
489 for(c = start; c<start + size; c++)
493 //printf("Writing %d - %d\n", c, c);
494 IDirectSoundBuffer_Lock(apDSBuffer[0], c * dwNotifySize, 1 * dwNotifySize, &pDSLockedBuffer, &dwDSLockedBufferSize, &pDSLockedBuffer2, &dwDSLockedBufferSize2, 0L);
495 //printf("In callback...");
496 callback(data, pDSLockedBuffer, dwDSLockedBufferSize);
498 IDirectSoundBuffer_Unlock(apDSBuffer[0], pDSLockedBuffer, dwDSLockedBufferSize, null, 0 );
499 dwNextWriteOffset = (playCursor) % (dwDSBufferSize / dwNotifySize);
506 size = playCursor - writeCursor;
507 for(c = start; c<start + size; c++)
511 //printf("Writing %d - %d\n", c, c);
512 IDirectSoundBuffer_Lock(apDSBuffer[0], c * dwNotifySize, 1 * dwNotifySize, &pDSLockedBuffer, &dwDSLockedBufferSize, &pDSLockedBuffer2, &dwDSLockedBufferSize2, 0L);
513 //printf("In callback...");
514 callback(data, pDSLockedBuffer, dwDSLockedBufferSize);
516 IDirectSoundBuffer_Unlock(apDSBuffer[0], pDSLockedBuffer, dwDSLockedBufferSize, null, 0 );
517 dwNextWriteOffset = (playCursor) % (dwDSBufferSize / dwNotifySize);
520 //printf("Next Offset: %d\n", dwNextWriteOffset);
535 dwNextWriteOffset = 0;
536 bFillNextNotificationWithSilence = false;
538 // Restore the buffer if it was lost
539 if(RestoreBuffer( apDSBuffer[0], &bRestored ))
544 // The buffer was restored, so we need to fill it with new data
545 if(FillBufferWithSound( apDSBuffer[0], false))
549 result = IDirectSoundBuffer_SetCurrentPosition(apDSBuffer[0], 0L );
553 int FillBufferWithSound( IDirectSoundBuffer * pDSB, bool bRepeatWavIfBufferLarger )
555 void * pDSLockedBuffer = null; // Pointer to locked buffer memory
556 DWORD dwDSLockedBufferSize = 0; // Size of the locked DirectSound buffer
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 // printf("Filling 16 spots\n");
573 IDirectSoundBuffer_Unlock(pDSB, pDSLockedBuffer, dwDSLockedBufferSize, null, 0 );
575 //dwNextWriteOffset = dwNotifySize*2;
580 static class CSoundManager
587 IDirectSound_Release( pDS );
590 int Initialize( void * hWnd )
596 IDirectSound_Release( pDS );
600 // Create IDirectSound using the primary sound device
601 if( FAILED( hr = DirectSoundCreate8( null, &pDS, null ) ) )
602 return 1; //DXUT_ERR( L"DirectSoundCreate8", hr );
604 // Set DirectSound coop level
605 if( FAILED( hr = IDirectSound_SetCooperativeLevel(pDS, hWnd, DSSCL_PRIORITY ) ) )
606 return 1; //DXUT_ERR( L"SetCooperativeLevel", hr );
612 int SetPrimaryBufferFormat( uint dwPrimaryChannels, uint dwPrimaryFreq, uint dwPrimaryBitRate )
615 IDirectSoundBuffer * pDSBPrimary = null;
620 return CO_E_NOTINITIALIZED;
622 // Get the primary buffer
623 ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
624 dsbd.dwSize = sizeof(DSBUFFERDESC);
625 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME;
626 dsbd.dwBufferBytes = 0;
627 dsbd.lpwfxFormat = null;
629 if( FAILED( hr = IDirectSound_CreateSoundBuffer(pDS, &dsbd, &pDSBPrimary, null ) ) )
630 return 1; // DXUT_ERR( L"CreateSoundBuffer", hr );
632 ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
633 wfx.wFormatTag = (WORD) WAVE_FORMAT_PCM;
634 wfx.nChannels = (WORD) dwPrimaryChannels;
635 wfx.nSamplesPerSec = (uint) dwPrimaryFreq;
636 wfx.wBitsPerSample = (WORD) dwPrimaryBitRate;
637 wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels);
638 wfx.nAvgBytesPerSec = (uint) (wfx.nSamplesPerSec * wfx.nBlockAlign);
640 if( FAILED( hr = IDirectSoundBuffer_SetFormat(pDSBPrimary, &wfx) ) )
641 return 1; //DXUT_ERR( L"SetFormat", hr );
645 IDirectSoundBuffer_Release( pDSBPrimary );
652 bool InitStreaming(StreamingSound * ppStreamingSound, uint dwNotifyCount, uint dwNotifySize, void * hNotifyEvent,
653 void (*callback)(void * data, void * buffer, int len), void * data, int nChannels, int freq,int bits)
655 IDirectSoundBuffer * pDSBuffer = null;
656 uint dwDSBufferSize = 0;
657 DSBPOSITIONNOTIFY * aPosNotify = null;
658 IDirectSoundNotify * pDSNotify = null;
664 if(!ppStreamingSound || !hNotifyEvent)
667 dwDSBufferSize = dwNotifySize * dwNotifyCount;
669 ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
670 dsbd.dwSize= sizeof(DSBUFFERDESC);
671 dsbd.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME;
672 dsbd.dwBufferBytes = dwDSBufferSize;
673 dsbd.guid3DAlgorithm = DS3DALG_DEFAULT;
674 dsbd.lpwfxFormat = new0 WAVEFORMATEX[1];
675 dsbd.lpwfxFormat->wFormatTag = WAVE_FORMAT_PCM;
676 dsbd.lpwfxFormat->nChannels = (uint16)nChannels;
677 dsbd.lpwfxFormat->nSamplesPerSec = freq;
678 dsbd.lpwfxFormat->wBitsPerSample = (uint16)bits;
679 dsbd.lpwfxFormat->nBlockAlign = dsbd.lpwfxFormat->nChannels * dsbd.lpwfxFormat->wBitsPerSample / 8;
680 dsbd.lpwfxFormat->nAvgBytesPerSec = dsbd.lpwfxFormat->nSamplesPerSec * dsbd.lpwfxFormat->nBlockAlign;
682 if(IDirectSound_CreateSoundBuffer(pDS, &dsbd, &pDSBuffer, null))
684 delete dsbd.lpwfxFormat;
687 delete dsbd.lpwfxFormat;
689 if(IDirectSoundBuffer_QueryInterface(pDSBuffer, &IID_IDirectSoundNotify, (void**)&pDSNotify))
695 aPosNotify = new DSBPOSITIONNOTIFY[ dwNotifyCount ];
696 if( aPosNotify == null )
699 for( i = 0; i < dwNotifyCount; i++ )
701 aPosNotify[i].dwOffset = (dwNotifySize * i) + dwNotifySize - 1;
702 aPosNotify[i].hEventNotify = hNotifyEvent;
705 if(IDirectSoundNotify_SetNotificationPositions(pDSNotify, dwNotifyCount, aPosNotify ))
708 IDirectSoundNotify_Release(pDSNotify);
709 delete( aPosNotify );
714 IDirectSoundNotify_Release(pDSNotify);
717 *ppStreamingSound = StreamingSound
719 dwCreationFlags = DSBCAPS_CTRLPAN|DSBCAPS_CTRLVOLUME, dwNumBuffers = 1, dwNotifySize = dwNotifySize, dwDSBufferSize = dwDSBufferSize, apDSBuffer = &pDSBuffer,
721 callback = callback, data = data
723 // ppStreamingSound->FillBufferWithSound( pDSBuffer, false );
728 static DSoundThread dSoundThread { };
730 static CSoundManager g_pSoundManager { };
731 static StreamingSound streamingSound;
733 static class DSoundThread : Thread
735 void * g_hNotificationEvent;
739 g_hNotificationEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
743 CloseHandle( g_hNotificationEvent );
748 SetPriority(timeCritical);
751 WaitForSingleObject(g_hNotificationEvent, INFINITE);
756 streamingSound.FillBufferWithSound( streamingSound.GetBuffer( 0 ), false );
757 streamingSound.Play( 0, 0, 0, -1, 0);
760 else if(!streamingSound.HandleWaveStreamNotification())