summaryrefslogtreecommitdiff
path: root/orkaudio/audiofile/PcmFile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'orkaudio/audiofile/PcmFile.cpp')
-rw-r--r--orkaudio/audiofile/PcmFile.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/orkaudio/audiofile/PcmFile.cpp b/orkaudio/audiofile/PcmFile.cpp
new file mode 100644
index 0000000..93f4743
--- /dev/null
+++ b/orkaudio/audiofile/PcmFile.cpp
@@ -0,0 +1,100 @@
+/*
+ * 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
+ *
+ */
+
+#include "PcmFile.h"
+
+PcmFile::PcmFile()
+{
+ m_stream = NULL;
+ m_mode = READ;
+ m_numChunksWritten = 0;
+}
+
+PcmFile::~PcmFile()
+{
+ Close();
+}
+
+
+void PcmFile::Close()
+{
+ if(m_stream)
+ {
+ ACE_OS::fclose(m_stream);
+ m_stream = NULL;
+ }
+}
+
+void PcmFile::WriteChunk(AudioChunkRef chunkRef)
+{
+ unsigned int numWritten = 0;
+ if (m_stream)
+ {
+ numWritten = ACE_OS::fwrite(chunkRef->m_pBuffer, sizeof(short), chunkRef->GetNumSamples(), m_stream);
+ }
+ else
+ {
+ throw(CStdString("Write attempt on unopened file:")+ m_filename);
+ }
+
+ if (numWritten != chunkRef->GetNumSamples())
+ {
+ throw(CStdString("Could not write to file:")+ m_filename);
+ }
+}
+
+int PcmFile::ReadChunkMono(AudioChunkRef& chunkRef)
+{
+ unsigned int numRead = 0;
+ if (m_stream)
+ {
+ chunkRef.reset(new AudioChunk());
+ short temp[PCM_FILE_DEFAULT_CHUNK_NUM_SAMPLES];
+ numRead = ACE_OS::fread(temp, sizeof(short), PCM_FILE_DEFAULT_CHUNK_NUM_SAMPLES, m_stream);
+ chunkRef->SetBuffer(temp, sizeof(short)*numRead, AudioChunk::PcmAudio);
+ }
+ else
+ {
+ throw(CStdString("Read attempt on unopened file:")+ m_filename);
+ }
+ return numRead;
+}
+
+
+void PcmFile::Open(CStdString& filename, fileOpenModeEnum mode, bool stereo, int sampleRate)
+{
+ if(!m_filename.Equals(filename))
+ {
+ m_filename = filename + ".pcm";
+ }
+ m_stream = NULL;
+ m_mode = mode;
+ if (mode == READ)
+ {
+ m_stream = ACE_OS::fopen((PCSTR)m_filename, "rb");
+ }
+ else
+ {
+ RecursiveMkdir(m_filename);
+ m_stream = ACE_OS::fopen((PCSTR)m_filename, "wb");
+ }
+ if(!m_stream)
+ {
+ throw(CStdString("Could not open file: ") + m_filename);
+ }
+}
+
+CStdString PcmFile::GetExtension()
+{
+ return ".pcm";
+}