@@ -332,11 +332,10 @@ void CSound::RateConvert(int SampleID)
332332 pSample->m_NumFrames = NumFrames;
333333}
334334
335- ISound::CSampleHandle CSound::LoadOpus (const char *pFilename )
335+ ISound::CSampleHandle CSound::LoadOpusMemory (const char *pContext, const unsigned char *pData, int DataSize )
336336{
337337 CSample *pSample;
338338 int SampleID = -1 ;
339-
340339 // don't waste memory on sound when we are stress testing
341340#ifdef CONF_DEBUG
342341 if (m_pConfig->m_DbgStress )
@@ -349,23 +348,16 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
349348
350349 if (!m_pStorage)
351350 return CSampleHandle ();
352-
353- lock_wait (m_SoundLock);
354- unsigned char *pFileData;
355- unsigned FileSize;
356- if (!m_pStorage->ReadFile (pFilename, IStorage::TYPE_ALL, (void **) &pFileData, &FileSize))
357- {
358- dbg_msg (" sound/opus" , " failed to open file. filename='%s'" , pFilename);
359- lock_unlock (m_SoundLock);
351+
352+ if (!pContext || !pData || DataSize <= 0 )
360353 return CSampleHandle ();
361- }
362354
355+ lock_wait (m_SoundLock);
363356 int Error = 0 ;
364- OggOpusFile *pOpusFile = op_open_memory (pFileData, FileSize , &Error);
357+ OggOpusFile *pOpusFile = op_open_memory (pData, DataSize , &Error);
365358 if (!pOpusFile)
366359 {
367- dbg_msg (" sound/opus" , " failed to open opus file '%s': error %d" , pFilename, Error);
368- mem_free (pFileData);
360+ dbg_msg (" sound/opus" , " failed to open opus '%s': error %d" , pContext, Error);
369361 lock_unlock (m_SoundLock);
370362 return CSampleHandle ();
371363 }
@@ -374,19 +366,17 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
374366 ;
375367 if (NumChannels < 0 || NumChannels > 2 )
376368 {
377- dbg_msg (" sound/opus" , " only mono/stereo supported. channels=%d, file='%s'" , NumChannels, pFilename );
369+ dbg_msg (" sound/opus" , " only mono/stereo supported. channels=%d, file='%s'" , NumChannels, pContext );
378370 op_free (pOpusFile);
379- mem_free (pFileData);
380371 lock_unlock (m_SoundLock);
381372 return CSampleHandle ();
382373 }
383374
384375 int TotalSamples = op_pcm_total (pOpusFile, -1 );
385376 if (TotalSamples < 0 )
386377 {
387- dbg_msg (" sound/opus" , " failed to get number of samples, error %d. file='%s'" , TotalSamples, pFilename );
378+ dbg_msg (" sound/opus" , " failed to get number of samples, error %d. file='%s'" , TotalSamples, pContext );
388379 op_free (pOpusFile);
389- mem_free (pFileData);
390380 lock_unlock (m_SoundLock);
391381 return CSampleHandle ();
392382 }
@@ -395,7 +385,6 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
395385 if (SampleID < 0 )
396386 {
397387 op_free (pOpusFile);
398- mem_free (pFileData);
399388 lock_unlock (m_SoundLock);
400389 return CSampleHandle ();
401390 }
@@ -404,7 +393,6 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
404393 if (!pSample->m_pData )
405394 {
406395 op_free (pOpusFile);
407- mem_free (pFileData);
408396 lock_unlock (m_SoundLock);
409397 return CSampleHandle ();
410398 }
@@ -417,8 +405,7 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
417405 {
418406 mem_free (pSample->m_pData );
419407 op_free (pOpusFile);
420- mem_free (pFileData);
421- dbg_msg (" sound/opus" , " op_read error %d at %d. file='%s'" , Read, Pos, pFilename);
408+ dbg_msg (" sound/opus" , " op_read error %d at %d. file='%s'" , Read, Pos, pContext);
422409 return CSampleHandle ();
423410 }
424411 else if (Read == 0 ) // EOF
@@ -428,7 +415,6 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
428415 }
429416
430417 op_free (pOpusFile);
431- mem_free (pFileData);
432418
433419 pSample->m_Channels = NumChannels;
434420 pSample->m_Rate = 48000 ;
@@ -438,13 +424,56 @@ ISound::CSampleHandle CSound::LoadOpus(const char *pFilename)
438424 pSample->m_PausedAt = 0 ;
439425
440426 if (m_pConfig->m_Debug )
441- dbg_msg (" sound/opus" , " loaded %s (%d samples, %d channels)" , pFilename , TotalSamples, NumChannels);
427+ dbg_msg (" sound/opus" , " loaded %s (%d samples, %d channels)" , pContext , TotalSamples, NumChannels);
442428
443429 RateConvert (SampleID);
444430 lock_unlock (m_SoundLock);
445431 return CreateSampleHandle (SampleID);
446432}
447433
434+ ISound::CSampleHandle CSound::LoadOpus (const char *pFilename)
435+ {
436+ // don't waste memory on sound when we are stress testing
437+ #ifdef CONF_DEBUG
438+ if (m_pConfig->m_DbgStress )
439+ return CSampleHandle ();
440+ #endif
441+
442+ // no need to load sound when we are running with no sound
443+ if (!m_SoundEnabled)
444+ return CSampleHandle ();
445+
446+ if (!m_pStorage)
447+ return CSampleHandle ();
448+
449+ unsigned char *pFileData;
450+ unsigned FileSize;
451+ if (!m_pStorage->ReadFile (pFilename, IStorage::TYPE_ALL, (void **) &pFileData, &FileSize))
452+ {
453+ dbg_msg (" sound/opus" , " failed to open file. filename='%s'" , pFilename);
454+ return CSampleHandle ();
455+ }
456+
457+ ISound::CSampleHandle Sample = LoadOpusMemory (pFilename, pFileData, FileSize);
458+ mem_free (pFileData);
459+ return Sample;
460+ }
461+
462+ bool CSound::UnloadSample (CSampleHandle *pSampleID)
463+ {
464+ if (!pSampleID)
465+ return false ;
466+
467+ if (m_aSamples[pSampleID->Id ()].m_pData )
468+ {
469+ mem_free (m_aSamples[pSampleID->Id ()].m_pData );
470+ m_aSamples[pSampleID->Id ()].m_pData = 0 ;
471+ pSampleID->Invalidate ();
472+ return true ;
473+ }
474+ return false ;
475+ }
476+
448477void CSound::SetListenerPos (float x, float y)
449478{
450479 m_CenterX = (int ) x;
0 commit comments