CLAM-Development  1.4.0
AudioCodecs_Stream.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 "AudioCodecs_Stream.hxx"
23 
24 namespace CLAM
25 {
26 
27 namespace AudioCodecs
28 {
30  : mFramesLastRead(0)
31  , mFramePosition(0)
32  {
33  }
34 
36  {
37  }
38 
39  bool Stream::ReadData( int channel, TData* buffer, unsigned nFrames )
40  {
41  AdjustInterleavedBuffer( nFrames );
43  if ( mFramesLastRead == 0 ) return mEOFReached;
44 
45  // Actual data reading
46  const TData* begin = &mInterleavedData[0];
47  const TData* end = begin + mInterleavedData.size();
48  for (const TData* i = begin + channel; i < end; i+=mChannels, buffer++ )
49  *buffer = *i;
50 
51  return mEOFReached;
52 
53  }
54  bool Stream::ReadData(TData** buffers, unsigned nFrames )
55  {
56  AdjustInterleavedBuffer( nFrames );
58  if ( mFramesLastRead == 0 ) return mEOFReached;
59 
60  const TData * interleaved = &mInterleavedData[0];
61  for (unsigned iFrame=0; iFrame<nFrames; iFrame++)
62  for (unsigned iChannel=0; iChannel<mChannels; iChannel++)
63  buffers[iChannel][iFrame] = *interleaved++;
64 
65  return mEOFReached;
66  }
67 
68  bool Stream::ReadData( int* channels, int nchannels, TData** buffers, unsigned nFrames )
69  {
70  AdjustInterleavedBuffer( nFrames );
72  if ( mFramesLastRead == 0 ) return mEOFReached;
73 
74  // Actual data reading
75  const TData* begin = &mInterleavedData[0];
76  const TData* end = begin + mInterleavedData.size();
77  const int* endChannels = channels + nchannels;
78 
79  for( int* currentChannel = channels;
80  currentChannel != endChannels;
81  currentChannel++ )
82  {
83  const int channelIndex = *currentChannel;
84  TData* pSamples = buffers[channelIndex];
85  for ( const TData* i = begin + channelIndex; i<end; i+=mChannels)
86  {
87  *pSamples++ = *i;
88  }
89  }
90  return mEOFReached;
91  }
92 
93  void Stream::AdjustInterleavedBuffer( unsigned nFrames )
94  {
95  unsigned newSize = nFrames * mChannels;
96  if (newSize == mInterleavedData.size()) return;
97  mInterleavedData.resize( nFrames * mChannels );
98  }
99 
100  void Stream::WriteData( int channel, const TData* buffer, unsigned nFrames )
101  {
102  AdjustInterleavedBuffer( nFrames );
103  TData* beginData = &mInterleavedData[0];
104  TData* endData = beginData + mInterleavedData.size();
105  for (TData* data = beginData+channel; data < endData; data += mChannels)
106  *data = *buffer++;
107 
109  }
110 
111  void Stream::WriteData(TData ** const buffers, unsigned nFrames)
112  {
113  AdjustInterleavedBuffer( nFrames );
114 
115  TData * interleaved = &mInterleavedData[0];
116  for (unsigned iFrame=0; iFrame<nFrames; iFrame++)
117  for (unsigned iChannel=0; iChannel<mChannels; iChannel++)
118  *interleaved++ = buffers[iChannel][iFrame];
119 
121  }
122 
123  void Stream::WriteData( int* channels, int nchannels,
124  TData** const samples, unsigned nFrames )
125  {
126  AdjustInterleavedBuffer( nFrames );
127 
128  TData* begin = &mInterleavedData[0];
129  TData* end = begin + mInterleavedData.size();
130  const int* endChannels = channels + nchannels;
131 
132  for( int* currentChannel = channels;
133  currentChannel != endChannels;
134  currentChannel++ )
135  {
136  const int channelIndex = *currentChannel;
137  const TData* pSamples = *(samples + channelIndex);
138  for ( TData* i = begin + channelIndex; i<end; i+=mChannels, pSamples++ )
139  {
140  *i = *pSamples;
141  }
142  }
143 
145  }
146 
147  void Stream::SetChannels( unsigned nChannels )
148  {
149  mChannels = nChannels;
150  }
151 
152 }
153 
154 }
155