00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef SH_AUDIODATA_H
00022 #define SH_AUDIODATA_H
00023
00024 #include <vector>
00025
00026 #include "AudioSampleValue.h"
00027 class BinaryIO ;
00028 #include "CvrStgObject.h"
00029
00037 class AudioData : public CvrStgObject {
00038 public:
00040 static const UWORD32 NoLimit = 0 ;
00041
00042 virtual void read (BinaryIO* io, UWORD32 n = NoLimit) = 0 ;
00043 virtual void write (BinaryIO* io, UWORD32 n = NoLimit) = 0 ;
00044 } ;
00045
00050 template<AUDIOSAMPLETYPE Type, class ValueType, class SampleValueType = AudioSampleValue<Type,ValueType> >
00051 class AudioDataImpl : public AudioData {
00052 public:
00053 AudioDataImpl (CvrStgFile* f) : TheCvrStgFile(f) {} ;
00054 virtual ~AudioDataImpl (void) {} ;
00055
00056 void read (BinaryIO* io, UWORD32 n = AudioData::NoLimit) ;
00057 void write (BinaryIO* io, UWORD32 n = AudioData::NoLimit) ;
00058
00059 unsigned long getNumSamples (void) const ;
00060 SampleValue* getSampleValue (const SamplePos pos) const ;
00061 void replaceSample (const SamplePos pos, const SampleValue* s) ;
00062
00063 private:
00064 std::vector<ValueType> Data ;
00065 CvrStgFile* TheCvrStgFile ;
00066
00067 ValueType readValue (BinaryIO* io) const ;
00068 void writeValue (BinaryIO* io, ValueType v) const ;
00069 } ;
00070
00071 #include "error.h"
00072
00073 template<AUDIOSAMPLETYPE Type, class ValueType, class SampleValueType>
00074 void AudioDataImpl<Type,ValueType,SampleValueType>::read (BinaryIO* io, UWORD32 n)
00075 {
00076 try {
00077 if (n == NoLimit) {
00078 Data.clear() ;
00079 while (!io->eof()) {
00080 Data.push_back (readValue(io)) ;
00081 }
00082 }
00083 else {
00084 Data.resize (n) ;
00085 for (UWORD32 i = 0 ; i < n ; i++) {
00086 Data[i] = readValue(io) ;
00087 }
00088 }
00089 }
00090 catch (BinaryInputError e) {
00091 switch (e.getType()) {
00092 case BinaryInputError::FILE_ERR:
00093 {
00094 throw SteghideError (_("an error occured while reading the audio data from the file \"%s\"."), io->getName().c_str()) ;
00095 break ;
00096 }
00097
00098 case BinaryInputError::FILE_EOF:
00099 {
00100 throw SteghideError (_("premature end of file \"%s\" while reading audio data."), io->getName().c_str()) ;
00101 break ;
00102 }
00103
00104 case BinaryInputError::STDIN_ERR:
00105 {
00106 throw SteghideError (_("an error occured while reading the audio data from standard input.")) ;
00107 break ;
00108 }
00109
00110 case BinaryInputError::STDIN_EOF:
00111 {
00112 throw SteghideError (_("premature end of data from standard input while reading audio data.")) ;
00113 break ;
00114 }
00115 }
00116 }
00117 }
00118
00119 template<AUDIOSAMPLETYPE Type, class ValueType, class SampleValueType>
00120 void AudioDataImpl<Type,ValueType,SampleValueType>::write (BinaryIO* io, UWORD32 n)
00121 {
00122 try {
00123 if (n == NoLimit) {
00124 n = Data.size() ;
00125 }
00126 for (UWORD32 i = 0 ; i < n ; i++) {
00127 writeValue (io, Data[i]) ;
00128 }
00129 }
00130 catch (BinaryOutputError e) {
00131 switch (e.getType()) {
00132 case BinaryOutputError::FILE_ERR:
00133 {
00134 throw SteghideError (_("an error occured while writing the audio data to the file \"%s\"."), io->getName().c_str()) ;
00135 break ;
00136 }
00137
00138 case BinaryOutputError::STDOUT_ERR:
00139 {
00140 throw SteghideError (_("an error occured while writing the audio data to standard output.")) ;
00141 break ;
00142 }
00143 }
00144 }
00145 }
00146
00147 template<AUDIOSAMPLETYPE Type, class ValueType, class SampleValueType>
00148 unsigned long AudioDataImpl<Type,ValueType,SampleValueType>::getNumSamples (void) const
00149 {
00150 return Data.size() ;
00151 }
00152
00153 template<AUDIOSAMPLETYPE Type, class ValueType, class SampleValueType>
00154 SampleValue* AudioDataImpl<Type,ValueType,SampleValueType>::getSampleValue (const SamplePos pos) const
00155 {
00156 myassert (pos < Data.size()) ;
00157 return ((SampleValue*) new SampleValueType (Data[pos])) ;
00158 }
00159
00160 template<AUDIOSAMPLETYPE Type, class ValueType, class SampleValueType>
00161 void AudioDataImpl<Type,ValueType,SampleValueType>::replaceSample (const SamplePos pos, const SampleValue* s)
00162 {
00163 const SampleValueType* sample = dynamic_cast<const SampleValueType*> (s) ;
00164 myassert (sample) ;
00165 myassert (pos < Data.size()) ;
00166 Data[pos] = sample->getValue() ;
00167 }
00168
00169 #endif // ndef SH_AUDIODATA_H