1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* 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;
m_sampleRate = 0;
}
PcmFile::~PcmFile()
{
Close();
}
void PcmFile::Close()
{
if(m_stream)
{
ACE_OS::fclose(m_stream);
m_stream = NULL;
}
}
void PcmFile::WriteChunk(AudioChunkRef chunkRef)
{
if(chunkRef.get() == NULL)
{
return;
}
if(chunkRef->GetDetails()->m_numBytes == 0)
{
return;
}
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);
AudioChunkDetails details;
details.m_encoding = PcmAudio;
chunkRef->SetBuffer(temp, sizeof(short)*numRead, details);
}
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_sampleRate == 0)
{
m_sampleRate = 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";
}
|