summaryrefslogtreecommitdiff
path: root/orkbasecxx/audiofile/LibSndFileFile.cpp
diff options
context:
space:
mode:
authorHenri Herscher <henri@oreka.org>2006-06-30 19:39:27 +0000
committerHenri Herscher <henri@oreka.org>2006-06-30 19:39:27 +0000
commit868a99733ddb6a44f660f4aab9089fd619f38cfa (patch)
tree487309e2706b8389bc8985283c5d29334343bf7e /orkbasecxx/audiofile/LibSndFileFile.cpp
parent3868172df25ae59bd0ecc524b782ac4734812891 (diff)
Moved more stuff to orkbasecxx
git-svn-id: https://oreka.svn.sourceforge.net/svnroot/oreka/trunk@287 09dcff7a-b715-0410-9601-b79a96267cd0
Diffstat (limited to 'orkbasecxx/audiofile/LibSndFileFile.cpp')
-rw-r--r--orkbasecxx/audiofile/LibSndFileFile.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/orkbasecxx/audiofile/LibSndFileFile.cpp b/orkbasecxx/audiofile/LibSndFileFile.cpp
new file mode 100644
index 0000000..0589466
--- /dev/null
+++ b/orkbasecxx/audiofile/LibSndFileFile.cpp
@@ -0,0 +1,138 @@
+/*
+ * Oreka -- A media capture and retrieval platform
+ *
+ * Copyright (C) 2005, orecx LLC
+ *
+ * http://www.orecx.com
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License.
+ * Please refer to http://www.gnu.org/copyleft/gpl.html
+ *
+ */
+
+#define _WINSOCKAPI_ // prevents the inclusion of winsock.h
+
+#include "Utils.h"
+#include "LibSndFileFile.h"
+
+LibSndFileFile::LibSndFileFile(int 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_pFile = NULL;
+ m_numChunksWritten = 0;
+ m_mode = READ;
+ m_sampleRate = 0;
+}
+
+LibSndFileFile::~LibSndFileFile()
+{
+ Close();
+}
+
+void LibSndFileFile::Open(CStdString& filename, fileOpenModeEnum mode, bool stereo, int sampleRate)
+{
+ if(!m_filename.Equals(filename))
+ {
+ m_filename = filename + ".wav";
+ }
+ m_mode = mode;
+ stereo ? m_fileInfo.channels = 2 : m_fileInfo.channels = 1;
+ if(m_sampleRate == 0)
+ {
+ m_sampleRate = sampleRate;
+ m_fileInfo.samplerate = sampleRate;
+ }
+
+ if( (mode==WRITE) && !sf_format_check(&m_fileInfo))
+ {
+ throw(CStdString("libsndfile: Selected output format not supported"));
+ }
+
+ RecursiveMkdir(m_filename);
+
+ int sndFileMode;
+ mode == READ ? sndFileMode = SFM_READ : sndFileMode = SFM_WRITE;
+ m_pFile = sf_open((PCSTR)m_filename, sndFileMode, &m_fileInfo);
+
+ if(!m_pFile)
+ {
+ throw(CStdString("sf_open failed, audio file could not be created:"+ m_filename));
+ }
+}
+
+void LibSndFileFile::WriteChunk(AudioChunkRef chunkRef)
+{
+ if(chunkRef.get() == NULL)
+ {
+ return;
+ }
+ if(chunkRef->GetDetails()->m_numBytes == 0)
+ {
+ return;
+ }
+
+ if (m_pFile)
+ {
+ if( chunkRef->GetEncoding() == AlawAudio || chunkRef->GetEncoding() == UlawAudio)
+ {
+ 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())
+ {
+ 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++;
+ }
+ else
+ {
+ throw(CStdString("Write attempt on unopened file:")+ m_filename);
+ }
+}
+
+int LibSndFileFile::ReadChunkMono(AudioChunkRef& chunk)
+{
+ unsigned int numRead = 0;
+ if (m_pFile)
+ {
+ chunk.reset(new AudioChunk());
+ short temp[8000];
+ numRead = sf_read_short(m_pFile, temp, 8000);
+ AudioChunkDetails details;
+ details.m_encoding = PcmAudio;
+ chunk->SetBuffer(temp, sizeof(short)*numRead, details);
+ }
+ else
+ {
+ throw(CStdString("Read attempt on unopened file:")+ m_filename);
+ }
+ return numRead;
+}
+
+
+void LibSndFileFile::Close()
+{
+ if (m_pFile)
+ {
+ sf_close(m_pFile);
+ m_pFile = NULL;
+ }
+}
+
+CStdString LibSndFileFile::GetExtension()
+{
+ return ".wav";
+}