Playing ogg and wav at the same time July 11, 2011 07:49PM | Registered: 13 years ago Posts: 1 |
Re: Playing ogg and wav at the same time July 11, 2011 09:51PM | Registered: 13 years ago Posts: 87 |
Re: Playing ogg and wav at the same time July 12, 2011 09:10AM | Registered: 13 years ago Posts: 99 |
#ifndef SoundManager_H_ #define SoundManager_H_ #include#include #include #include
#include#include "SoundManager.h" #include "malloc.h" #include "debug.h" #include "string.h" #include "config.h" #include "WiiManager.h" #include "WiiFile.h" #include "Util.h" #include "tremor/ivorbiscodec.h" #include "tremor/ivorbisfile.h" int RawSample::Play(u8 VolumeLeft, u8 VolumeRight, bool bLoop) { if ( !Singleton ::GetInstanceByRef().IsGameStateShowingGame() ) return 0; int Chan = ASND_GetFirstUnusedVoice(); if (bLoop) ASND_SetInfiniteVoice( Chan, m_NumberOfChannels,m_SampleRate,0, m_RawData, m_RawDataLength , VolumeLeft, VolumeRight); else ASND_SetVoice( Chan, m_NumberOfChannels,m_SampleRate,0, m_RawData, m_RawDataLength , VolumeLeft, VolumeRight, NULL); return Chan; } int SoundManager::PlaySound(HashLabel SoundName, u8 VolumeLeft, u8 VolumeRight, bool bLoop) { if ( !Singleton ::GetInstanceByRef().IsGameStateShowingGame() ) return 0; //todo - replace [] will something that will fail rather then create a new item we are looking for ! RawSample* pRaw = GetSound(SoundName); if (pRaw!=NULL) { return m_SoundContainer[SoundName]->Play( VolumeLeft, VolumeRight, bLoop ); } return -1; } void SoundManager::StopSound(u8 Chan) { if ( !Singleton ::GetInstanceByRef().IsGameStateShowingGame() ) return; ASND_StopVoice(Chan); } SoundManager::SoundManager( ) { Init(); } SoundManager::~SoundManager( ) { UnInit(); } void SoundManager::Init( ) { #ifdef ENABLE_SOUND SND_Init(INIT_RATE_48000); // note: SND_xxx is the same as ASND_xxx SND_Pause(0); #endif } void SoundManager::UnInit( ) { ASND_Pause(1); //pause ASND_End(); } void SoundManager::LoadSound( std::string FullFileNameWithPath,std::string LookUpName ) { Util::StringToLower(FullFileNameWithPath); if (WiiFile::GetFileExtension(FullFileNameWithPath) == "wav") { StoreSoundFromWav(FullFileNameWithPath,LookUpName); } else if (WiiFile::GetFileExtension(FullFileNameWithPath) == "ogg") { StoreSoundFromOgg(FullFileNameWithPath,LookUpName); } } // StoreSoundFromWav is a supporting function for LoadSound void SoundManager::StoreSoundFromWav( std::string FullFileNameWithPath,std::string LookUpName ) { FILE* WAVFile = WiiFile::FileOpenForRead((FullFileNameWithPath).c_str()); //------------------------------------------------------------- // "RIFF" chunk discriptor RIFFChunk RIFFChunkData; fread(&RIFFChunkData, sizeof(RIFFChunk), 1, WAVFile); if ( strncmp ( RIFFChunkData.RIFF, "RIFF", 4 ) != 0 ) ExitPrintf("'RIFF' check failed %c%c%c%c" ,RIFFChunkData.RIFF[0],RIFFChunkData.RIFF[1],RIFFChunkData.RIFF[2],RIFFChunkData.RIFF[3] ); if ( strncmp ( RIFFChunkData.RIFFType, "WAVE", 4 ) != 0 ) ExitPrintf("'WAVE' check failed %c%c%c%c",RIFFChunkData.RIFFType[0],RIFFChunkData.RIFFType[1],RIFFChunkData.RIFFType[2],RIFFChunkData.RIFFType[3]); //------------------------------------------------------------- // "fmt" sub-chunk fmtChunk fmtChunkData; fread(&fmtChunkData, sizeof(fmtChunk), 1, WAVFile); if ( strncmp ( fmtChunkData.fmt, "fmt ", 4 ) != 0 ) ExitPrintf("fmt' check failed %c%c%c%c" , fmtChunkData.fmt[0],fmtChunkData.fmt[1],fmtChunkData.fmt[2],fmtChunkData.fmt[3]); fmtChunkData.Channels = Util::ENDIAN16(fmtChunkData.Channels); fmtChunkData.SampleRate = Util::ENDIAN32(fmtChunkData.SampleRate); //------------------------------------------------------------- // "data" sub-chunk dataChunk dataChunkData; fread(&dataChunkData, sizeof(dataChunk), 1, WAVFile); if ( strncmp ( dataChunkData.data, "data", 4 ) != 0 ) ExitPrintf("'data' check failed"); dataChunkData.dataLength = Util::ENDIAN32(dataChunkData.dataLength); //------------------------------------------------------------- // Raw sound data RawSample* pRawSample( new RawSample ); u8* pData = (u8*)memalign(32, dataChunkData.dataLength ); fread(pData, 1, dataChunkData.dataLength, WAVFile); fmtChunkData.BitResolution = Util::ENDIAN16(fmtChunkData.BitResolution); pRawSample->SetRawData(pData); pRawSample->SetRawDataLength(dataChunkData.dataLength); if (fmtChunkData.BitResolution == 16 ) { if (fmtChunkData.Channels == 1) pRawSample->SetNumberOfChannels(VOICE_MONO_16BIT); else pRawSample->SetNumberOfChannels(VOICE_STEREO_16BIT); } else { if (fmtChunkData.Channels == 1) pRawSample->SetNumberOfChannels(VOICE_MONO_8BIT); else pRawSample->SetNumberOfChannels(VOICE_STEREO_8BIT); } pRawSample->SetSampleRate(fmtChunkData.SampleRate); pRawSample->SetBitsPerSample(fmtChunkData.BitResolution); //printf("dataLength %d",dataChunkData.dataLength); //printf("Channels %d",fmtChunkData.Channels); //printf("SampleRate %d",fmtChunkData.SampleRate); //printf("BitResolution %d",fmtChunkData.BitResolution); //------------------------------------------------------------- u16* pData16 = (u16*)pData; if (fmtChunkData.BitResolution == 16) // 8 or 16 bit samples - anything other than 16 is just seen as 8 bit { for (u32 i(0); i = Util::ENDIAN16(pData16); } } fclose ( WAVFile ); m_SoundContainer[ (HashLabel)LookUpName ] = pRawSample ; } // StoreSoundFromOgg is a supporting function for LoadSound void SoundManager::StoreSoundFromOgg(std::string FullFileNameWithPath,std::string LookUpName) { //printf("start ogg"); char PCM_Out[4096]; // quick working fudge - todo make it dynamic OggVorbis_File vf; int current_section; if(ov_open( WiiFile::FileOpenForRead( FullFileNameWithPath.c_str() ) , &vf, NULL, 0) < 0) { ExitPrintf("Not a Ogg file\n"); } // We're going to pull the channel and bitrate info from the file using ov_info() //and show them to the user. We also want to pull out and show the user a comment attached to the file using ov_comment(). char **ptr=ov_comment(&vf,-1)->user_comments; vorbis_info *vi=ov_info(&vf,-1); while(*ptr){ fprintf(stderr,"%s\n",*ptr); ++ptr; } //printf("\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate); //printf("\nDecoded length: %ld samples\n",(long)ov_pcm_total(&vf,-1)); //printf("Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor); //printf("--------"); //printf("%ld", ((long)ov_pcm_total(&vf,-1)) * vi->channels * (int)sizeof(u16) ); //printf("--------"); // Raw sound data RawSample* pRawSample( new RawSample ); int RawDataLength = ((long)ov_pcm_total(&vf,-1)) * vi->channels * (int)sizeof(u16); int NumberOfChannels = VOICE_STEREO_16BIT; if (vi->channels==1) NumberOfChannels = VOICE_MONO_16BIT; int SampleRate = vi->rate; int BitsPerSample = 16; //sizeof(u16); RawDataLength/=2; u8* pRawData = (u8*)memalign(32, RawDataLength ); if (pRawData==NULL) { ExitPrintf("memalign NULL"); } u8* pTemp = pRawData; int eof=0; int CheckTotal=0; while(!eof) { long ret=ov_read(&vf,PCM_Out,sizeof(PCM_Out),¤t_section); if (ret == 0) { eof=1; } else if (ret < 0) // error in the stream { } else // we don't bother dealing with sample rate changes, etc, but you'll have to { if (CheckTotal>=RawDataLength) { //printf("half"); break; } memcpy(pTemp, PCM_Out, ret) ;//fwrite(PCM_Out,1,ret,pOutFile); pTemp+=ret; CheckTotal += ret; } } ov_clear(&vf); //printf("SampleRate %d",SampleRate); //printf("SampleRate %d",BitsPerSample); //printf("SampleRate %d",NumberOfChannels); //printf("SampleRate %d",RawDataLength); pRawSample->SetRawData(pRawData); pRawSample->SetRawDataLength(RawDataLength); pRawSample->SetNumberOfChannels(NumberOfChannels); pRawSample->SetSampleRate(SampleRate); pRawSample->SetBitsPerSample(BitsPerSample); //------------------------------------------------------------- m_SoundContainer[ (HashLabel)LookUpName ] = pRawSample ; }