-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibkmaudio_playback.cpp
executable file
·173 lines (145 loc) · 4.73 KB
/
libkmaudio_playback.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Audio Library for Linux and Windows
* ===================================
* Author: DJ0ABR
*
* Author: Kurt Moraw, Ham radio: DJ0ABR, github: dj0abr
* License: GPL-3
*
* compilation:
* Windows ... Visual Studio
* Linux ... make
*
* Documentation see: libkmaudio.h
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* libkmaudio_playback.cpp ...
* starts a portaudio playback stream and a callback routine. Plays the
* audio samples coming via the fifo (Windows only)
*/
#include "libkmaudio.h"
#define FRAMES_PER_BUFFER 512
int playCallback(const void* inputBuffer,
void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData);
void close_playback_stream(int idx)
{
if (devlist[idx].pbStream != NULL)
{
Pa_CloseStream(devlist[idx].pbStream);
devlist[idx].pbStream = NULL;
}
}
int kmaudio_startPlayback(char* devname, int samprate)
{
printf("Start request for PB stream:%s\n", devname);
if (devname == NULL || strlen(devname) < 3) // no devices defined yet
{
printf("no PB devices specified\n");
return -1;
}
int idx = searchDevice(devname, 1);
if (idx == -1)
{
printf("Playback Device:<%s> not found\n", devname);
return -1;
}
devlist[idx].working = 0;
close_playback_stream(idx);
printf("Starting PB stream:%s [%d]\n", devname, idx);
io_fifo_clear(idx);
devlist[idx].requested_samprate = samprate;
if (getRealSamprate(idx) == -1)
{
printf("Samplerate %d not supported by device:<%s>\n", samprate, devname);
return -1;
}
if (devlist[idx].requested_samprate != devlist[idx].real_samprate)
resampler_create(idx);
struct PaWasapiStreamInfo wasapiInfo;
memset(&wasapiInfo, 0, sizeof(PaWasapiStreamInfo));
wasapiInfo.size = sizeof(PaWasapiStreamInfo);
wasapiInfo.hostApiType = paWASAPI;
wasapiInfo.version = 1;
wasapiInfo.flags = (paWinWasapiExclusive | paWinWasapiThreadPriority);
wasapiInfo.threadPriority = eThreadPriorityProAudio;
devlist[idx].outputParameters.hostApiSpecificStreamInfo = (&wasapiInfo);
PaError e = Pa_IsFormatSupported(&devlist[idx].outputParameters, NULL, (double)devlist[idx].real_samprate);
printf("Playback : err:%d device:%d PAdev:%d samprate: %f chans:%d\n", e, idx, devlist[idx].devnum, (double)devlist[idx].real_samprate, devlist[idx].outputParameters.channelCount);
devlist[idx].index = idx;
int err = Pa_OpenStream(
&devlist[idx].pbStream,
NULL,
&devlist[idx].outputParameters,
(double)devlist[idx].real_samprate,
FRAMES_PER_BUFFER,
paClipOff,
playCallback,
&(devlist[idx].index));
if (err != paNoError)
{
printf("cannot open playback stream for device:<%s> %d\n", devname, err);
return -1;
}
err = Pa_StartStream(devlist[idx].pbStream);
if (err != paNoError)
{
printf("cannot start playback stream for device:<%s>\n", devname);
return -1;
}
printf("Playback started sucessfully\n");
devlist[idx].working = 1;
return idx;
}
int playCallback( const void* inputBuffer,
void* outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData)
{
int16_t* rptr = (int16_t*)outputBuffer;
int devidx = *((int*)userData);
int chans = devlist[devidx].outputParameters.channelCount;
//measure_speed_bps(framesPerBuffer);
int16_t f[FRAMES_PER_BUFFER];
memset(f, 0, sizeof(int16_t) * FRAMES_PER_BUFFER);
unsigned int num = io_read_fifo_num_short(devidx, f, framesPerBuffer);
if (num < framesPerBuffer)
{
//printf("got %d from fifo, requested %d\n", num, framesPerBuffer);
}
int av = io_fifo_elems_avail(devidx);
for (unsigned int i = 0; i < framesPerBuffer; i++)
{
if (chans == 1) rptr[i] = f[i];
else
{
rptr[i * 2] = f[i];
rptr[i * 2 + 1] = f[i];
}
}
// Prevent unused variable warnings
(void)inputBuffer;
(void)timeInfo;
(void)statusFlags;
if (keeprunning == 1)
return paContinue;
return paComplete;
}