diff options
author | Gerald Begumisa <ben_g@users.sourceforge.net> | 2008-02-27 19:21:41 +0000 |
---|---|---|
committer | Gerald Begumisa <ben_g@users.sourceforge.net> | 2008-02-27 19:21:41 +0000 |
commit | 0937742d2f5689c93efca3a5a56e8b36f81152c7 (patch) | |
tree | a0c817c8b45c888c88d020583a45a5ec65a225dd /orkbasecxx/audiofile/LibSndFileFile.cpp | |
parent | 9483156b326918005bb42751cc0e37a16b3e3e41 (diff) |
Added support for storage of stereo files - typically files containing 2 separate channels. Added the config.xml variable StereoRecording which needs to be set to true for this to work. Added another config.xml variable TapeNumChannels which should be set to 2 to test this. If TapeNumChannels is set to 1, default behaviour happens. Currently only 1 and 2 are the only supported values for TapeNumChannels.
git-svn-id: https://oreka.svn.sourceforge.net/svnroot/oreka/trunk@526 09dcff7a-b715-0410-9601-b79a96267cd0
Diffstat (limited to 'orkbasecxx/audiofile/LibSndFileFile.cpp')
-rw-r--r-- | orkbasecxx/audiofile/LibSndFileFile.cpp | 115 |
1 files changed, 103 insertions, 12 deletions
diff --git a/orkbasecxx/audiofile/LibSndFileFile.cpp b/orkbasecxx/audiofile/LibSndFileFile.cpp index c7377fd..6250057 100644 --- a/orkbasecxx/audiofile/LibSndFileFile.cpp +++ b/orkbasecxx/audiofile/LibSndFileFile.cpp @@ -19,12 +19,12 @@ LibSndFileFile::LibSndFileFile(int fileFormat) { - m_fileInfo.format = fileFormat; + m_fileInfo.format = fileFormat; m_fileInfo.frames = 0; - m_fileInfo.samplerate = 0; - m_fileInfo.channels = 0; - m_fileInfo.sections = 0; - m_fileInfo.seekable = 0; + m_fileInfo.samplerate = 0; + m_fileInfo.channels = 0; + m_fileInfo.sections = 0; + m_fileInfo.seekable = 0; m_pFile = NULL; m_numChunksWritten = 0; m_mode = READ; @@ -43,7 +43,21 @@ void LibSndFileFile::Open(CStdString& filename, fileOpenModeEnum mode, bool ster m_filename = filename + ".wav"; } m_mode = mode; - stereo ? m_fileInfo.channels = 2 : m_fileInfo.channels = 1; + if(CONFIG.m_stereoRecording == true) + { + if(CONFIG.m_tapeNumChannels > 1) + { + m_fileInfo.channels = CONFIG.m_tapeNumChannels; + } + else + { + m_fileInfo.channels = 1; + } + } + else + { + m_fileInfo.channels = 1; + } if(m_sampleRate == 0) { m_sampleRate = sampleRate; @@ -82,18 +96,95 @@ void LibSndFileFile::WriteChunk(AudioChunkRef chunkRef) { if( chunkRef->GetEncoding() == AlawAudio || chunkRef->GetEncoding() == UlawAudio) { - if(sf_write_raw(m_pFile, chunkRef->m_pBuffer, chunkRef->GetNumSamples()) != chunkRef->GetNumSamples()) + // We have faith that whoever is producing these chunks created them + // with the same number of channels that we have opened the soundfile + // with above - this is enforced in the RtpMixer + if(chunkRef->m_numChannels > 0 && CONFIG.m_stereoRecording == true) + { + int numBytes = 0; + unsigned char *muxAudio = NULL; + unsigned char *wrPtr = NULL; + + numBytes = chunkRef->GetNumSamples() * chunkRef->m_numChannels; + muxAudio = (unsigned char*)malloc(numBytes); + wrPtr = muxAudio; + + if(!muxAudio) + { + CStdString exception; + + exception.Format("sf_write_raw failed for stereo write: could not allocate %d bytes", numBytes); + throw(exception); + } + + for(int x = 0; x < chunkRef->GetNumSamples(); x++) + { + for(int i = 0; i < chunkRef->m_numChannels; i++) + { + *wrPtr++ = (unsigned char)*((unsigned char*)(chunkRef->m_pChannelAudio[i])+x); + } + } + + if(sf_write_raw(m_pFile, muxAudio, numBytes) != numBytes) + { + CStdString numChunksWrittenString = IntToString(m_numChunksWritten); + free(muxAudio); + throw(CStdString("sf_write_raw failed, audio file " + m_filename + " could not be written after " + numChunksWrittenString + " chunks written")); + } + free(muxAudio); + } + else { - CStdString numChunksWrittenString = IntToString(m_numChunksWritten); - throw(CStdString("sf_write_raw failed, audio file " + m_filename + " could not be written after " + numChunksWrittenString + " chunks written")); + if(sf_write_raw(m_pFile, chunkRef->m_pBuffer, chunkRef->GetNumSamples()) != chunkRef->GetNumSamples()) + { + CStdString numChunksWrittenString = IntToString(m_numChunksWritten); + throw(CStdString("sf_write_raw failed, audio file " + m_filename + " could not be written after " + numChunksWrittenString + " chunks written")); + } } } else if (chunkRef->GetEncoding() == PcmAudio) { - if(sf_write_short(m_pFile, (short*)chunkRef->m_pBuffer, chunkRef->GetNumSamples()) != chunkRef->GetNumSamples()) + // We have faith that whoever is producing these chunks created them + // with the same number of channels that we have opened the soundfile + // with above - this is enforced in the RtpMixer + if(chunkRef->m_numChannels > 0 && CONFIG.m_stereoRecording == true) + { + int numShorts = 0; + short *muxAudio = NULL; + short *wrPtr = NULL; + + numShorts = chunkRef->GetNumSamples()*chunkRef->m_numChannels; + muxAudio = (short*)calloc(numShorts, sizeof(short)); + wrPtr = muxAudio; + if(!muxAudio) + { + CStdString exception; + + exception.Format("sf_write_raw failed for stereo write: could not allocate %d bytes", numShorts*sizeof(short)); + throw(exception); + } + for(int x = 0; x < chunkRef->GetNumSamples(); x++) + { + for(int i = 0; i < chunkRef->m_numChannels; i++) + { + *wrPtr++ = (short)*((short*)(chunkRef->m_pChannelAudio[i])+x); + } + } + if(sf_write_short(m_pFile, muxAudio, numShorts) != numShorts) + { + CStdString numChunksWrittenString = IntToString(m_numChunksWritten); + free(muxAudio); + throw(CStdString("sf_write_short failed, audio file " + m_filename + " could not be written after " + numChunksWrittenString + " chunks written")); + } + free(muxAudio); + } + else { - CStdString numChunksWrittenString = IntToString(m_numChunksWritten); - throw(CStdString("sf_write_short failed, audio file " + m_filename + " could not be written after " + numChunksWrittenString + " chunks written")); + if(sf_write_short(m_pFile, (short*)chunkRef->m_pBuffer, chunkRef->GetNumSamples()) != chunkRef->GetNumSamples()) + { + CStdString numChunksWrittenString = IntToString(m_numChunksWritten); + throw(CStdString("sf_write_short failed, audio file " + m_filename + " could not be written after " + numChunksWrittenString + " chunks written")); + } } } m_numChunksWritten++; |