1
+ #ifdef _WIN32
1
2
2
3
#include " ofMediaFoundationSoundPlayer.h"
3
4
#include " ofLog.h"
@@ -168,7 +169,7 @@ void ofMediaFoundationSoundPlayer::sCloseAudioSystems() {
168
169
sNumInstances --;
169
170
if (sNumInstances <= 0 ) {
170
171
ofLogVerbose (" ofMediaFoundationSoundPlayer" ) << " closing XAudio2." ;
171
- sCloseXAudio2 ();
172
+ sCloseXAudio2 ();
172
173
}
173
174
ofMediaFoundationUtils::CloseMediaFoundation ();
174
175
if (sNumInstances < 0 ) {
@@ -189,7 +190,7 @@ ofMediaFoundationSoundPlayer::~ofMediaFoundationSoundPlayer() {
189
190
// --------------------
190
191
bool ofMediaFoundationSoundPlayer::load (const of::filesystem::path & fileName, bool stream) {
191
192
unload ();
192
-
193
+
193
194
auto filePath = fileName;
194
195
std::string fileStr = ofPathToString (fileName);
195
196
bool bStream = false ;
@@ -223,7 +224,7 @@ bool ofMediaFoundationSoundPlayer::load(const of::filesystem::path & fileName, b
223
224
224
225
225
226
LPCWSTR path = filePath.c_str ();
226
-
227
+
227
228
228
229
hr = MFCreateSourceReaderFromURL (
229
230
path,
@@ -255,14 +256,14 @@ bool ofMediaFoundationSoundPlayer::load(const of::filesystem::path & fileName, b
255
256
hr = mSrcReader ->GetNativeMediaType (MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0 , &nativeType);
256
257
257
258
auto nativeTypePtr = std::unique_ptr<IMFMediaType, MyComDeleterFunctor>(nativeType);
258
- // get a wave format
259
+ // get a wave format
259
260
hr = MFCreateWaveFormatExFromMFMediaType (nativeType, &nativeFormat, &formatSize);
260
-
261
+
261
262
mNumChannels = nativeFormat->nChannels ;
262
263
mSampleRate = nativeFormat->nSamplesPerSec ;
263
264
264
265
CoTaskMemFree (nativeFormat);
265
-
266
+
266
267
267
268
ComPtr<IMFMediaType> mediaType;
268
269
hr = MFCreateMediaType (mediaType.GetAddressOf ());
@@ -335,19 +336,19 @@ bool ofMediaFoundationSoundPlayer::load(const of::filesystem::path & fileName, b
335
336
}
336
337
337
338
ofLogVerbose (" ofMediaFoundationSoundPlayer::load" ) << " made it all the way to the end." ;
338
-
339
+
339
340
if (!mBStreaming ) {
340
341
mSrcReader .Reset ();
341
342
mSrcReader = nullptr ;
342
343
}
343
344
344
345
{
345
346
346
- // create stream context for listening to voice
347
+ // create stream context for listening to voice
347
348
mVoiceContext = std::make_shared<StreamingVoiceContext>();
348
349
// Create the source voice
349
350
IXAudio2SourceVoice* pSourceVoice = nullptr ;
350
- // setting max freq ratio to 3, though it may need to be higher to play at a
351
+ // setting max freq ratio to 3, though it may need to be higher to play at a
351
352
// faster pitch
352
353
if (mBStreaming ) {
353
354
hr = sXAudio2 ->CreateSourceVoice (
@@ -430,7 +431,7 @@ void ofMediaFoundationSoundPlayer::unload() {
430
431
mTotalNumFrames = 0 ;
431
432
mNumSamplesAlreadyPlayed = 0 ;
432
433
mBRequestNewReaderSample = false ;
433
-
434
+
434
435
LeaveCriticalSection (&m_critSec);
435
436
};
436
437
@@ -467,7 +468,7 @@ void ofMediaFoundationSoundPlayer::update(ofEventArgs& args) {
467
468
} else {
468
469
bRequestStop = true ;
469
470
// we need to request stop outside of the scope of the lock
470
- // since stop() also locks to set vars
471
+ // since stop() also locks to set vars
471
472
}
472
473
}
473
474
}
@@ -497,7 +498,7 @@ void ofMediaFoundationSoundPlayer::update(ofEventArgs& args) {
497
498
if (!xstate.BuffersQueued && mExtraVoices .size () < 1 ) {
498
499
// we have reached the end //
499
500
if (mBLoop ) {
500
- // set isPlaying to false, so that it will create a new instance
501
+ // set isPlaying to false, so that it will create a new instance
501
502
// for mVoice and not an Extra Voice
502
503
mPosPct = 0 .0f ;
503
504
mBIsPlaying = false ;
@@ -530,7 +531,7 @@ void ofMediaFoundationSoundPlayer::play() {
530
531
return ;
531
532
}
532
533
533
- // don't want a ton of loops going on here
534
+ // don't want a ton of loops going on here
534
535
if (mBLoop ) {
535
536
stop ();
536
537
}
@@ -539,7 +540,7 @@ void ofMediaFoundationSoundPlayer::play() {
539
540
}
540
541
541
542
if (mBStreaming ) {
542
- // just in case, multiplay is not supported for streams
543
+ // just in case, multiplay is not supported for streams
543
544
_clearExtraVoices ();
544
545
}
545
546
@@ -583,7 +584,7 @@ void ofMediaFoundationSoundPlayer::play() {
583
584
XAUDIO2_BUFFER buffer = {};
584
585
buffer.pAudioData = mBuffer .data ();
585
586
// tell the source voice not to expect any data after this buffer
586
- buffer.Flags = XAUDIO2_END_OF_STREAM;
587
+ buffer.Flags = XAUDIO2_END_OF_STREAM;
587
588
buffer.AudioBytes = mBuffer .size ();
588
589
589
590
hr = pSourceVoice->SubmitSourceBuffer (&buffer);
@@ -605,7 +606,7 @@ void ofMediaFoundationSoundPlayer::play() {
605
606
buffer.AudioBytes = mBuffer .size ();
606
607
607
608
mVoice ->SubmitSourceBuffer (&buffer);
608
-
609
+
609
610
}
610
611
611
612
mVoice ->SetVolume (mVolume );
@@ -711,7 +712,7 @@ void ofMediaFoundationSoundPlayer::setPaused(bool bP) {
711
712
// --------------------
712
713
void ofMediaFoundationSoundPlayer::setLoop (bool bLp) {
713
714
if (bLp) {
714
- // we don't want a lot of looping iterations
715
+ // we don't want a lot of looping iterations
715
716
_clearExtraVoices ();
716
717
}
717
718
mBLoop = bLp;
@@ -740,7 +741,7 @@ void ofMediaFoundationSoundPlayer::setPosition(float pct) {
740
741
ofLogWarning (" ofMediaFoundationSoundPlayer::setPosition" ) << " unable to seek." ;
741
742
return ;
742
743
}
743
-
744
+
744
745
// ok we need to kill buffers and resubmit a buffer
745
746
if (mVoice ) {
746
747
std::ignore = mVoice ->Stop ();
@@ -785,7 +786,7 @@ void ofMediaFoundationSoundPlayer::setPositionMS(int ms) {
785
786
};
786
787
787
788
// --------------------
788
- float ofMediaFoundationSoundPlayer::getPosition () const {
789
+ float ofMediaFoundationSoundPlayer::getPosition () const {
789
790
return mPosPct ;
790
791
};
791
792
@@ -880,7 +881,7 @@ void ofMediaFoundationSoundPlayer::OnSourceReaderEvent(HRESULT hrStatus, DWORD d
880
881
}
881
882
882
883
hr = mediaBuffer->Unlock ();
883
-
884
+
884
885
if (mVoice && mVoiceContext ) {
885
886
XAUDIO2_VOICE_STATE state;
886
887
@@ -909,7 +910,7 @@ void ofMediaFoundationSoundPlayer::OnSourceReaderEvent(HRESULT hrStatus, DWORD d
909
910
std::unique_lock<std::mutex> lk (mSrcReaderMutex );
910
911
mBRequestNewReaderSample = true ;
911
912
}
912
-
913
+
913
914
}
914
915
}
915
916
@@ -953,7 +954,7 @@ void ofMediaFoundationSoundPlayer::_setPan(IXAudio2SourceVoice* avoice, float ap
953
954
float outputMatrix[8 ];
954
955
for (int i = 0 ; i < 8 ; i++) outputMatrix[i] = 0 ;
955
956
956
- // pan of -1.0 indicates all left speaker,
957
+ // pan of -1.0 indicates all left speaker,
957
958
// 1.0 is all right speaker, 0.0 is split between left and right
958
959
float left = 0 .5f - apan / 2 ;
959
960
float right = 0 .5f + apan / 2 ;
@@ -990,7 +991,7 @@ void ofMediaFoundationSoundPlayer::_setPan(IXAudio2SourceVoice* avoice, float ap
990
991
991
992
// Assuming pVoice sends to pMasteringVoice
992
993
993
- // TODO: Cache this
994
+ // TODO: Cache this
994
995
XAUDIO2_VOICE_DETAILS MasterVoiceDetails;
995
996
sXAudioMasteringVoice ->GetVoiceDetails (&MasterVoiceDetails);
996
997
@@ -1019,7 +1020,7 @@ bool ofMediaFoundationSoundPlayer::_readToBuffer(IMFSourceReader* areader) {
1019
1020
HRESULT hr = areader->ReadSample (
1020
1021
MF_SOURCE_READER_FIRST_AUDIO_STREAM,
1021
1022
0 , // Flags.
1022
- &streamIndex, // Receives the actual stream index.
1023
+ &streamIndex, // Receives the actual stream index.
1023
1024
&flags, // Receives status flags.
1024
1025
&llAudioTimeStamp, // Receives the time stamp.
1025
1026
&audioSample // Receives the sample or NULL.
@@ -1049,14 +1050,14 @@ bool ofMediaFoundationSoundPlayer::_readToBuffer(IMFSourceReader* areader) {
1049
1050
if (hr != S_OK) {
1050
1051
continue ;
1051
1052
}
1052
-
1053
+
1053
1054
size_t numFramesRead = uint64_t (sampleBufferLength) / (bytes64 * numChannels64);
1054
1055
ofLogVerbose (" ofMediaFoundationSoundPlayer::_readToBuffer" ) << " sampleBufferLength : " << sampleBufferLength << " num frames: " << numFramesRead << std::endl;
1055
1056
totalFrames += numFramesRead;
1056
1057
std::vector<BYTE> tempBuffer;
1057
1058
tempBuffer.resize (sampleBufferLength, 0 );
1058
1059
memcpy_s (tempBuffer.data (), sampleBufferLength, audioData, sampleBufferLength);
1059
- // add into the main buffer?
1060
+ // add into the main buffer?
1060
1061
mBuffer .insert (mBuffer .end (), tempBuffer.begin (), tempBuffer.end ());
1061
1062
1062
1063
hr = mediaBuffer->Unlock ();
@@ -1073,3 +1074,5 @@ bool ofMediaFoundationSoundPlayer::_readToBuffer(IMFSourceReader* areader) {
1073
1074
ofLogVerbose (" ofMediaFoundationSoundPlayer::_readToBuffer" ) << " Total frames read: " << (totalFrames) << " mTotalNumFrames: " << mTotalNumFrames << " dur millis: " << durMillis << " dur seconds: " << durSeconds << std::endl;
1074
1075
return mBuffer .size () > 0 ;
1075
1076
}
1077
+
1078
+ #endif
0 commit comments