SoundFile.cpp
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 #include "nomlib/audio/AL/SoundFile.hpp"
30 
31 namespace nom {
32  namespace OpenAL {
33 
34 SoundFile::SoundFile ( void )
35 {
36 NOM_LOG_TRACE ( NOM );
37 
38  this->fp.reset();
39 
40  // Initialize here
41 }
42 
43 SoundFile::~SoundFile ( void )
44 {
45 NOM_LOG_TRACE ( NOM );
46  // Clean up instance variables
47 }
48 
49 size_t SoundFile::getSampleCount ( void ) const
50 {
51  return this->sample_count;
52 }
53 
54 uint32 SoundFile::getChannelCount ( void ) const
55 {
56  return this->channel_count;
57 }
58 
59 uint32 SoundFile::getSampleRate ( void ) const
60 {
61  return this->sample_rate;
62 }
63 
64 uint32 SoundFile::getChannelFormat ( void ) const
65 {
66  return this->channel_format;
67 }
68 
69 size_t SoundFile::getDataByteSize ( void ) const
70 {
71  return this->getSampleCount() * sizeof ( int16 );
72 }
73 
74 bool SoundFile::open ( const std::string& filename )
75 {
76  SF_INFO info;
77 
78  this->fp = std::shared_ptr<SNDFILE> ( sf_open ( filename.c_str(), SFM_READ, &info ), sf_close );
79 
80  if ( this->fp.get() == nullptr )
81  {
82 NOM_LOG_ERR ( NOM, "Could not not audio file: " + filename );
83  return false;
84  }
85 
86  this->channel_count = info.channels;
87  // sample_count should be the same size as samples
88  this->sample_count = info.frames * info.channels;
89  this->sample_rate = info.samplerate;
90 
91  switch ( info.channels )
92  {
93  default: this->channel_format = 0; break;
94  case 1: this->channel_format = alGetEnumValue ( "AL_FORMAT_MONO16" ); break;
95  case 2: this->channel_format = alGetEnumValue ( "AL_FORMAT_STEREO16" ); break;
96  case 4: this->channel_format = alGetEnumValue ( "AL_FORMAT_QUAD16" ); break;
97  case 6: this->channel_format = alGetEnumValue ( "AL_FORMAT_51CHN16" ); break;
98  case 7: this->channel_format = alGetEnumValue ( "AL_FORMAT_61CHN16" ); break;
99  case 8: this->channel_format = alGetEnumValue ( "AL_FORMAT_71CHN16" ); break;
100  }
101 
102  return true;
103 }
104 
105 bool SoundFile::read ( std::vector<int16>& data )
106 {
107  size_t read_size = 0;
108  std::vector<int16> read_buffer;
109  read_buffer.resize ( BUFFER_SIZE );
110 
111  while ( ( read_size = sf_read_short ( this->fp.get(), read_buffer.data(), read_buffer.size() ) ) != 0 )
112  {
113  data.insert ( data.end(), read_buffer.begin(), read_buffer.begin() + read_size);
114  }
115 
116  return true;
117 }
118 
119 
120  } // namespace OpenAL
121 } // namespace nom
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