SoundFile.hpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #ifndef NOMLIB_AL_SOUNDFILE_HEADERS
30 #define NOMLIB_AL_SOUNDFILE_HEADERS
31 
32 #include <iostream>
33 #include <string>
34 #include <memory>
35 #include <vector>
36 
37 #include <sndfile.h>
38 
39 #include "nomlib/config.hpp"
40 #include "nomlib/audio/AL/OpenAL.hpp"
41 
42 namespace nom {
43  namespace OpenAL {
44 
45 class SoundFile
46 {
47  public:
48  SoundFile ( void );
49  ~SoundFile ( void );
50 
52  size_t getSampleCount ( void ) const;
53 
55  uint32 getChannelCount ( void ) const;
56 
58  uint32 getSampleRate ( void ) const;
59 
61  uint32 getChannelFormat ( void ) const;
62 
64  size_t getDataByteSize ( void ) const;
65 
66  bool open ( const std::string& filename );
67  bool read ( std::vector<int16>& data );
68 
69  private:
71  std::shared_ptr<SNDFILE> fp;
73  std::vector<int16> samples;
75  std::size_t sample_count;
77  uint32 channel_count;
79  uint32 sample_rate;
82 };
83 
84 
85  } // namespace OpenAL
86 } // namespace nom
87 
88 #endif // NOMLIB_AL_SOUNDFILE_HEADERS defined
uint32 getChannelCount(void) const
Obtain number of channels used by audio file.
Definition: SoundFile.cpp:54
uint32 getChannelFormat(void) const
Obtain channel format; used internally by OpenAL.
Definition: SoundFile.cpp:64
uint32 getSampleRate(void) const
Obtain audio sampling rate; this is the number of samples per second.
Definition: SoundFile.cpp:59
uint32 channel_count
Number of audio channels used by sound.
Definition: SoundFile.hpp:77
uint32 sample_rate
Number of samples per second.
Definition: SoundFile.hpp:79
std::shared_ptr< SNDFILE > fp
SNDFILE* file descriptor.
Definition: SoundFile.hpp:71
size_t getDataByteSize(void) const
Obtain audio data size in bytes.
Definition: SoundFile.cpp:69
size_t getSampleCount(void) const
Obtain number of samples in audio file.
Definition: SoundFile.cpp:49
int32 channel_format
OpenAL compatible audio channels used by sound.
Definition: SoundFile.hpp:81
std::size_t sample_count
Total number of samples in the file.
Definition: SoundFile.hpp:75
std::vector< int16 > samples
Extracted audio stream from file.
Definition: SoundFile.hpp:73