My Project
Loading...
Searching...
No Matches
CAudioBuffer.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, 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.3.0
41*/
42//==============================================================================
43
44//------------------------------------------------------------------------------
45#ifndef CAudioBufferH
46#define CAudioBufferH
47//------------------------------------------------------------------------------
48#include "math/CMaths.h"
49//------------------------------------------------------------------------------
50
51//------------------------------------------------------------------------------
52namespace chai3d {
53//------------------------------------------------------------------------------
54
55//==============================================================================
62//==============================================================================
63
64//==============================================================================
77//==============================================================================
79{
80 //--------------------------------------------------------------------------
81 // CONSTRUCTOR & DESTRUCTOR:
82 //--------------------------------------------------------------------------
83
84public:
85
88
90 virtual ~cAudioBuffer();
91
92
93 //--------------------------------------------------------------------------
94 // PUBLIC METHODS:
95 //--------------------------------------------------------------------------
96
97public:
98
100 void clear() { cleanup(); }
101
103 bool setup(unsigned char* a_data, const unsigned int a_size, int a_frequency, bool a_stereo, unsigned short a_bitsPerSample);
104
106 bool loadFromFile(const std::string& a_filename);
107
109 std::string getFilename() const { return (m_filename); }
110
112 unsigned int getBuffer() { return (m_buffer); }
113
115 unsigned int getSizeInBytes() { return (m_sizeInBytes); }
116
118 unsigned int getNumSamples() { return m_numSamples; }
119
121 double getDuration() { return (m_duration); }
122
124 double getSamplingRate() { return (m_samplingRate); }
125
127 bool getStereo() { return (m_stereo); }
128
131
133 unsigned char* getData() { return (m_data); }
134
136 short getSampleByTimeL(const double a_time, const bool a_loop);
137
139 short getSampleByTimeR(const double a_time, const bool a_loop);
140
142 short getSampleByIndexL(const unsigned int a_index, const bool a_loop);
143
145 short getSampleByIndexR(const unsigned int a_index, const bool a_loop);
146
148 bool convertToMono();
149
150
151 //--------------------------------------------------------------------------
152 // PROTECTED METHODS:
153 //--------------------------------------------------------------------------
154
155protected:
156
158 bool checkError();
159
161 bool cleanup();
162
165
166
167 //--------------------------------------------------------------------------
168 // PROTECTED MEMBERS:
169 //--------------------------------------------------------------------------
170
171protected:
172
174 std::string m_filename;
175
177 unsigned char* m_data;
178
180 unsigned int m_buffer;
181
183 unsigned int m_sizeInBytes;
184
186 unsigned int m_numSamples;
187
190
193
196
198 unsigned short m_bitsPerSample;
199
202};
203
204//------------------------------------------------------------------------------
205} // namespace chai3d
206//------------------------------------------------------------------------------
207
208//------------------------------------------------------------------------------
209#endif
210//------------------------------------------------------------------------------
Implements general math utility functions.
This class implements an audio buffer.
Definition CAudioBuffer.h:79
double m_samplingRate
Sampling rate of the audio signal.
Definition CAudioBuffer.h:192
int getBitsPerSample()
This method returns the sample format of the audio data.
Definition CAudioBuffer.h:130
bool m_flagDeleteData
Audio data ownership flag.
Definition CAudioBuffer.h:201
std::string getFilename() const
This method returns the filename from which this audio data was most recently loaded.
Definition CAudioBuffer.h:109
unsigned char * getData()
This method returns a pointer to the audio data.
Definition CAudioBuffer.h:133
short getSampleByTimeR(const double a_time, const bool a_loop)
This method returns a sample from the right channel by passing time as argument.
Definition CAudioBuffer.cpp:480
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:124
void clear()
This method clears the audio buffer.
Definition CAudioBuffer.h:100
double getSamplingRate()
This method returns the sampling rate of the audio data.
Definition CAudioBuffer.h:124
bool getStereo()
This method returns true if the audio data in in stereo format, otherwise false.
Definition CAudioBuffer.h:127
short getSampleByIndexR(const unsigned int a_index, const bool a_loop)
This method returns the current sample on the right channel.
Definition CAudioBuffer.cpp:575
bool cleanup()
This method clears all memory.
Definition CAudioBuffer.cpp:372
void computeNumSamplesAndDuration()
This method computes the number of samples and duration stored in the audio buffer.
Definition CAudioBuffer.cpp:401
double m_duration
Duration of the audio signal stored in the audio buffer.
Definition CAudioBuffer.h:189
virtual ~cAudioBuffer()
Destructor of cAudioBuffer.
Definition CAudioBuffer.cpp:95
cAudioBuffer()
Constructor of cAudioBuffer.
Definition CAudioBuffer.cpp:65
bool checkError()
This methods checks for any OpenAL errors.
Definition CAudioBuffer.cpp:639
unsigned int m_sizeInBytes
Audio buffer size in bytes.
Definition CAudioBuffer.h:183
double getDuration()
This method returns the duration in seconds of the audio data.
Definition CAudioBuffer.h:121
bool loadFromFile(const std::string &a_filename)
This method loads an audio file by passing the filename as argument.
Definition CAudioBuffer.cpp:201
short getSampleByIndexL(const unsigned int a_index, const bool a_loop)
This method returns the current sample on the left channel.
Definition CAudioBuffer.cpp:504
bool m_stereo
Audio data format (stereo = true, mono = false).
Definition CAudioBuffer.h:195
unsigned int m_buffer
OpenAL buffer ID.
Definition CAudioBuffer.h:180
unsigned int getSizeInBytes()
This method returns the size in bytes of the audio data.
Definition CAudioBuffer.h:115
short getSampleByTimeL(const double a_time, const bool a_loop)
This method returns a sample from the left channel by passing time as argument.
Definition CAudioBuffer.cpp:456
unsigned char * m_data
Audio data.
Definition CAudioBuffer.h:177
bool convertToMono()
This method converts a stereo stream to mono.
Definition CAudioBuffer.cpp:304
std::string m_filename
Audio filename.
Definition CAudioBuffer.h:174
unsigned short m_bitsPerSample
Audio data resolution. Number of bits per sample (8 or 16).
Definition CAudioBuffer.h:198
unsigned int m_numSamples
Number of audio samples in audio buffer.
Definition CAudioBuffer.h:186
unsigned int getNumSamples()
This method returns the number of samples that compose the audio data.
Definition CAudioBuffer.h:118
unsigned int getBuffer()
This method returns the OpenAL buffer ID.
Definition CAudioBuffer.h:112
Definition CAudioBuffer.cpp:57