TDME2  1.9.200
VorbisAudioStream.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 
5 #include <tdme/tdme.h>
10 #include <tdme/utilities/Console.h>
11 
13 
14 using std::string;
15 using std::to_string;
16 
22 
23 void VorbisAudioStream::rewind()
24 {
25  if (initiated == false) return;
26 
27  try {
28  decoder.reset();
29  } catch (FileSystemException &fse) {
30  Console::println(string("VorbisAudioStream::rewind(): '"+ (id) + "': " + fse.what()));
31  } catch (AudioDecoderException &ade) {
32  Console::println(string("VorbisAudioStream::rewind(): '" + (id) + "': " + ade.what()));
33  }
34 }
35 
37 {
38  if (initiated == true) return true;
39 
40  uint32_t sampleRate = 0;
41  uint8_t channels = 0;
42 
43  // decode audio stream
44  try {
45  // decode ogg vorbis
47  Console::println(
48  string(
49  "VorbisAudioStream::initialize(): '" +
50  id +
51  "' with " +
52  to_string(decoder.getBitsPerSample()) +
53  " bits per sample, " +
54  to_string(decoder.getChannels()) +
55  " channels, " +
56  to_string(decoder.getSampleRate()) +
57  " samplerate"
58  )
59  );
62  } catch (FileSystemException& fse) {
63  Console::println(string("VorbisAudioStream::initialize(): '" + (id) + "': " + fse.what()));
64  decoder.close();
65  dispose();
66  return false;
67  } catch (AudioDecoderException& ade) {
68  Console::println(string("VorbisAudioStream::initialize(): '" + (id) + "': " + ade.what()));
69  decoder.close();
70  dispose();
71  return false;
72  }
73 
74  // set stream parameters
76 
77  // check if underlying audio stream did initialize
78  if (AudioStream::initialize() == false) {
79  // nope, dispose and such
80  decoder.close();
81  dispose();
82  return false;
83  }
84 
85  // success
86  initiated = true;
87  return true;
88 }
89 
91  auto bytesDecoded = 0;
92  try {
93  bytesDecoded = decoder.readFromStream(data);
94  if (looping == true && bytesDecoded < data->getCapacity()) {
95  decoder.reset();
96  }
97  } catch (FileSystemException& fse) {
98  Console::println(string("Audio stream: '" + (id) + "': " + fse.what()));
99  } catch (AudioDecoderException& ade) {
100  Console::println(string("Audio stream: '" + (id) + "': " + ade.what()));
101  }
102 }
103 
105 {
106  if (initiated == false) return;
107 
108  // close decoder
109  decoder.close();
110 
111  // dispose audio stream
113 
114  //
115  initiated = false;
116 }
virtual void dispose() override
Dispose this entity from OpenAL.
virtual bool initialize() override
Initiates this OpenAL entity to OpenAl.
unique_ptr< ByteBuffer > data
Definition: AudioStream.h:34
virtual void setParameters(uint32_t sampleRate, uint8_t channels, const int64_t bufferSize=32768)
Set audio initialization parameters.
Definition: AudioStream.cpp:35
void dispose() override
Dispose this entity from OpenAL.
bool initialize() override
Initiates this OpenAL entity to OpenAl.
void fillBuffer(ByteBuffer *data) override
Fill buffer.
OGG/Vorbis audio decoder.
Definition: VorbisDecoder.h:31
void close() override
Closes the audio file.
int64_t readFromStream(ByteBuffer *data) override
Read raw PCM data from stream.
void reset() override
Resets this audio decoder, if a stream was open it will be rewinded.
void openFile(const string &pathName, const string &fileName) override
Open a local file.
Byte buffer class.
Definition: ByteBuffer.h:27
Console class.
Definition: Console.h:29