audio_sample.h

00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2003
00004  *     Brian Gerkey
00005  *
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 
00023 // Sample type
00024 typedef uint8_t SampleType;
00025 // No type - no wave data loaded
00026 const SampleType SAMPLE_TYPE_NONE       = 0;
00027 // File samples must be opened, and the data comes from/goes to the file on demand
00028 const SampleType SAMPLE_TYPE_FILE       = 1;
00029 // Memory samples are stored as data in memory, eg a sample received via the player server
00030 const SampleType SAMPLE_TYPE_MEM        = 2;
00031 
00032 class AudioSample
00033 {
00034         public:
00035                 AudioSample (void);
00036                 AudioSample (const player_audio_wav_t *source);
00037                 AudioSample (const uint8_t *source, uint32_t length, uint16_t channels, uint32_t sr, uint16_t bps);
00038                 ~AudioSample (void);
00039 
00040                 // Data management functions
00041                 void SetDataPosition (uint32_t newPosition);    // Set current position in the data (in frames, not bytes)
00042                 uint32_t GetDataPosition (void) const;          // Get current position in the data (in frames, not bytes)
00043                 uint32_t GetDataLength (void) const;            // Get length of data (in frames, not bytes)
00044                 int GetData (int frameCount, uint8_t *buffer);  // Get a block of data from current position
00045                 void ClearSample (void);                                        // Clear the entire sample (including format), making this a SAMPLE_TYPE_NONE
00046                 bool FillSilence (uint32_t time);                       // Fill the sample with silence
00047 
00048                 // Data conversion functions
00049                 bool ToPlayer (player_audio_wav_t *dest);                       // Convert to player wave structure
00050                 bool FromPlayer (const player_audio_wav_t *source);     // Convert from player wave structure
00051 
00052                 // File management functions
00053                 bool LoadFile (const char *fileName);   // Load wave data from a file
00054                 void CloseFile (void);                                  // Close the opened file
00055                 const char* GetFilePath (void) const    { return filePath; }
00056 
00057                 // Wave format functions
00058                 SampleType GetType (void) const                 { return type; }
00059                 void SetType (SampleType val)                   { type = val; }
00060                 uint16_t GetNumChannels (void) const    { return numChannels; }
00061                 void SetNumChannels (uint16_t val)              { numChannels = val; }
00062                 uint32_t GetSampleRate (void) const             { return sampleRate; }
00063                 void SetSampleRate (uint32_t val)               { sampleRate = val; byteRate = blockAlign * sampleRate; }
00064                 uint32_t GetByteRate (void) const               { return byteRate; }
00065                 uint16_t GetBlockAlign (void) const             { return blockAlign; }
00066                 void SetBlockAlign (uint16_t val)               { blockAlign = val; byteRate = blockAlign * sampleRate; }
00067                 uint16_t GetBitsPerSample (void) const  { return bitsPerSample; }
00068                 void SetBitsPerSample (uint16_t val)    { bitsPerSample = val; }
00069                 uint32_t GetNumFrames (void) const              { return numFrames; }
00070                 bool SameFormat (const AudioSample *rhs);
00071                 void CopyFormat (const AudioSample *rhs);
00072 
00073                 // Other useful functions
00074                 void PrintWaveInfo (void);      // Print out the wave information
00075 
00076         private:
00077                 SampleType type;                // Sample type
00078 
00079                 // Information about the wave this sample stores
00080                 uint16_t numChannels;   // Number of channels in the data
00081                 uint32_t sampleRate;    // Number of samples per second
00082                 uint32_t byteRate;              // Number of bytes per second
00083                 uint16_t blockAlign;    // Number of bytes for one sample over all channels (i.e. frame size)
00084                 uint16_t bitsPerSample; // Number of bits per sample (eg 8, 16, 24, ...)
00085                 uint32_t numFrames;             // Number of frames (divide by sampleRate to get time)
00086 
00087                 // Current position in the wave data (in bytes)
00088                 uint32_t position;
00089 
00090                 // If this is a file sample, this is the file info
00091                 FILE *waveFile;                 // File pointer
00092                 char *filePath;                 // Path to the file on disc
00093                 uint32_t headerSize;    // Size of the wave format header in the file (i.e. start of actual data in the file)
00094 
00095                 // If this is a memory sample, the data is stored here
00096                 uint32_t dataLength;    // Length of data in bytes
00097                 uint8_t *data;                  // Array on the heap containing the data
00098 };

Last updated 12 September 2005 21:38:45