VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
tklb::ogg Namespace Reference

Functions

template<typename T , class Buffer = AudioBufferTpl<T>>
bool load (const char *path, Buffer &out, typename Buffer::Size length=0)
 Decode ogg/vorbis from memory or file path. More...
 

Function Documentation

◆ load()

template<typename T , class Buffer = AudioBufferTpl<T>>
bool tklb::ogg::load ( const char *  path,
Buffer &  out,
typename Buffer::Size  length = 0 
)

Decode ogg/vorbis from memory or file path.

Parameters
pathThe path or the wav file buffer if length is non 0
outThe buffer to store the result in
lengthThe length of the wav file buffer if not reading from file

Definition at line 28 of file TOggFile.hpp.

28 {
29 using Size = typename Buffer::Size;
30 using uchar = unsigned char;
31 using ushort = unsigned short;
32
33 stb_vorbis* vorbis;
34 int error = 0;
35 if (length == 0) {
36 #ifndef TKLB_NO_STDIO
37 vorbis = stb_vorbis_open_filename(path, &error, nullptr);
38 #else
39 return false;
40 #endif
41 } else {
42 vorbis = stb_vorbis_open_memory(
43 reinterpret_cast<const unsigned char*>(path), length, &error, nullptr
44 );
45 }
46
47 if (error != VORBIS__no_error) {
48 stb_vorbis_close(vorbis);
49 return false;
50 }
51
52 const stb_vorbis_info info = stb_vorbis_get_info(vorbis);
53 const Size streamLength = stb_vorbis_stream_length_in_samples(vorbis);
54
55
56 out.sampleRate = info.sample_rate;
57 out.resize(streamLength, Size(info.channels)); // will fail at channels higher than allowed
58
59 if (std::is_same<float, T>::value) {
60 HeapBuffer<T*> buffer;
61 buffer.resize(info.channels);
62 out.getRaw(buffer.data());
63 // seems evil but where only here if it's alredy a float**
64 float** fbuf = reinterpret_cast<float**>(buffer.data());
65 Size got = stb_vorbis_get_samples_float(
66 vorbis, info.channels, fbuf, streamLength
67 );
68 out.setValidSize(got);
69 stb_vorbis_close(vorbis);
70 return got == streamLength;
71 }
72
73 Size read = 0;
74
75 int channels = info.channels;
76 float** bufferPtr = nullptr;
77 while (read < streamLength) {
78 Size got = stb_vorbis_get_frame_float(vorbis, &channels, &bufferPtr);
79 out.set(bufferPtr, got, channels, 0, read);
80 read += got;
81 }
82
83 out.setValidSize(read);
84
85 stb_vorbis_close(vorbis);
86
87 if (read == 0) {
88 return false;
89 }
90
91 return true;
92 }
unsigned int Size
How the elements are addressed in the heapbuffer.
Definition: vae.hpp:33
Here is the call graph for this function: