@@ -116,47 +116,31 @@ int32_t main() {
116116 sherpa_onnx::Microphone mic;
117117
118118 PaDeviceIndex num_devices = Pa_GetDeviceCount ();
119- std::cout << " Num devices: " << num_devices << " \n " ;
120119 if (num_devices == 0 ) {
121120 std::cerr << " If you are using Linux, please try "
122121 " ./build/bin/sense-voice-simulate-streaming-alsa-cxx-api\n " ;
123122 return -1 ;
124123 }
125124
126125 int32_t device_index = Pa_GetDefaultInputDevice ();
127-
128126 const char *pDeviceIndex = std::getenv (" SHERPA_ONNX_MIC_DEVICE" );
129127 if (pDeviceIndex) {
130128 fprintf (stderr, " Use specified device: %s\n " , pDeviceIndex);
131129 device_index = atoi (pDeviceIndex);
132130 }
131+ mic.PrintDevices (device_index);
133132
134- for (int32_t i = 0 ; i != num_devices; ++i) {
135- const PaDeviceInfo *info = Pa_GetDeviceInfo (i);
136- fprintf (stderr, " %s %d %s\n " , (i == device_index) ? " *" : " " , i,
137- info->name );
138- }
139-
140- PaStreamParameters param;
141- param.device = device_index;
142-
143- fprintf (stderr, " Use device: %d\n " , param.device );
144-
145- const PaDeviceInfo *info = Pa_GetDeviceInfo (param.device );
146- fprintf (stderr, " Name: %s\n " , info->name );
147- fprintf (stderr, " Max input channels: %d\n " , info->maxInputChannels );
148-
149- param.channelCount = 1 ;
150- param.sampleFormat = paFloat32;
151-
152- param.suggestedLatency = info->defaultLowInputLatency ;
153- param.hostApiSpecificStreamInfo = nullptr ;
154133 float mic_sample_rate = 16000 ;
155134 const char *sample_rate_str = std::getenv (" SHERPA_ONNX_MIC_SAMPLE_RATE" );
156135 if (sample_rate_str) {
157136 fprintf (stderr, " Use sample rate %f for mic\n " , mic_sample_rate);
158137 mic_sample_rate = atof (sample_rate_str);
159138 }
139+ if (!mic.OpenDevice (device_index, mic_sample_rate, 1 , RecordCallback,
140+ nullptr ) == false ) {
141+ std::cerr << " Failed to open microphone device\n " ;
142+ return -1 ;
143+ }
160144 float sample_rate = 16000 ;
161145 LinearResampler resampler;
162146 if (mic_sample_rate != sample_rate) {
@@ -168,27 +152,6 @@ int32_t main() {
168152 lowpass_cutoff, lowpass_filter_width);
169153 }
170154
171- PaStream *stream;
172- PaError err =
173- Pa_OpenStream (&stream, ¶m, nullptr , /* &outputParameters, */
174- mic_sample_rate,
175- 0 , // frames per buffer
176- paClipOff, // we won't output out of range samples
177- // so don't bother clipping them
178- RecordCallback, // RecordCallback is run in a separate
179- // thread created by portaudio
180- nullptr );
181- if (err != paNoError) {
182- fprintf (stderr, " portaudio error: %s\n " , Pa_GetErrorText (err));
183- exit (EXIT_FAILURE );
184- }
185-
186- err = Pa_StartStream (stream);
187- if (err != paNoError) {
188- fprintf (stderr, " portaudio error: %s\n " , Pa_GetErrorText (err));
189- exit (EXIT_FAILURE );
190- }
191-
192155 int32_t window_size = 512 ; // samples, please don't change
193156
194157 int32_t offset = 0 ;
@@ -276,11 +239,5 @@ int32_t main() {
276239 }
277240 }
278241
279- err = Pa_CloseStream (stream);
280- if (err != paNoError) {
281- fprintf (stderr, " portaudio error: %s\n " , Pa_GetErrorText (err));
282- exit (EXIT_FAILURE );
283- }
284-
285242 return 0 ;
286243}
0 commit comments