28 #include <SFML/Audio/SoundFileOgg.hpp> 29 #include <SFML/Audio/stb_vorbis/stb_vorbis.h> 40 SoundFileOgg::SoundFileOgg() :
51 SoundFileOgg::~SoundFileOgg()
54 stb_vorbis_close(myStream);
61 bool SoundFileOgg::IsFileSupported(
const std::string& Filename,
bool Read)
66 stb_vorbis* Stream = stb_vorbis_open_filename(const_cast<char*>(Filename.c_str()), NULL, NULL);
70 stb_vorbis_close(Stream);
89 bool SoundFileOgg::IsFileSupported(
const char* Data, std::size_t SizeInBytes)
92 unsigned char* Buffer =
reinterpret_cast<unsigned char*
>(
const_cast<char*
>(Data));
93 int Length =
static_cast<int>(SizeInBytes);
94 stb_vorbis* Stream = stb_vorbis_open_memory(Buffer, Length, NULL, NULL);
98 stb_vorbis_close(Stream);
111 bool SoundFileOgg::OpenRead(
const std::string& Filename, std::size_t& NbSamples,
unsigned int& ChannelsCount,
unsigned int& SampleRate)
115 stb_vorbis_close(myStream);
118 myStream = stb_vorbis_open_filename(const_cast<char*>(Filename.c_str()), NULL, NULL);
119 if (myStream == NULL)
121 std::cerr <<
"Failed to read sound file \"" << Filename <<
"\" (cannot open the file)" << std::endl;
126 stb_vorbis_info Infos = stb_vorbis_get_info(myStream);
127 ChannelsCount = myChannelsCount = Infos.channels;
128 SampleRate = Infos.sample_rate;
129 NbSamples =
static_cast<std::size_t
>(stb_vorbis_stream_length_in_samples(myStream) * ChannelsCount);
138 bool SoundFileOgg::OpenRead(
const char* Data, std::size_t SizeInBytes, std::size_t& NbSamples,
unsigned int& ChannelsCount,
unsigned int& SampleRate)
142 stb_vorbis_close(myStream);
145 unsigned char* Buffer =
reinterpret_cast<unsigned char*
>(
const_cast<char*
>(Data));
146 int Length =
static_cast<int>(SizeInBytes);
147 myStream = stb_vorbis_open_memory(Buffer, Length, NULL, NULL);
148 if (myStream == NULL)
150 std::cerr <<
"Failed to read sound file from memory (cannot open the file)" << std::endl;
155 stb_vorbis_info Infos = stb_vorbis_get_info(myStream);
156 ChannelsCount = myChannelsCount = Infos.channels;
157 SampleRate = Infos.sample_rate;
158 NbSamples =
static_cast<std::size_t
>(stb_vorbis_stream_length_in_samples(myStream) * ChannelsCount);
167 std::size_t SoundFileOgg::Read(Int16* Data, std::size_t NbSamples)
169 if (myStream && Data && NbSamples)
171 int Read = stb_vorbis_get_samples_short_interleaved(myStream, myChannelsCount, Data, static_cast<int>(NbSamples));
172 return static_cast<std::size_t
>(Read * myChannelsCount);