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 };
#define PLAYER_WARN1(msg, a)
Definition: error.h:90
Definition: pf_vector.h:42
static bool MatchMessage(player_msghdr_t *hdr, int type, int subtype, player_devaddr_t addr)
Helper for message processing.
Definition: message.h:159
double ReadFloat(int section, const char *name, double value)
Read a floating point (double) value.
An angle in 3D space.
Definition: player.h:207
Generic message header.
Definition: player.h:162
virtual int MainSetup(void)
Sets up the resources needed by the driver thread.
Definition: driver.h:658
virtual void MainQuit(void)
Cleanup method for driver thread (called when main exits)
Definition: driver.h:664
uint8_t type
Message type; must be one of PLAYER_MSGTYPE_*.
Definition: player.h:166
const char * ReadString(int section, const char *name, const char *value)
Read a string value.
A point in 3D space.
Definition: player.h:195
virtual void Main(void)=0
Main method for driver thread.
double ReadAngle(int section, const char *name, double value)
Read an angle (includes unit conversion).
const char * ReadTupleString(int section, const char *name, int index, const char *value)
Read a string from a tuple field.
#define PLAYER_MAX_MESSAGE_SIZE
The largest possible message.
Definition: player.h:68
int ReadInt(int section, const char *name, int value)
Read an integer value.
#define PLAYER_MSGTYPE_DATA
A data message.
Definition: player.h:95
#define PLAYER_ERROR2(msg, a, b)
Definition: error.h:83
virtual void Update()
Update non-threaded drivers.
Definition: driver.h:423
#define PLAYER_MSGTYPE_RESP_ACK
A positive response message.
Definition: player.h:112
Definition: audio_sample.h:33
virtual int ProcessMessage(QueuePointer &resp_queue, player_msghdr *hdr, void *data)
Message handler.
#define PLAYER_MSGTYPE_REQ
A request message.
Definition: player.h:106
#define PLAYER_MSGTYPE_RESP_NACK
A negative response message.
Definition: player.h:125
Definition: imagebase.h:62
Class for loading configuration file information.
Definition: configfile.h:197
virtual int Setup()
Initialize the driver.
Definition: driver.h:386
An autopointer for the message queue.
Definition: message.h:74
#define PLAYER_ERROR1(msg, a)
Definition: error.h:82
#define PLAYER_ERROR(msg)
Definition: error.h:81
Base class for drivers which oeprate with a thread.
Definition: driver.h:553
uint32_t size
Size in bytes of the payload to follow.
Definition: player.h:174
#define PLAYER_WARN(msg)
Warning message macros.
Definition: error.h:89
#define PLAYER_MSGTYPE_CMD
A command message.
Definition: player.h:99
Definition: localization/amcl/map/map.h:52
virtual int Shutdown()
Finalize the driver.
Definition: driver.h:393
Base class for all drivers.
Definition: driver.h:109
#define PLAYER_MSGQUEUE_DEFAULT_MAXLEN
Default maximum length for a message queue.
Definition: player.h:76