CLAM-Development  1.4.0
PCMAudioStream.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "PCMAudioStream.hxx"
23 #include "AudioFile.hxx"
24 #include "Assert.hxx"
25 
26 #ifdef CLAM_DOUBLE
27 #define CLAM_sf_readf sf_readf_double
28 #define CLAM_sf_writef sf_writef_double
29 #else
30 #define CLAM_sf_readf sf_readf_float
31 #define CLAM_sf_writef sf_writef_float
32 #endif
33 
34 namespace CLAM
35 {
36 
37 namespace AudioCodecs
38 {
40  : mFileHandle( NULL )
41  {
42  mName = file.GetLocation();
43  mNativeFileParams.channels = file.GetHeader().GetChannels();
44  mNativeFileParams.samplerate = (int) file.GetHeader().GetSampleRate();
45  mNativeFileParams.format = file.GetHeader().GetFormat() | file.GetHeader().GetEncoding() | file.GetHeader().GetEndianess();
46  SetChannels( mNativeFileParams.channels );
47  }
48 
50  {
51  Dispose();
52  }
53 
55  {
56  mFileHandle = sf_open( mName.c_str(),
57  SFM_READ,
58  &mNativeFileParams );
59 
60  CLAM_ASSERT( mFileHandle != NULL,
61  "Cannot open file for reading!!!" );
62  mEOFReached = false;
63  mFramePosition = 0;
64  }
65 
67  {
68  mFileHandle = sf_open( mName.c_str(),
69  SFM_WRITE,
70  &mNativeFileParams );
71 
72  CLAM_ASSERT( mFileHandle != NULL,
73  "Cannot open file for writing!!!" );
74  }
75 
77  {
78  if ( mFileHandle )
79  {
80  sf_close( mFileHandle );
81  mFileHandle = NULL;
82  }
83  }
84 
86  {
87  int nChannels = mNativeFileParams.channels;
88  unsigned nFrames = mInterleavedData.size()/nChannels;
89 
90  TData* begin = &mInterleavedData[0];
91  const TData* end = begin + mInterleavedData.size();
92 
93  sf_count_t framesRead = CLAM_sf_readf( mFileHandle, begin, nFrames );
94  mFramesLastRead = (TSize)framesRead;
96 
97  if (not framesRead) // No more data to read - EOF reached
98  {
99  mEOFReached = true;
100  return;
101  }
102  if (framesRead<nFrames) // EOF reached
103  {
104  mEOFReached = true;
105  for (TData * p=begin+framesRead*nChannels; p!=end; p++)
106  *p= 0.0;
107  }
108  }
109 
111  {
112  unsigned nFrames = mInterleavedData.size()/mChannels;
113  const TData* begin = &mInterleavedData[0];
114  sf_count_t samplesWritten = CLAM_sf_writef( mFileHandle,
115  begin,
116  nFrames );
117 
118  CLAM_DEBUG_ASSERT( samplesWritten == nFrames,
119  "Could not write all samples to disk!" );
120  }
121 
122  void PCMAudioStream::SeekTo(long unsigned framePosition)
123  {
124  mFramePosition = sf_seek(mFileHandle, framePosition, SEEK_SET);
125  mEOFReached = false;
126  }
127 }
128 }
129