-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudio.cpp
158 lines (122 loc) · 3.76 KB
/
audio.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "audio.h"
#include "stb_vorbis.h"
#include <iostream>
#include <string>
#include <sstream>
#include <climits>
#include <stdio.h>
#include <stdlib.h>
#define SAMPLE_RATE 44100
void callback(struct SoundIoOutStream *outstream, int frame_count_min, int frame_count_max) {
const struct SoundIoChannelLayout* layout = &outstream->layout;
struct SoundIoChannelArea* areas;
int frames_left = frame_count_max;
int err;
while (frames_left > 0) {
int frame_count = frames_left;
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
fprintf(stderr, "%s\n", soundio_strerror(err));
exit(1);
}
if (!frame_count)
break;
AudioData* audioData;
if (outstream->userdata != NULL) {
audioData = (AudioData*) outstream->userdata;
} else {
audioData = nullptr;
}
float sample = 0.0f;
int delay = 0;
if (audioData != nullptr) {
// remember it's two channels
delay = -((audioData->startDelay / 1000.0f * 44100) / 2 * 2) * 2;
}
for (int frame = 0; frame < frame_count; frame += 1) {
for (int channel = 0; channel < layout->channel_count; channel += 1) {
float *ptr = (float*) (areas[channel].ptr + areas[channel].step * frame);
if (audioData != nullptr &&
audioData->pos + delay >= 0 &&
audioData->pos + delay < audioData->len * 2) {
sample = (float) audioData->stream[(audioData->pos + delay) + (channel % audioData->channels)] / SHRT_MAX;
} else {
sample = 0.0f;
}
*ptr = sample;
}
audioData->pos += audioData->channels;
}
if ((err = soundio_outstream_end_write(outstream))) {
fprintf(stderr, "%s\n", soundio_strerror(err));
exit(1);
}
frames_left -= frame_count;
}
}
Audio::Audio(std::string songId)
: songId(songId)
{
int err;
audioData = new AudioData();
soundio = soundio_create();
if (!soundio) {
fprintf(stderr, "out of memory\n");
}
if ((err = soundio_connect(soundio))) {
fprintf(stderr, "error connecting: %s", soundio_strerror(err));
}
soundio_flush_events(soundio);
int default_out_device_index = soundio_default_output_device_index(soundio);
if (default_out_device_index < 0) {
fprintf(stderr, "no output device found");
}
// device
device = soundio_get_output_device(soundio, default_out_device_index);
if (!device) {
fprintf(stderr, "out of memory");
}
// outstream
outstream = soundio_outstream_create(device);
outstream->format = SoundIoFormatFloat32NE;
outstream->sample_rate = SAMPLE_RATE;
outstream->write_callback = callback;
outstream->userdata = audioData;
if ((err = soundio_outstream_open(outstream))) {
fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
}
if (outstream->layout_error)
fprintf(stderr, "unable to set channel layout: %s\n", soundio_strerror(outstream->layout_error));
if ((err = soundio_outstream_start(outstream))) {
fprintf(stderr, "unable to start device: %s", soundio_strerror(err));
}
fprintf(stderr, "Output device: %s\n", device->name);
loadOgg();
}
bool Audio::loadOgg() {
short *song;
int channels, len, sampleRate;
std::stringstream fullPath;
fullPath << "songs/" << songId << "/" << songId << ".ogg";
len = stb_vorbis_decode_filename(fullPath.str().c_str(), &channels, &sampleRate, &song);
if (len < 0) {
return false;
}
audioData->pos = 0;
audioData->len = len;
audioData->stream = song;
audioData->channels = channels;
return true;
}
Audio::~Audio() {
soundio_outstream_destroy(outstream);
soundio_device_unref(device);
soundio_destroy(soundio);
free(audioData->stream);
}
AudioData::AudioData() {
startDelay = -1;
pos = 0;
len = -1;
channels = 0;
stream = nullptr;
}