-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiointerface.cpp
140 lines (123 loc) · 4.02 KB
/
audiointerface.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
// Written by Adrian Musceac YO8RZZ at gmail dot com, started October 2013.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "audiointerface.h"
AudioInterface::AudioInterface(QObject *parent, unsigned sample_rate, unsigned channels, int normal) :
QObject(parent)
{
_s_rec = NULL;
_s_play = NULL;
_s_short_play = NULL;
_s_short_rec = NULL;
_error=0;
int rand_len = 4;
char rand[5];
genRandomStr(rand,rand_len);
static QString instance_name = QString::fromLocal8Bit(rand);
QString dtmf_rec = "qradiolink_dtmf_" + instance_name;
QString q_play = "qradiolink_" + instance_name;
QString audio_rec = "qradiolink_audio_" + instance_name;
pa_buffer_attr attr;
attr.fragsize = 1280;
attr.maxlength = -1;
attr.minreq = -1;
attr.prebuf = -1;
attr.tlength = 1280;
if(!normal)
{
pa_sample_spec ss;
ss.format = PA_SAMPLE_FLOAT32LE;
ss.rate = sample_rate;
ss.channels = channels;
if (!(_s_rec = pa_simple_new(NULL, dtmf_rec.toStdString().c_str(), PA_STREAM_RECORD, NULL, "record", &ss, NULL, &attr, &_error)))
{
fprintf(stderr, __FILE__": pa_simple_new() failed:\n");
}
if (!(_s_play = pa_simple_new(NULL, q_play.toStdString().c_str(), PA_STREAM_PLAYBACK, NULL, "play", &ss, NULL, &attr, &_error)))
{
fprintf(stderr, __FILE__": pa_simple_new() failed:\n");
}
}
else
{
pa_sample_spec ss_short;
ss_short.format = PA_SAMPLE_S16LE;
ss_short.rate = sample_rate;
ss_short.channels = channels;
if (!(_s_short_play = pa_simple_new(NULL, q_play.toStdString().c_str(), PA_STREAM_PLAYBACK, NULL, "play", &ss_short, NULL, &attr, &_error)))
{
fprintf(stderr, __FILE__": pa_simple_new() failed:\n");
}
if (!(_s_short_rec = pa_simple_new(NULL, audio_rec.toStdString().c_str(), PA_STREAM_RECORD, NULL, "record", &ss_short, NULL, &attr, &_error)))
{
fprintf(stderr, __FILE__": pa_simple_new() failed:\n");
}
}
}
AudioInterface::~AudioInterface()
{
if (_s_rec)
pa_simple_free(_s_rec);
if (_s_play)
pa_simple_free(_s_play);
if (_s_short_play)
pa_simple_free(_s_short_play);
if (_s_short_rec)
pa_simple_free(_s_short_rec);
}
int AudioInterface::read(float *buf, short bufsize)
{
if(!_s_rec)
return 1;
if (pa_simple_read(_s_rec, buf, bufsize, &_error) < 0)
{
fprintf(stderr, __FILE__": pa_simple_read() failed:\n");
return 1;
}
return 0;
}
int AudioInterface::write(float *buf, short bufsize)
{
if(!_s_play)
return 1;
if(pa_simple_write(_s_play, buf, bufsize, &_error) < 0)
{
fprintf(stderr, __FILE__": pa_simple_write() failed:\n");
return 1;
}
return 0;
}
int AudioInterface::write_short(short *buf, short bufsize)
{
if(!_s_short_play)
return 1;
if(pa_simple_write(_s_short_play, buf, bufsize, &_error) < 0)
{
fprintf(stderr, __FILE__": pa_simple_write() failed:\n");
return 1;
}
return 0;
}
int AudioInterface::read_short(short *buf, short bufsize)
{
if(!_s_short_rec)
return 1;
if(pa_simple_read(_s_short_rec, buf, bufsize, &_error) < 0)
{
fprintf(stderr, __FILE__": pa_simple_read() failed:\n");
return 1;
}
return 0;
}