28 #include <SFML/Audio/SoundBuffer.hpp> 29 #include <SFML/Audio/SoundFile.hpp> 30 #include <SFML/Audio/Sound.hpp> 31 #include <SFML/Audio/AudioDevice.hpp> 32 #include <SFML/Audio/OpenAL.hpp> 47 ALCheck(alGenBuffers(1, &myBuffer));
58 mySamples (Copy.mySamples),
59 myDuration (Copy.myDuration),
63 ALCheck(alGenBuffers(1, &myBuffer));
76 for (SoundList::const_iterator it = mySounds.begin(); it != mySounds.end(); ++it)
81 ALCheck(alDeleteBuffers(1, &myBuffer));
91 std::auto_ptr<priv::SoundFile> File(priv::SoundFile::CreateRead(Filename));
97 std::size_t NbSamples = File->GetSamplesCount();
98 unsigned int ChannelsCount = File->GetChannelsCount();
99 unsigned int SampleRate = File->GetSampleRate();
102 mySamples.resize(NbSamples);
103 if (File->Read(&mySamples[0], NbSamples) == NbSamples)
106 return Update(ChannelsCount, SampleRate);
111 std::cerr <<
"Failed to read audio data from file \"" << Filename <<
"\"" << std::endl;
119 std::cerr <<
"Failed to load sound buffer from file \"" << Filename <<
"\"" << std::endl;
132 std::auto_ptr<priv::SoundFile> File(priv::SoundFile::CreateRead(Data, SizeInBytes));
138 std::size_t NbSamples = File->GetSamplesCount();
139 unsigned int ChannelsCount = File->GetChannelsCount();
140 unsigned int SampleRate = File->GetSampleRate();
143 mySamples.resize(NbSamples);
144 if (File->Read(&mySamples[0], NbSamples) == NbSamples)
147 return Update(ChannelsCount, SampleRate);
152 std::cerr <<
"Failed to read audio data from file in memory" << std::endl;
160 std::cerr <<
"Failed to load sound buffer from file in memory" << std::endl;
173 if (Samples && SamplesCount && ChannelsCount && SampleRate)
176 mySamples.assign(Samples, Samples + SamplesCount);
179 return Update(ChannelsCount, SampleRate);
184 std::cerr <<
"Failed to load sound buffer from memory (" 185 <<
"Samples : " << Samples <<
", " 186 <<
"Samples count : " << SamplesCount <<
", " 187 <<
"Channels count : " << ChannelsCount <<
", " 188 <<
"Sample rate : " << SampleRate <<
")" 206 File->Write(&mySamples[0], mySamples.size());
213 std::cerr <<
"Failed to save sound buffer to file \"" << Filename <<
"\"" << std::endl;
225 return mySamples.empty() ? NULL : &mySamples[0];
234 return mySamples.size();
244 ALCheck(alGetBufferi(myBuffer, AL_FREQUENCY, &SampleRate));
256 ALCheck(alGetBufferi(myBuffer, AL_CHANNELS, &ChannelsCount));
258 return ChannelsCount;
278 std::swap(mySamples, Temp.mySamples);
279 std::swap(myBuffer, Temp.myBuffer);
280 std::swap(myDuration, Temp.myDuration);
281 std::swap(mySounds, Temp.mySounds);
290 bool SoundBuffer::Update(
unsigned int ChannelsCount,
unsigned int SampleRate)
293 if (!SampleRate || !ChannelsCount || mySamples.empty())
297 ALenum Format = priv::AudioDevice::GetInstance().GetFormatFromChannelsCount(ChannelsCount);
302 std::cerr <<
"Unsupported number of channels (" << ChannelsCount <<
")" << std::endl;
307 ALsizei Size =
static_cast<ALsizei
>(mySamples.size()) *
sizeof(Int16);
308 ALCheck(alBufferData(myBuffer, Format, &mySamples[0], Size, SampleRate));
311 myDuration =
static_cast<float>(mySamples.size()) / SampleRate / ChannelsCount;
320 void SoundBuffer::AttachSound(Sound* Instance)
const 322 mySounds.insert(Instance);
329 void SoundBuffer::DetachSound(Sound* Instance)
const 331 mySounds.erase(Instance);
unsigned int GetChannelsCount() const
Return the number of channels (1 = mono, 2 = stereo, ...)
Base class for every resource that needs to notify dependent classes about its destruction.
bool SaveToFile(const std::string &Filename) const
Save the sound buffer to a file.
const Int16 * GetSamples() const
Return the sound samples.
bool LoadFromMemory(const char *Data, std::size_t SizeInBytes)
Load the sound buffer from a file in memory.
SoundBuffer & operator=(const SoundBuffer &Other)
Assignment operator.
unsigned int GetSampleRate() const
Get the sample rate.
float GetDuration() const
Get the sound duration.
~SoundBuffer()
Destructor.
SoundBuffer()
Default constructor.
std::size_t GetSamplesCount() const
Return the samples count.
bool LoadFromSamples(const Int16 *Samples, std::size_t SamplesCount, unsigned int ChannelsCount, unsigned int SampleRate)
Load the sound buffer from an array of samples - assumed format for samples is 16 bits signed integer...
bool LoadFromFile(const std::string &Filename)
Load the sound buffer from a file.
SoundBuffer is the low-level for loading and manipulating sound buffers.
Abstract base class for every class that owns a device-dependant resource – allow them to initialize...