-
Notifications
You must be signed in to change notification settings - Fork 5
/
pawrap.cc
150 lines (127 loc) · 4.54 KB
/
pawrap.cc
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
/* Synaesthesia - program to display sound graphically
Copyright (C) 1997 Paul Francis Harrison
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.
The author may be contacted at:
or
27 Bond St., Mt. Waverley, 3149, Melbourne, Australia
*/
#include <stdint.h>
#include <string.h>
#include <portaudio.h>
#ifdef __linux__
#include <unistd.h>
#include <fcntl.h>
#endif
#include "syna.h"
PaStream *stream;
void setupMixer(double &loudness) {
}
void setVolume(double loudness) {
}
static int pa_callback(const void *in, void *out,
unsigned long frameCount,
const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) {
sndbuf_store((int16_t *)in, frameCount * 2);
return paContinue;
}
void openSound(SoundSource source, int inFrequency, char *dspName,
char *mixerName, unsigned int chunk_size) {
PaError err;
#ifdef __linux__
int oldstderr;
/* Send useless ALSA stderr output to /dev/null */
oldstderr = dup(2);
if (oldstderr >= 0) {
int devnull = open("/dev/null", O_WRONLY);
if (devnull >= 0) {
dup2(devnull, 2);
close(devnull);
} else {
close(oldstderr);
oldstderr = -1;
}
}
#endif
err = Pa_Initialize();
#ifdef __linux__
if (oldstderr >= 0) {
/* dup2 will also close 2 before replacing it */
dup2(oldstderr, 2);
close(oldstderr);
}
#endif
if(err != paNoError) error("initializing PortAudio.");
sndbuf_init(chunk_size * 2);
PaStreamParameters inputParameters;
/* Search through devices to find one matching dspName.
However, don't use /dev/dsp via Portaudio. It works badly,
just like when you're using it directly. Excluding it
because it will be the default in the configuration file. */
if (dspName != NULL && strcmp(dspName, "/dev/dsp")) {
PaDeviceIndex numDevices = Pa_GetDeviceCount();
#ifdef WIN32
unsigned int namelen = strlen(dspName);
#endif
for (inputParameters.device = 0; inputParameters.device < numDevices;
inputParameters.device++) {
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(inputParameters.device);
if (deviceInfo != NULL && deviceInfo->name != NULL &&
#ifdef WIN32
/* Allow substring matches, for eg. "Stereo Mix (Sound card name)" */
!strncmp(deviceInfo->name, dspName, namelen)
#else
!strcmp(deviceInfo->name, dspName)
#endif
) break;
}
if (inputParameters.device == numDevices) {
warning("couldn't find selected sound device, using default");
inputParameters.device = Pa_GetDefaultInputDevice();
}
} else {
inputParameters.device = Pa_GetDefaultInputDevice();
}
if (inputParameters.device == paNoDevice) {
error("couldn't find default sound input device");
}
inputParameters.channelCount = 2;
inputParameters.sampleFormat = paInt16; /* PortAudio uses CPU endianness. */
inputParameters.suggestedLatency = Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream(&stream,
&inputParameters,
NULL, //&outputParameters,
inFrequency,
chunk_size,
paClipOff,
pa_callback,
NULL); /* no callback userData */
if(err != paNoError) error("opening PortAudio stream");
err = Pa_StartStream( stream );
if(err != paNoError) error("starting PortAudio stream");
data = new int16_t[NumSamples*2];
memset((char*)data,0,NumSamples*4);
}
void closeSound() {
PaError err;
err = Pa_StopStream(stream);
if (err != paNoError) error("stopping PortAudio stream");
err = Pa_CloseStream(stream);
if (err != paNoError) error("closing PortAudio stream");
err = Pa_Terminate();
if (err != paNoError) error("terminating PortAudio");
sndbuf_quit();
delete data;
}