SoundFile.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/SoundFile.hpp>
29#include <SFML/Audio/SoundFileDefault.hpp>
30#include <SFML/Audio/SoundFileOgg.hpp>
31#include <iostream>
32
33
34namespace sf
35{
36namespace priv
37{
41SoundFile* SoundFile::CreateRead(const std::string& Filename)
42{
43 // Create the file according to its type
44 SoundFile* File = NULL;
45 if (SoundFileOgg::IsFileSupported(Filename, true)) File = new SoundFileOgg;
46 else if (SoundFileDefault::IsFileSupported(Filename, true)) File = new SoundFileDefault;
47
48 // Open it for reading
49 if (File)
50 {
51 std::size_t SamplesCount;
52 unsigned int ChannelsCount;
53 unsigned int SampleRate;
54
55 if (File->OpenRead(Filename, SamplesCount, ChannelsCount, SampleRate))
56 {
57 File->myFilename = Filename;
58 File->myData = NULL;
59 File->mySize = 0;
60 File->myNbSamples = SamplesCount;
61 File->myChannelsCount = ChannelsCount;
62 File->mySampleRate = SampleRate;
63 }
64 else
65 {
66 delete File;
67 File = NULL;
68 }
69 }
70
71 return File;
72}
73
74
78SoundFile* SoundFile::CreateRead(const char* Data, std::size_t SizeInMemory)
79{
80 // Create the file according to its type
81 SoundFile* File = NULL;
82 if (SoundFileOgg::IsFileSupported(Data, SizeInMemory)) File = new SoundFileOgg;
83 else if (SoundFileDefault::IsFileSupported(Data, SizeInMemory)) File = new SoundFileDefault;
84
85 // Open it for reading
86 if (File)
87 {
88 std::size_t SamplesCount;
89 unsigned int ChannelsCount;
90 unsigned int SampleRate;
91
92 if (File->OpenRead(Data, SizeInMemory, SamplesCount, ChannelsCount, SampleRate))
93 {
94 File->myFilename = "";
95 File->myData = Data;
96 File->mySize = SizeInMemory;
97 File->myNbSamples = SamplesCount;
98 File->myChannelsCount = ChannelsCount;
99 File->mySampleRate = SampleRate;
100 }
101 else
102 {
103 delete File;
104 File = NULL;
105 }
106 }
107
108 return File;
109}
110
111
115SoundFile* SoundFile::CreateWrite(const std::string& Filename, unsigned int ChannelsCount, unsigned int SampleRate)
116{
117 // Create the file according to its type
118 SoundFile* File = NULL;
119 if (SoundFileOgg::IsFileSupported(Filename, false)) File = new SoundFileOgg;
120 else if (SoundFileDefault::IsFileSupported(Filename, false)) File = new SoundFileDefault;
121
122 // Open it for writing
123 if (File)
124 {
125 if (File->OpenWrite(Filename, ChannelsCount, SampleRate))
126 {
127 File->myFilename = "";
128 File->myData = NULL;
129 File->mySize = 0;
130 File->myNbSamples = 0;
131 File->myChannelsCount = ChannelsCount;
132 File->mySampleRate = SampleRate;
133 }
134 else
135 {
136 delete File;
137 File = NULL;
138 }
139 }
140
141 return File;
142}
143
144
148SoundFile::SoundFile() :
149myNbSamples (0),
150myChannelsCount(0),
151mySampleRate (0)
152{
153
154}
155
156
160SoundFile::~SoundFile()
161{
162 // Nothing to do
163}
164
165
169std::size_t SoundFile::GetSamplesCount() const
170{
171 return myNbSamples;
172}
173
174
178unsigned int SoundFile::GetChannelsCount() const
179{
180 return myChannelsCount;
181}
182
183
187unsigned int SoundFile::GetSampleRate() const
188{
189 return mySampleRate;
190}
191
192
196bool SoundFile::Restart()
197{
198 if (myData)
199 {
200 // Reopen from memory
201 return OpenRead(myData, mySize, myNbSamples, myChannelsCount, mySampleRate);
202 }
203 else if (myFilename != "")
204 {
205 // Reopen from file
206 return OpenRead(myFilename, myNbSamples, myChannelsCount, mySampleRate);
207 }
208 else
209 {
210 // Trying to reopen a file opened in write mode... error
211 std::cerr << "Warning : trying to restart a sound opened in write mode, which is not allowed" << std::endl;
212 return false;
213 }
214}
215
216
220bool SoundFile::OpenRead(const std::string& Filename, std::size_t&, unsigned int&, unsigned int&)
221{
222 std::cerr << "Failed to open sound file \"" << Filename << "\", format is not supported by SFML" << std::endl;
223
224 return false;
225}
226
227
231bool SoundFile::OpenRead(const char*, std::size_t, std::size_t&, unsigned int&, unsigned int&)
232{
233 std::cerr << "Failed to open sound file from memory, format is not supported by SFML" << std::endl;
234
235 return false;
236}
237
238
242bool SoundFile::OpenWrite(const std::string& Filename, unsigned int, unsigned int)
243{
244 std::cerr << "Failed to open sound file \"" << Filename << "\", format is not supported by SFML" << std::endl;
245
246 return false;
247}
248
249
253std::size_t SoundFile::Read(Int16*, std::size_t)
254{
255 std::cerr << "Failed to read from sound file (not supported)" << std::endl;
256
257 return 0;
258}
259
260
264void SoundFile::Write(const Int16*, std::size_t)
265{
266 std::cerr << "Failed to write to sound file (not supported)" << std::endl;
267}
268
269} // namespace priv
270
271} // namespace sf