CAudioBuffer.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Francois Conti
40  \version 3.2.0 $Rev: 2181 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CAudioBufferH
46 #define CAudioBufferH
47 //------------------------------------------------------------------------------
48 #include "math/CMaths.h"
49 //------------------------------------------------------------------------------
50 
51 //------------------------------------------------------------------------------
52 namespace chai3d {
53 //------------------------------------------------------------------------------
54 
55 //==============================================================================
62 //==============================================================================
63 
64 //==============================================================================
77 //==============================================================================
79 {
80  //--------------------------------------------------------------------------
81  // CONSTRUCTOR & DESTRUCTOR:
82  //--------------------------------------------------------------------------
83 
84 public:
85 
87  cAudioBuffer();
88 
90  virtual ~cAudioBuffer();
91 
92 
93  //--------------------------------------------------------------------------
94  // PUBLIC METHODS:
95  //--------------------------------------------------------------------------
96 
97 public:
98 
100  bool setup(unsigned char* a_data, const unsigned int a_size, int a_frequency, bool a_stereo, unsigned short a_bitsPerSample);
101 
103  bool loadFromFile(const std::string& a_filename);
104 
106  std::string getFilename() const { return (m_filename); }
107 
109  unsigned int getBuffer() { return (m_buffer); }
110 
112  int getSize() { return (m_size); }
113 
115  int getNumSamples();
116 
118  int getFrequency() { return (m_frequency); }
119 
121  bool getStereo() { return (m_stereo); }
122 
124  int getBitsPerSample() { return (m_bitsPerSample); }
125 
127  unsigned char* getData() { return (m_data); }
128 
130  short getSampleLeft(const double a_time, const bool a_loop);
131 
133  short getSampleRight(const double a_time, const bool a_loop);
134 
136  bool convertToMono();
137 
138 
139  //--------------------------------------------------------------------------
140  // PROTECTED METHODS:
141  //--------------------------------------------------------------------------
142 
143 protected:
144 
146  bool checkError();
147 
149  bool cleanup();
150 
151 
152  //--------------------------------------------------------------------------
153  // PROTECTED MEMBERS:
154  //--------------------------------------------------------------------------
155 
156 protected:
157 
159  std::string m_filename;
160 
162  unsigned char* m_data;
163 
165  unsigned int m_buffer;
166 
168  int m_size;
169 
172 
174  bool m_stereo;
175 
177  unsigned short m_bitsPerSample;
178 
181 };
182 
183 //------------------------------------------------------------------------------
184 } // namespace chai3d
185 //------------------------------------------------------------------------------
186 
187 //------------------------------------------------------------------------------
188 #endif
189 //------------------------------------------------------------------------------
Implements general math utility functions.
bool m_stereo
Audio data format (stereo = true, mono = false).
Definition: CAudioBuffer.h:174
bool checkError()
This methods checks for any OpenAL errors.
Definition: CAudioBuffer.cpp:545
virtual ~cAudioBuffer()
Destructor of cAudioBuffer.
Definition: CAudioBuffer.cpp:90
short getSampleRight(const double a_time, const bool a_loop)
This method returns the current sample on the right channel.
Definition: CAudioBuffer.cpp:480
std::string getFilename() const
This method returns the filename from which this audio data was most recently loaded.
Definition: CAudioBuffer.h:106
int m_frequency
Audio data frequency.
Definition: CAudioBuffer.h:171
int m_size
Audio buffer size in bytes.
Definition: CAudioBuffer.h:168
This class implements an audio buffer.
Definition: CAudioBuffer.h:78
int getFrequency()
This method returns the sampling frequency of the audio data.
Definition: CAudioBuffer.h:118
int getSize()
This method returns the size in bytes of the audio data.
Definition: CAudioBuffer.h:112
bool loadFromFile(const std::string &a_filename)
This method loads an audio file by passing the filename as argument.
Definition: CAudioBuffer.cpp:187
bool getStereo()
This method returns true if the audio data in in stereo format, otherwise false.
Definition: CAudioBuffer.h:121
cAudioBuffer()
Constructor of cAudioBuffer.
Definition: CAudioBuffer.cpp:64
unsigned int m_buffer
OpenAL buffer ID.
Definition: CAudioBuffer.h:165
bool setup(unsigned char *a_data, const unsigned int a_size, int a_frequency, bool a_stereo, unsigned short a_bitsPerSample)
This method sets a buffer by passing a pointer to the audio data and defines the data specifications...
Definition: CAudioBuffer.cpp:117
unsigned char * getData()
This method returns a pointer to the audio data.
Definition: CAudioBuffer.h:127
unsigned int getBuffer()
This method returns the OpenAL buffer ID.
Definition: CAudioBuffer.h:109
unsigned short m_bitsPerSample
Audio data resolution. Number of bits per sample (8 or 16).
Definition: CAudioBuffer.h:177
unsigned char * m_data
Audio data.
Definition: CAudioBuffer.h:162
bool m_flagDeleteData
Audio data ownership flag.
Definition: CAudioBuffer.h:180
Definition: CAudioBuffer.cpp:56
bool cleanup()
This method clears all memory.
Definition: CAudioBuffer.cpp:338
bool convertToMono()
This method converts a stereo stream to mono.
Definition: CAudioBuffer.cpp:273
short getSampleLeft(const double a_time, const bool a_loop)
This method returns the current sample on the left channel.
Definition: CAudioBuffer.cpp:410
int getBitsPerSample()
This method returns the sample format of the audio data.
Definition: CAudioBuffer.h:124
int getNumSamples()
This method returns the number of samples that compose the audio data.
Definition: CAudioBuffer.cpp:367
std::string m_filename
Audio filename.
Definition: CAudioBuffer.h:159