smk::SoundBuffer Class Reference

#include <SoundBuffer.hpp>

A sound file loaded in memory. More...

Description

A sound file loaded in memory.

See also
Sound

Example

// Load a sound file.
auto sound_buffer = smk::SoundBuffer(asset::water_mp3);
// Create a sound source.
auto sound = smk::Sound(sound_buffer);
// Start playing.
sound.Play()
A sound file loaded in memory.
Definition: SoundBuffer.hpp:33
Represent a sound being played.
Definition: Sound.hpp:31
Examples
sound.cpp.

Definition at line 33 of file SoundBuffer.hpp.

Public Member Functions

 SoundBuffer (const std::string &filename)
 Load a sound resource into memory from a file. More...
 
 SoundBuffer (SoundBuffer &&) noexcept
 
 SoundBuffer (const SoundBuffer &)=delete
 
SoundBufferoperator= (SoundBuffer &&) noexcept
 
SoundBufferoperator= (const SoundBuffer &)=delete
 
unsigned int buffer () const
 

Constructor & Destructor Documentation

◆ SoundBuffer() [1/2]

smk::SoundBuffer::SoundBuffer ( const std::string &  filename)

Load a sound resource into memory from a file.

Definition at line 20 of file SoundBuffer.cpp.

20 : SoundBuffer() {
21 if (!Audio::Initialized()) {
22 static bool once = true;
23 if (once) {
24 std::cerr
25 << "Error: smk::Audio has not been initialized. Please create a "
26 "smk::Audio instance in the main() function before creating a "
27 "smk::SoundBuffer"
28 << std::endl;
29 once = false;
30 }
31 }
32
33 nqr::AudioData fileData;
34 nqr::NyquistIO loader;
35 loader.Load(&fileData, filename);
36
37 auto sample_rate = static_cast<ALsizei>(fileData.sampleRate);
38
39 std::vector<ALshort> data;
40 for (auto& it : fileData.samples) {
41 it = std::min(it, +1.f);
42 it = std::max(it, -1.f);
43 data.push_back(ALshort(it * float((1 << 15) - 1))); // NOLINT
44 }
45
46 ALenum format = {};
47 switch (fileData.channelCount) {
48 case 1:
49 format = AL_FORMAT_MONO16;
50 break;
51 case 2:
52 format = AL_FORMAT_STEREO16;
53 break;
54 default:
55 std::cerr << "SoundBuffer: Unsupported format file " + filename
56 << std::endl;
57 return;
58 }
59
60 alGenBuffers(1, &buffer_);
61 alBufferData(buffer_, format, data.data(),
62 ALsizei(data.size() * sizeof(ALshort)), sample_rate);
63
64 if (alGetError() != AL_NO_ERROR) {
65 std::cerr << "SoundBuffer: OpenAL error" << std::endl;
66 return;
67 }
68}
static bool Initialized()
Definition: Audio.cpp:91

◆ ~SoundBuffer()

smk::SoundBuffer::~SoundBuffer ( )

Definition at line 70 of file SoundBuffer.cpp.

70 {
71 if (buffer_) {
72 alDeleteBuffers(1, &buffer_);
73 }
74}

◆ SoundBuffer() [2/2]

smk::SoundBuffer::SoundBuffer ( SoundBuffer &&  o)
noexcept

Definition at line 76 of file SoundBuffer.cpp.

76 {
77 this->operator=(std::move(o));
78}

Member Function Documentation

◆ buffer()

unsigned int smk::SoundBuffer::buffer ( ) const

Definition at line 85 of file SoundBuffer.cpp.

85 {
86 return buffer_;
87}

◆ operator=()

SoundBuffer & smk::SoundBuffer::operator= ( SoundBuffer &&  o)
noexcept

Definition at line 80 of file SoundBuffer.cpp.

80 {
81 std::swap(buffer_, o.buffer_);
82 return *this;
83}

The documentation for this class was generated from the following files: