forked from paulh002/sdrberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioOutput.cpp
108 lines (94 loc) · 2.05 KB
/
AudioOutput.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
#include "AudioOutput.h"
int Audioout( void *outputBuffer,void *inputBuffer,unsigned int nBufferFrames,double streamTime,RtAudioStreamStatus status, void *userData)
{
double *buffer = (double *) outputBuffer;
if (status)
std::cout << "Stream underflow detected!" << std::endl;
// Write interleaved audio data.
if(((DataBuffer<Sample> *)userData)->queued_samples() == 0)
{
return 0;
}
SampleVector samples = ((DataBuffer<Sample> *)userData)->pull();
int i = 0;
for (auto& col : samples)
{
Sample v = col;
((double *)buffer)[i++] = v;
}
samples.clear();
samples.resize(0);
return 0;
}
bool AudioOutput::init(std::string device, int pcmrate)
{
if (this->getDeviceCount() < 1) {
std::cout << "\nNo audio devices found!\n";
m_zombie = true;
return false;
}
parameters.deviceId = this->getDefaultOutputDevice();
//if (parameters.deviceId == 0)
// {
// m_zombie = true;
// return false;
// }
parameters.nChannels = 2;
parameters.firstChannel = 0;
sampleRate = pcmrate;
bufferFrames = 1024; // 256 sample frames
return true;
}
bool AudioOutput::open(DataBuffer<Sample> *AudioBuffer)
{
databuffer = AudioBuffer;
try {
this->openStream(¶meters, NULL, RTAUDIO_FLOAT64, sampleRate, &bufferFrames, &Audioout, (void *)AudioBuffer);
this->startStream();
printf("bufferFrames set: %d\n", bufferFrames);
}
catch (RtAudioError& e) {
e.printMessage();
return false;
}
return true;
}
void AudioOutput::adjust_gain(SampleVector& samples)
{
for (unsigned int i = 0, n = samples.size(); i < n; i++) {
samples[i] *= m_volume;
}
}
void AudioOutput::close()
{
stop();
if (isStreamOpen())
closeStream();
}
AudioOutput::~AudioOutput()
{
close();
}
void AudioOutput::stop()
{
if (isStreamOpen())
{
try {
// Stop the stream
stopStream();
}
catch (RtAudioError& e) {
e.printMessage();
}
}
}
bool AudioOutput::write(SampleVector& audiosamples)
{
if (databuffer)
{
databuffer->push(move(audiosamples));
//printf("queued audio vectors %d\n", databuffer->queued_samples());
}
audiosamples.clear();
return true;
}