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
|
/*
* lingot, a musical instrument tuner.
*
* Copyright (C) 2004-2011 Ibán Cereijo Graña, Jairo Chapela Martínez.
*
* This file is part of lingot.
*
* lingot 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.
*
* lingot 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 lingot; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include "lingot-defs.h"
#include "lingot-audio-portaudio.h"
#include "lingot-i18n.h"
#include "lingot-msg.h"
#include <string.h>
LingotAudioHandler* lingot_audio_portaudio_new(char* device, int sample_rate) {
LingotAudioHandler* audio = NULL;
# ifdef PORTAUDIO
char error_message[1000];
PaError err;
const char* exception;
int channels = 1;
const PaStreamInfo* s_info;
PaDeviceIndex dev_idx_default;
const PaDeviceInfo* dev_info;
audio = malloc(sizeof(LingotAudioHandler));
audio->read_buffer = NULL;
audio->audio_system = AUDIO_SYSTEM_PORTAUDIO;
audio->read_buffer_size = 128;
try {
if ((err = Pa_Initialize()) != paNoError)
{
sprintf(error_message, "Pa_Initialize error: %s\n",
Pa_GetErrorText(err));
throw(error_message);
}
dev_idx_default = Pa_GetDefaultInputDevice();
dev_info = Pa_GetDeviceInfo(dev_idx_default);
strcpy(audio->device, dev_info->name);
if ((err = Pa_OpenDefaultStream( &audio->portaudio_stream, channels, 0, paInt16, sample_rate,
audio->read_buffer_size, NULL,
NULL)) != paNoError)
{
sprintf(error_message, "Pa_OpenDefaultStream error: %s\n",
Pa_GetErrorText(err));
throw(error_message);
}
if ((err = Pa_StartStream(audio->portaudio_stream)) != paNoError)
{
sprintf(error_message, "Pa_StartStream error: %s\n",
Pa_GetErrorText(err));
throw(error_message);
}
s_info = Pa_GetStreamInfo(audio->portaudio_stream);
audio->real_sample_rate = s_info->sampleRate;
audio->read_buffer = malloc(channels * audio->read_buffer_size
* Pa_GetSampleSize(paInt16));
memset(audio->read_buffer, 0, audio->read_buffer_size
* Pa_GetSampleSize(paInt16));
} catch {
if (audio->portaudio_stream != NULL)
Pa_CloseStream(audio->portaudio_stream);
free(audio);
audio = NULL;
lingot_msg_add_error(exception);
}
# else
lingot_msg_add_error(
_("The application has not been built with PORTAUDIO support"));
# endif
return audio;
}
void lingot_audio_portaudio_destroy(LingotAudioHandler* audio) {
# ifdef PORTAUDIO
if (audio != NULL) {
Pa_CloseStream(audio->portaudio_stream);
free(audio->read_buffer);
}
# endif
}
int lingot_audio_portaudio_read(LingotAudioHandler* audio) {
# ifdef PORTAUDIO
PaError err;
int i;
if (((err = Pa_ReadStream(audio->portaudio_stream, audio->read_buffer,
audio->read_buffer_size)) != paNoError) && (err != paInputOverflowed))
{
char buff[100];
sprintf(buff, _("Read from audio interface failed.\n%s."),
Pa_GetErrorText(err));
lingot_msg_add_error(buff);
return -1;
}
// float point conversion
for (i = 0; i < audio->read_buffer_size; i++) {
audio->flt_read_buffer[i] = audio->read_buffer[i];
}
# endif
return 0;
}
LingotAudioSystemProperties* lingot_audio_portaudio_get_audio_system_properties(
audio_system_t audio_system) {
LingotAudioSystemProperties* result =
(LingotAudioSystemProperties*) malloc(1
* sizeof(LingotAudioSystemProperties));
// TODO
result->forced_sample_rate = 0;
result->n_devices = 0;
result->devices = NULL;
result->n_sample_rates = 5;
result->sample_rates = malloc(result->n_sample_rates * sizeof(int));
result->sample_rates[0] = 8000;
result->sample_rates[1] = 11025;
result->sample_rates[2] = 22050;
result->sample_rates[3] = 44100;
result->sample_rates[4] = 48000;
return result;
}
|