audio_sample.h
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2003
4  * Brian Gerkey
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 
23 // Sample type
24 typedef uint8_t SampleType;
25 // No type - no wave data loaded
26 const SampleType SAMPLE_TYPE_NONE = 0;
27 // File samples must be opened, and the data comes from/goes to the file on demand
28 const SampleType SAMPLE_TYPE_FILE = 1;
29 // Memory samples are stored as data in memory, eg a sample received via the player server
30 const SampleType SAMPLE_TYPE_MEM = 2;
31 
33 {
34  public:
35  AudioSample (void);
36  AudioSample (const player_audio_wav_t *source);
37  AudioSample (const uint8_t *source, uint32_t length, uint16_t channels, uint32_t sr, uint16_t bps);
38  ~AudioSample (void);
39 
40  // Data management functions
41  void SetDataPosition (uint32_t newPosition); // Set current position in the data (in frames, not bytes)
42  uint32_t GetDataPosition (void) const; // Get current position in the data (in frames, not bytes)
43  uint32_t GetDataLength (void) const; // Get length of data (in frames, not bytes)
44  int GetData (int frameCount, uint8_t *buffer); // Get a block of data from current position
45  void ClearSample (void); // Clear the entire sample (including format), making this a SAMPLE_TYPE_NONE
46  bool FillSilence (uint32_t time); // Fill the sample with silence
47 
48  // Data conversion functions
49  bool ToPlayer (player_audio_wav_t *dest); // Convert to player wave structure
50  bool FromPlayer (const player_audio_wav_t *source); // Convert from player wave structure
51 
52  // File management functions
53  bool LoadFile (const char *fileName); // Load wave data from a file
54  void CloseFile (void); // Close the opened file
55  const char* GetFilePath (void) const { return filePath; }
56 
57  // Wave format functions
58  SampleType GetType (void) const { return type; }
59  void SetType (SampleType val) { type = val; }
60  uint16_t GetNumChannels (void) const { return numChannels; }
61  void SetNumChannels (uint16_t val) { numChannels = val; }
62  uint32_t GetSampleRate (void) const { return sampleRate; }
63  void SetSampleRate (uint32_t val) { sampleRate = val; byteRate = blockAlign * sampleRate; }
64  uint32_t GetByteRate (void) const { return byteRate; }
65  uint16_t GetBlockAlign (void) const { return blockAlign; }
66  void SetBlockAlign (uint16_t val) { blockAlign = val; byteRate = blockAlign * sampleRate; }
67  uint16_t GetBitsPerSample (void) const { return bitsPerSample; }
68  void SetBitsPerSample (uint16_t val) { bitsPerSample = val; }
69  uint32_t GetNumFrames (void) const { return numFrames; }
70  bool SameFormat (const AudioSample *rhs);
71  void CopyFormat (const AudioSample *rhs);
72 
73  // Other useful functions
74  void PrintWaveInfo (void); // Print out the wave information
75 
76  private:
77  SampleType type; // Sample type
78 
79  // Information about the wave this sample stores
80  uint16_t numChannels; // Number of channels in the data
81  uint32_t sampleRate; // Number of samples per second
82  uint32_t byteRate; // Number of bytes per second
83  uint16_t blockAlign; // Number of bytes for one sample over all channels (i.e. frame size)
84  uint16_t bitsPerSample; // Number of bits per sample (eg 8, 16, 24, ...)
85  uint32_t numFrames; // Number of frames (divide by sampleRate to get time)
86 
87  // Current position in the wave data (in bytes)
88  uint32_t position;
89 
90  // If this is a file sample, this is the file info
91  FILE *waveFile; // File pointer
92  char *filePath; // Path to the file on disc
93  uint32_t headerSize; // Size of the wave format header in the file (i.e. start of actual data in the file)
94 
95  // If this is a memory sample, the data is stored here
96  uint32_t dataLength; // Length of data in bytes
97  uint8_t *data; // Array on the heap containing the data
98 };
Data: Raw audio data.
Definition: player_interfaces.h:1463
Definition: audio_sample.h:32