Music.cpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
26// Headers
28#include <SFML/Audio/Music.hpp>
29#include <SFML/Audio/OpenAL.hpp>
30#include <SFML/Audio/SoundFile.hpp>
31#include <fstream>
32#include <iostream>
33
34
35namespace sf
36{
40Music::Music(std::size_t BufferSize) :
41myFile (NULL),
42myDuration(0.f),
43mySamples (BufferSize)
44{
45
46}
47
48
53{
54 // We must stop before destroying the file :)
55 Stop();
56
57 delete myFile;
58}
59
60
64bool Music::OpenFromFile(const std::string& Filename)
65{
66 // First stop the music if it was already running
67 Stop();
68
69 // Create the sound file implementation, and open it in read mode
70 delete myFile;
71 myFile = priv::SoundFile::CreateRead(Filename);
72 if (!myFile)
73 {
74 std::cerr << "Failed to open \"" << Filename << "\" for reading" << std::endl;
75 return false;
76 }
77
78 // Compute the duration
79 myDuration = static_cast<float>(myFile->GetSamplesCount()) / myFile->GetSampleRate() / myFile->GetChannelsCount();
80
81 // Initialize the stream
82 Initialize(myFile->GetChannelsCount(), myFile->GetSampleRate());
83
84 return true;
85}
86
87
91bool Music::OpenFromMemory(const char* Data, std::size_t SizeInBytes)
92{
93 // First stop the music if it was already running
94 Stop();
95
96 // Create the sound file implementation, and open it in read mode
97 delete myFile;
98 myFile = priv::SoundFile::CreateRead(Data, SizeInBytes);
99 if (!myFile)
100 {
101 std::cerr << "Failed to open music from memory for reading" << std::endl;
102 return false;
103 }
104
105 // Compute the duration
106 myDuration = static_cast<float>(myFile->GetSamplesCount()) / myFile->GetSampleRate();
107
108 // Initialize the stream
109 Initialize(myFile->GetChannelsCount(), myFile->GetSampleRate());
110
111 return true;
112}
113
114
118bool Music::OnStart()
119{
120 return myFile && myFile->Restart();
121}
122
123
127bool Music::OnGetData(SoundStream::Chunk& Data)
128{
129 if (myFile)
130 {
131 // Fill the chunk parameters
132 Data.Samples = &mySamples[0];
133 Data.NbSamples = myFile->Read(&mySamples[0], mySamples.size());
134
135 // Check if we have reached the end of the audio file
136 return Data.NbSamples == mySamples.size();
137 }
138 else
139 {
140 return false;
141 }
142}
143
144
149{
150 return myDuration;
151}
152
153} // namespace sf
bool OpenFromMemory(const char *Data, std::size_t SizeInBytes)
Open a music file from memory (doesn't play it – call Play() for that).
Definition Music.cpp:91
Music(std::size_t BufferSize=44100)
Construct the music with a buffer size.
Definition Music.cpp:40
bool OpenFromFile(const std::string &Filename)
Open a music file (doesn't play it – call Play() for that).
Definition Music.cpp:64
~Music()
Destructor.
Definition Music.cpp:52
float GetDuration() const
Get the music duration.
Definition Music.cpp:148
void Stop()
Stop playing the audio stream.
void Initialize(unsigned int ChannelsCount, unsigned int SampleRate)
Set the audio stream parameters, you must call it before Play().