-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioStream.h
69 lines (61 loc) · 1.54 KB
/
AudioStream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <ao/ao.h>
#include <mpg123.h>
#include <vector>
#include <string>
class AudioStream
{
public:
void FromFile(std::string filename)
{
int status;
m_handle = mpg123_new(NULL, &status);
// TODO: error handling
// if(status == xxx)
// ...
m_buffer.resize(mpg123_outblock(m_handle));
mpg123_open(m_handle, filename.c_str());
mpg123_getformat(m_handle, &m_rate, &m_numChannels, &m_encoding);
m_format.bits = mpg123_encsize(m_encoding) * 8;
m_format.rate = m_rate;
m_format.channels = m_numChannels;
m_format.byte_format = AO_FMT_NATIVE;
m_format.matrix = 0;
}
void Play()
{
m_audioDevice = ao_open_live(m_audioDriver, &m_format, NULL);
size_t status = 0;
while(mpg123_read(m_handle, (unsigned char*)m_buffer.data(),
m_buffer.size(), &status) == MPG123_OK)
{
ao_play(m_audioDevice, (char*)m_buffer.data(), status);
}
ao_close(m_audioDevice);
}
void Release()
{
}
AudioStream()
{
ao_initialize();
m_audioDriver = ao_default_driver_id();
mpg123_init();
}
~AudioStream()
{
mpg123_close(m_handle);
mpg123_delete(m_handle);
mpg123_exit();
ao_shutdown();
}
private:
mpg123_handle* m_handle;
std::vector<char> m_buffer;
int m_audioDriver;
ao_device* m_audioDevice;
ao_sample_format m_format;
int m_numChannels;
int m_encoding;
long m_rate;
};