Committed by
GitHub
move portaudio common record code to microphone (#2264)
Co-authored-by: cqm <cqm@97kid.com>
正在显示
12 个修改的文件
包含
130 行增加
和
446 行删除
| @@ -116,7 +116,6 @@ int32_t main() { | @@ -116,7 +116,6 @@ int32_t main() { | ||
| 116 | sherpa_onnx::Microphone mic; | 116 | sherpa_onnx::Microphone mic; |
| 117 | 117 | ||
| 118 | PaDeviceIndex num_devices = Pa_GetDeviceCount(); | 118 | PaDeviceIndex num_devices = Pa_GetDeviceCount(); |
| 119 | - std::cout << "Num devices: " << num_devices << "\n"; | ||
| 120 | if (num_devices == 0) { | 119 | if (num_devices == 0) { |
| 121 | std::cerr << " If you are using Linux, please try " | 120 | std::cerr << " If you are using Linux, please try " |
| 122 | "./build/bin/sense-voice-simulate-streaming-alsa-cxx-api\n"; | 121 | "./build/bin/sense-voice-simulate-streaming-alsa-cxx-api\n"; |
| @@ -124,39 +123,24 @@ int32_t main() { | @@ -124,39 +123,24 @@ int32_t main() { | ||
| 124 | } | 123 | } |
| 125 | 124 | ||
| 126 | int32_t device_index = Pa_GetDefaultInputDevice(); | 125 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 127 | - | ||
| 128 | const char *pDeviceIndex = std::getenv("SHERPA_ONNX_MIC_DEVICE"); | 126 | const char *pDeviceIndex = std::getenv("SHERPA_ONNX_MIC_DEVICE"); |
| 129 | if (pDeviceIndex) { | 127 | if (pDeviceIndex) { |
| 130 | fprintf(stderr, "Use specified device: %s\n", pDeviceIndex); | 128 | fprintf(stderr, "Use specified device: %s\n", pDeviceIndex); |
| 131 | device_index = atoi(pDeviceIndex); | 129 | device_index = atoi(pDeviceIndex); |
| 132 | } | 130 | } |
| 131 | + mic.PrintDevices(device_index); | ||
| 133 | 132 | ||
| 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; | ||
| 154 | float mic_sample_rate = 16000; | 133 | float mic_sample_rate = 16000; |
| 155 | const char *sample_rate_str = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 134 | const char *sample_rate_str = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 156 | if (sample_rate_str) { | 135 | if (sample_rate_str) { |
| 157 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); | 136 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); |
| 158 | mic_sample_rate = atof(sample_rate_str); | 137 | mic_sample_rate = atof(sample_rate_str); |
| 159 | } | 138 | } |
| 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 | + } | ||
| 160 | float sample_rate = 16000; | 144 | float sample_rate = 16000; |
| 161 | LinearResampler resampler; | 145 | LinearResampler resampler; |
| 162 | if (mic_sample_rate != sample_rate) { | 146 | if (mic_sample_rate != sample_rate) { |
| @@ -168,27 +152,6 @@ int32_t main() { | @@ -168,27 +152,6 @@ int32_t main() { | ||
| 168 | lowpass_cutoff, lowpass_filter_width); | 152 | lowpass_cutoff, lowpass_filter_width); |
| 169 | } | 153 | } |
| 170 | 154 | ||
| 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 | - | ||
| 192 | int32_t window_size = 512; // samples, please don't change | 155 | int32_t window_size = 512; // samples, please don't change |
| 193 | 156 | ||
| 194 | int32_t offset = 0; | 157 | int32_t offset = 0; |
| @@ -276,11 +239,5 @@ int32_t main() { | @@ -276,11 +239,5 @@ int32_t main() { | ||
| 276 | } | 239 | } |
| 277 | } | 240 | } |
| 278 | 241 | ||
| 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 | - | ||
| 285 | return 0; | 242 | return 0; |
| 286 | } | 243 | } |
| @@ -112,7 +112,6 @@ int32_t main() { | @@ -112,7 +112,6 @@ int32_t main() { | ||
| 112 | sherpa_onnx::Microphone mic; | 112 | sherpa_onnx::Microphone mic; |
| 113 | 113 | ||
| 114 | PaDeviceIndex num_devices = Pa_GetDeviceCount(); | 114 | PaDeviceIndex num_devices = Pa_GetDeviceCount(); |
| 115 | - std::cout << "Num devices: " << num_devices << "\n"; | ||
| 116 | if (num_devices == 0) { | 115 | if (num_devices == 0) { |
| 117 | std::cerr << " If you are using Linux, please try " | 116 | std::cerr << " If you are using Linux, please try " |
| 118 | "./build/bin/sense-voice-simulate-streaming-alsa-cxx-api\n"; | 117 | "./build/bin/sense-voice-simulate-streaming-alsa-cxx-api\n"; |
| @@ -120,33 +119,13 @@ int32_t main() { | @@ -120,33 +119,13 @@ int32_t main() { | ||
| 120 | } | 119 | } |
| 121 | 120 | ||
| 122 | int32_t device_index = Pa_GetDefaultInputDevice(); | 121 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 123 | - | ||
| 124 | const char *pDeviceIndex = std::getenv("SHERPA_ONNX_MIC_DEVICE"); | 122 | const char *pDeviceIndex = std::getenv("SHERPA_ONNX_MIC_DEVICE"); |
| 125 | if (pDeviceIndex) { | 123 | if (pDeviceIndex) { |
| 126 | fprintf(stderr, "Use specified device: %s\n", pDeviceIndex); | 124 | fprintf(stderr, "Use specified device: %s\n", pDeviceIndex); |
| 127 | device_index = atoi(pDeviceIndex); | 125 | device_index = atoi(pDeviceIndex); |
| 128 | } | 126 | } |
| 127 | + mic.PrintDevices(device_index); | ||
| 129 | 128 | ||
| 130 | - for (int32_t i = 0; i != num_devices; ++i) { | ||
| 131 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 132 | - fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 133 | - info->name); | ||
| 134 | - } | ||
| 135 | - | ||
| 136 | - PaStreamParameters param; | ||
| 137 | - param.device = device_index; | ||
| 138 | - | ||
| 139 | - fprintf(stderr, "Use device: %d\n", param.device); | ||
| 140 | - | ||
| 141 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device); | ||
| 142 | - fprintf(stderr, " Name: %s\n", info->name); | ||
| 143 | - fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 144 | - | ||
| 145 | - param.channelCount = 1; | ||
| 146 | - param.sampleFormat = paFloat32; | ||
| 147 | - | ||
| 148 | - param.suggestedLatency = info->defaultLowInputLatency; | ||
| 149 | - param.hostApiSpecificStreamInfo = nullptr; | ||
| 150 | float mic_sample_rate = 16000; | 129 | float mic_sample_rate = 16000; |
| 151 | const char *sample_rate_str = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 130 | const char *sample_rate_str = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 152 | if (sample_rate_str) { | 131 | if (sample_rate_str) { |
| @@ -163,26 +142,10 @@ int32_t main() { | @@ -163,26 +142,10 @@ int32_t main() { | ||
| 163 | resampler = LinearResampler::Create(mic_sample_rate, sample_rate, | 142 | resampler = LinearResampler::Create(mic_sample_rate, sample_rate, |
| 164 | lowpass_cutoff, lowpass_filter_width); | 143 | lowpass_cutoff, lowpass_filter_width); |
| 165 | } | 144 | } |
| 166 | - | ||
| 167 | - PaStream *stream; | ||
| 168 | - PaError err = | ||
| 169 | - Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 170 | - mic_sample_rate, | ||
| 171 | - 0, // frames per buffer | ||
| 172 | - paClipOff, // we won't output out of range samples | ||
| 173 | - // so don't bother clipping them | ||
| 174 | - RecordCallback, // RecordCallback is run in a separate | ||
| 175 | - // thread created by portaudio | ||
| 176 | - nullptr); | ||
| 177 | - if (err != paNoError) { | ||
| 178 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 179 | - exit(EXIT_FAILURE); | ||
| 180 | - } | ||
| 181 | - | ||
| 182 | - err = Pa_StartStream(stream); | ||
| 183 | - if (err != paNoError) { | ||
| 184 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 185 | - exit(EXIT_FAILURE); | 145 | + if (mic.OpenDevice(device_index, mic_sample_rate, 1, RecordCallback, |
| 146 | + nullptr) == false) { | ||
| 147 | + std::cerr << "Failed to open microphone device\n"; | ||
| 148 | + return -1; | ||
| 186 | } | 149 | } |
| 187 | 150 | ||
| 188 | int32_t window_size = 512; // samples, please don't change | 151 | int32_t window_size = 512; // samples, please don't change |
| @@ -272,11 +235,5 @@ int32_t main() { | @@ -272,11 +235,5 @@ int32_t main() { | ||
| 272 | } | 235 | } |
| 273 | } | 236 | } |
| 274 | 237 | ||
| 275 | - err = Pa_CloseStream(stream); | ||
| 276 | - if (err != paNoError) { | ||
| 277 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 278 | - exit(EXIT_FAILURE); | ||
| 279 | - } | ||
| 280 | - | ||
| 281 | return 0; | 238 | return 0; |
| 282 | } | 239 | } |
| @@ -7,8 +7,6 @@ | @@ -7,8 +7,6 @@ | ||
| 7 | #include <stdio.h> | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
| 9 | 9 | ||
| 10 | -#include "portaudio.h" // NOLINT | ||
| 11 | - | ||
| 12 | namespace sherpa_onnx { | 10 | namespace sherpa_onnx { |
| 13 | 11 | ||
| 14 | Microphone::Microphone() { | 12 | Microphone::Microphone() { |
| @@ -20,10 +18,85 @@ Microphone::Microphone() { | @@ -20,10 +18,85 @@ Microphone::Microphone() { | ||
| 20 | } | 18 | } |
| 21 | 19 | ||
| 22 | Microphone::~Microphone() { | 20 | Microphone::~Microphone() { |
| 21 | + CloseDevice(); | ||
| 23 | PaError err = Pa_Terminate(); | 22 | PaError err = Pa_Terminate(); |
| 24 | if (err != paNoError) { | 23 | if (err != paNoError) { |
| 25 | fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | 24 | fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); |
| 26 | - exit(-1); | 25 | + } |
| 26 | +} | ||
| 27 | + | ||
| 28 | +int Microphone::GetDeviceCount() const { | ||
| 29 | + return Pa_GetDeviceCount(); | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +int Microphone::GetDefaultInputDevice() const { | ||
| 33 | + return Pa_GetDefaultInputDevice(); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +void Microphone::PrintDevices(int device_index) const { | ||
| 37 | + PaDeviceIndex num_devices = Pa_GetDeviceCount(); | ||
| 38 | + fprintf(stderr, "Num devices: %d\n", num_devices); | ||
| 39 | + for (int32_t i = 0; i != num_devices; ++i) { | ||
| 40 | + const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 41 | + fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 42 | + info->name); | ||
| 43 | + } | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +bool Microphone::OpenDevice(int index, int sample_rate, int channel, PaStreamCallback cb, void* userdata) { | ||
| 47 | + if (index < 0 || index >= Pa_GetDeviceCount()) { | ||
| 48 | + fprintf(stderr, "Invalid device index: %d\n", index); | ||
| 49 | + return false; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + const PaDeviceInfo *info = Pa_GetDeviceInfo(index); | ||
| 53 | + if (!info) { | ||
| 54 | + fprintf(stderr, "No device info found for index: %d\n", index); | ||
| 55 | + return false; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + CloseDevice(); | ||
| 59 | + | ||
| 60 | + fprintf(stderr, "Use device: %d\n", index); | ||
| 61 | + fprintf(stderr, " Name: %s\n", info->name); | ||
| 62 | + fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 63 | + | ||
| 64 | + PaStreamParameters param; | ||
| 65 | + param.device = index; | ||
| 66 | + param.channelCount = channel; | ||
| 67 | + param.sampleFormat = paFloat32; | ||
| 68 | + param.suggestedLatency = info->defaultLowInputLatency; | ||
| 69 | + param.hostApiSpecificStreamInfo = nullptr; | ||
| 70 | + | ||
| 71 | + PaError err = Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 72 | + sample_rate, | ||
| 73 | + 0, // frames per buffer | ||
| 74 | + paClipOff, // we won't output out of range samples | ||
| 75 | + // so don't bother clipping them | ||
| 76 | + cb, userdata); | ||
| 77 | + if (err != paNoError) { | ||
| 78 | + fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 79 | + return false; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + err = Pa_StartStream(stream); | ||
| 83 | + fprintf(stderr, "Started\n"); | ||
| 84 | + | ||
| 85 | + if (err != paNoError) { | ||
| 86 | + fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 87 | + CloseDevice(); | ||
| 88 | + return false; | ||
| 89 | + } | ||
| 90 | + return true; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +void Microphone::CloseDevice() { | ||
| 94 | + if (stream) { | ||
| 95 | + PaError err = Pa_CloseStream(stream); | ||
| 96 | + if (err != paNoError) { | ||
| 97 | + fprintf(stderr, "Pa_CloseStream error: %s\n", Pa_GetErrorText(err)); | ||
| 98 | + } | ||
| 99 | + stream = nullptr; | ||
| 27 | } | 100 | } |
| 28 | } | 101 | } |
| 29 | 102 |
| @@ -4,13 +4,22 @@ | @@ -4,13 +4,22 @@ | ||
| 4 | 4 | ||
| 5 | #ifndef SHERPA_ONNX_CSRC_MICROPHONE_H_ | 5 | #ifndef SHERPA_ONNX_CSRC_MICROPHONE_H_ |
| 6 | #define SHERPA_ONNX_CSRC_MICROPHONE_H_ | 6 | #define SHERPA_ONNX_CSRC_MICROPHONE_H_ |
| 7 | +#include "portaudio.h" // NOLINT | ||
| 7 | 8 | ||
| 8 | namespace sherpa_onnx { | 9 | namespace sherpa_onnx { |
| 9 | 10 | ||
| 10 | class Microphone { | 11 | class Microphone { |
| 12 | + PaStream *stream = nullptr; | ||
| 11 | public: | 13 | public: |
| 12 | Microphone(); | 14 | Microphone(); |
| 13 | ~Microphone(); | 15 | ~Microphone(); |
| 16 | + | ||
| 17 | + int GetDeviceCount() const; | ||
| 18 | + int GetDefaultInputDevice() const; | ||
| 19 | + void PrintDevices(int sel) const; | ||
| 20 | + | ||
| 21 | + bool OpenDevice(int index, int sample_rate, int channel, PaStreamCallback cb, void* userdata); | ||
| 22 | + void CloseDevice(); | ||
| 14 | }; | 23 | }; |
| 15 | 24 | ||
| 16 | } // namespace sherpa_onnx | 25 | } // namespace sherpa_onnx |
| @@ -14,7 +14,6 @@ | @@ -14,7 +14,6 @@ | ||
| 14 | 14 | ||
| 15 | #include "sherpa-onnx/csrc/alsa.h" | 15 | #include "sherpa-onnx/csrc/alsa.h" |
| 16 | #include "sherpa-onnx/csrc/macros.h" | 16 | #include "sherpa-onnx/csrc/macros.h" |
| 17 | -#include "sherpa-onnx/csrc/microphone.h" | ||
| 18 | #include "sherpa-onnx/csrc/speaker-embedding-extractor.h" | 17 | #include "sherpa-onnx/csrc/speaker-embedding-extractor.h" |
| 19 | #include "sherpa-onnx/csrc/speaker-embedding-manager.h" | 18 | #include "sherpa-onnx/csrc/speaker-embedding-manager.h" |
| 20 | #include "sherpa-onnx/csrc/wave-reader.h" | 19 | #include "sherpa-onnx/csrc/wave-reader.h" |
| @@ -79,11 +79,7 @@ for a list of pre-trained models to download. | @@ -79,11 +79,7 @@ for a list of pre-trained models to download. | ||
| 79 | 79 | ||
| 80 | sherpa_onnx::Microphone mic; | 80 | sherpa_onnx::Microphone mic; |
| 81 | 81 | ||
| 82 | - PaDeviceIndex num_devices = Pa_GetDeviceCount(); | ||
| 83 | - fprintf(stderr, "Num devices: %d\n", num_devices); | ||
| 84 | - | ||
| 85 | int32_t device_index = Pa_GetDefaultInputDevice(); | 82 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 86 | - | ||
| 87 | if (device_index == paNoDevice) { | 83 | if (device_index == paNoDevice) { |
| 88 | fprintf(stderr, "No default input device found\n"); | 84 | fprintf(stderr, "No default input device found\n"); |
| 89 | fprintf(stderr, "If you are using Linux, please switch to \n"); | 85 | fprintf(stderr, "If you are using Linux, please switch to \n"); |
| @@ -97,26 +93,7 @@ for a list of pre-trained models to download. | @@ -97,26 +93,7 @@ for a list of pre-trained models to download. | ||
| 97 | device_index = atoi(pDeviceIndex); | 93 | device_index = atoi(pDeviceIndex); |
| 98 | } | 94 | } |
| 99 | 95 | ||
| 100 | - for (int32_t i = 0; i != num_devices; ++i) { | ||
| 101 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 102 | - fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 103 | - info->name); | ||
| 104 | - } | ||
| 105 | - | ||
| 106 | - PaStreamParameters param; | ||
| 107 | - param.device = device_index; | ||
| 108 | - | ||
| 109 | - fprintf(stderr, "Use device: %d\n", param.device); | ||
| 110 | - | ||
| 111 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device); | ||
| 112 | - fprintf(stderr, " Name: %s\n", info->name); | ||
| 113 | - fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 114 | - | ||
| 115 | - param.channelCount = 1; | ||
| 116 | - param.sampleFormat = paFloat32; | ||
| 117 | - | ||
| 118 | - param.suggestedLatency = info->defaultLowInputLatency; | ||
| 119 | - param.hostApiSpecificStreamInfo = nullptr; | 96 | + mic.PrintDevices(device_index); |
| 120 | 97 | ||
| 121 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 98 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 122 | if (pSampleRateStr) { | 99 | if (pSampleRateStr) { |
| @@ -124,24 +101,9 @@ for a list of pre-trained models to download. | @@ -124,24 +101,9 @@ for a list of pre-trained models to download. | ||
| 124 | mic_sample_rate = atof(pSampleRateStr); | 101 | mic_sample_rate = atof(pSampleRateStr); |
| 125 | } | 102 | } |
| 126 | 103 | ||
| 127 | - PaStream *stream; | ||
| 128 | - PaError err = | ||
| 129 | - Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 130 | - mic_sample_rate, | ||
| 131 | - 0, // frames per buffer | ||
| 132 | - paClipOff, // we won't output out of range samples | ||
| 133 | - // so don't bother clipping them | ||
| 134 | - RecordCallback, s.get()); | ||
| 135 | - if (err != paNoError) { | ||
| 136 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 137 | - exit(EXIT_FAILURE); | ||
| 138 | - } | ||
| 139 | - | ||
| 140 | - err = Pa_StartStream(stream); | ||
| 141 | - fprintf(stderr, "Started\n"); | ||
| 142 | - | ||
| 143 | - if (err != paNoError) { | ||
| 144 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | 104 | + if(!mic.OpenDevice(device_index, mic_sample_rate, 1, |
| 105 | + RecordCallback, s.get())) { | ||
| 106 | + fprintf(stderr, "portaudio error: %d\n", device_index); | ||
| 145 | exit(EXIT_FAILURE); | 107 | exit(EXIT_FAILURE); |
| 146 | } | 108 | } |
| 147 | 109 | ||
| @@ -164,11 +126,5 @@ for a list of pre-trained models to download. | @@ -164,11 +126,5 @@ for a list of pre-trained models to download. | ||
| 164 | Pa_Sleep(20); // sleep for 20ms | 126 | Pa_Sleep(20); // sleep for 20ms |
| 165 | } | 127 | } |
| 166 | 128 | ||
| 167 | - err = Pa_CloseStream(stream); | ||
| 168 | - if (err != paNoError) { | ||
| 169 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 170 | - exit(EXIT_FAILURE); | ||
| 171 | - } | ||
| 172 | - | ||
| 173 | return 0; | 129 | return 0; |
| 174 | } | 130 | } |
| @@ -120,11 +120,7 @@ for more models. | @@ -120,11 +120,7 @@ for more models. | ||
| 120 | 120 | ||
| 121 | sherpa_onnx::Microphone mic; | 121 | sherpa_onnx::Microphone mic; |
| 122 | 122 | ||
| 123 | - PaDeviceIndex num_devices = Pa_GetDeviceCount(); | ||
| 124 | - fprintf(stderr, "Num devices: %d\n", num_devices); | ||
| 125 | - | ||
| 126 | int32_t device_index = Pa_GetDefaultInputDevice(); | 123 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 127 | - | ||
| 128 | if (device_index == paNoDevice) { | 124 | if (device_index == paNoDevice) { |
| 129 | fprintf(stderr, "No default input device found\n"); | 125 | fprintf(stderr, "No default input device found\n"); |
| 130 | fprintf(stderr, "If you are using Linux, please switch to \n"); | 126 | fprintf(stderr, "If you are using Linux, please switch to \n"); |
| @@ -138,26 +134,7 @@ for more models. | @@ -138,26 +134,7 @@ for more models. | ||
| 138 | device_index = atoi(pDeviceIndex); | 134 | device_index = atoi(pDeviceIndex); |
| 139 | } | 135 | } |
| 140 | 136 | ||
| 141 | - for (int32_t i = 0; i != num_devices; ++i) { | ||
| 142 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 143 | - fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 144 | - info->name); | ||
| 145 | - } | ||
| 146 | - | ||
| 147 | - PaStreamParameters param; | ||
| 148 | - param.device = device_index; | ||
| 149 | - | ||
| 150 | - fprintf(stderr, "Use device: %d\n", param.device); | ||
| 151 | - | ||
| 152 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device); | ||
| 153 | - fprintf(stderr, " Name: %s\n", info->name); | ||
| 154 | - fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 155 | - | ||
| 156 | - param.channelCount = 1; | ||
| 157 | - param.sampleFormat = paFloat32; | ||
| 158 | - | ||
| 159 | - param.suggestedLatency = info->defaultLowInputLatency; | ||
| 160 | - param.hostApiSpecificStreamInfo = nullptr; | 137 | + mic.PrintDevices(device_index); |
| 161 | float mic_sample_rate = 16000; | 138 | float mic_sample_rate = 16000; |
| 162 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 139 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 163 | if (pSampleRateStr) { | 140 | if (pSampleRateStr) { |
| @@ -165,24 +142,9 @@ for more models. | @@ -165,24 +142,9 @@ for more models. | ||
| 165 | mic_sample_rate = atof(pSampleRateStr); | 142 | mic_sample_rate = atof(pSampleRateStr); |
| 166 | } | 143 | } |
| 167 | 144 | ||
| 168 | - PaStream *stream; | ||
| 169 | - PaError err = | ||
| 170 | - Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 171 | - mic_sample_rate, | ||
| 172 | - 0, // frames per buffer | ||
| 173 | - paClipOff, // we won't output out of range samples | ||
| 174 | - // so don't bother clipping them | ||
| 175 | - RecordCallback, nullptr); | ||
| 176 | - if (err != paNoError) { | ||
| 177 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 178 | - exit(EXIT_FAILURE); | ||
| 179 | - } | ||
| 180 | - | ||
| 181 | - err = Pa_StartStream(stream); | ||
| 182 | - fprintf(stderr, "Started\n"); | ||
| 183 | - | ||
| 184 | - if (err != paNoError) { | ||
| 185 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | 145 | + if (!mic.OpenDevice(device_index, mic_sample_rate, 1, |
| 146 | + RecordCallback, nullptr /* user_data */)){ | ||
| 147 | + fprintf(stderr, "portaudio error: %d\n", device_index); | ||
| 186 | exit(EXIT_FAILURE); | 148 | exit(EXIT_FAILURE); |
| 187 | } | 149 | } |
| 188 | 150 | ||
| @@ -226,11 +188,5 @@ for more models. | @@ -226,11 +188,5 @@ for more models. | ||
| 226 | } | 188 | } |
| 227 | t.join(); | 189 | t.join(); |
| 228 | 190 | ||
| 229 | - err = Pa_CloseStream(stream); | ||
| 230 | - if (err != paNoError) { | ||
| 231 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 232 | - exit(EXIT_FAILURE); | ||
| 233 | - } | ||
| 234 | - | ||
| 235 | return 0; | 191 | return 0; |
| 236 | } | 192 | } |
| @@ -220,9 +220,6 @@ Note that `zh` means Chinese, while `en` means English. | @@ -220,9 +220,6 @@ Note that `zh` means Chinese, while `en` means English. | ||
| 220 | 220 | ||
| 221 | sherpa_onnx::Microphone mic; | 221 | sherpa_onnx::Microphone mic; |
| 222 | 222 | ||
| 223 | - PaDeviceIndex num_devices = Pa_GetDeviceCount(); | ||
| 224 | - fprintf(stderr, "Num devices: %d\n", num_devices); | ||
| 225 | - | ||
| 226 | int32_t device_index = Pa_GetDefaultInputDevice(); | 223 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 227 | if (device_index == paNoDevice) { | 224 | if (device_index == paNoDevice) { |
| 228 | fprintf(stderr, "No default input device found\n"); | 225 | fprintf(stderr, "No default input device found\n"); |
| @@ -238,52 +235,18 @@ Note that `zh` means Chinese, while `en` means English. | @@ -238,52 +235,18 @@ Note that `zh` means Chinese, while `en` means English. | ||
| 238 | device_index = atoi(pDeviceIndex); | 235 | device_index = atoi(pDeviceIndex); |
| 239 | } | 236 | } |
| 240 | 237 | ||
| 241 | - for (int32_t i = 0; i != num_devices; ++i) { | ||
| 242 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 243 | - fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 244 | - info->name); | ||
| 245 | - } | ||
| 246 | - | ||
| 247 | - PaStreamParameters param; | ||
| 248 | - param.device = device_index; | ||
| 249 | - | ||
| 250 | - fprintf(stderr, "Use device: %d\n", param.device); | ||
| 251 | - | ||
| 252 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device); | ||
| 253 | - fprintf(stderr, " Name: %s\n", info->name); | ||
| 254 | - fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 255 | - | ||
| 256 | - param.channelCount = 1; | ||
| 257 | - param.sampleFormat = paFloat32; | 238 | + mic.PrintDevices(device_index); |
| 258 | 239 | ||
| 259 | - param.suggestedLatency = info->defaultLowInputLatency; | ||
| 260 | - param.hostApiSpecificStreamInfo = nullptr; | ||
| 261 | float mic_sample_rate = 16000; | 240 | float mic_sample_rate = 16000; |
| 262 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 241 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 263 | if (pSampleRateStr) { | 242 | if (pSampleRateStr) { |
| 264 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); | 243 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); |
| 265 | mic_sample_rate = atof(pSampleRateStr); | 244 | mic_sample_rate = atof(pSampleRateStr); |
| 266 | } | 245 | } |
| 267 | - float sample_rate = 16000; | ||
| 268 | - | ||
| 269 | - PaStream *stream; | ||
| 270 | - PaError err = | ||
| 271 | - Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 272 | - mic_sample_rate, | ||
| 273 | - 0, // frames per buffer | ||
| 274 | - paClipOff, // we won't output out of range samples | ||
| 275 | - // so don't bother clipping them | ||
| 276 | - RecordCallback, nullptr); | ||
| 277 | - if (err != paNoError) { | ||
| 278 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 279 | - exit(EXIT_FAILURE); | ||
| 280 | - } | ||
| 281 | - | ||
| 282 | - err = Pa_StartStream(stream); | ||
| 283 | - fprintf(stderr, "Started\n"); | ||
| 284 | 246 | ||
| 285 | - if (err != paNoError) { | ||
| 286 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | 247 | + if (!mic.OpenDevice(device_index, mic_sample_rate, 1, |
| 248 | + RecordCallback, nullptr /* user_data */)){ | ||
| 249 | + fprintf(stderr, "portaudio error: %d\n", device_index); | ||
| 287 | exit(EXIT_FAILURE); | 250 | exit(EXIT_FAILURE); |
| 288 | } | 251 | } |
| 289 | 252 | ||
| @@ -323,11 +286,5 @@ Note that `zh` means Chinese, while `en` means English. | @@ -323,11 +286,5 @@ Note that `zh` means Chinese, while `en` means English. | ||
| 323 | } | 286 | } |
| 324 | t.join(); | 287 | t.join(); |
| 325 | 288 | ||
| 326 | - err = Pa_CloseStream(stream); | ||
| 327 | - if (err != paNoError) { | ||
| 328 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 329 | - exit(EXIT_FAILURE); | ||
| 330 | - } | ||
| 331 | - | ||
| 332 | return 0; | 289 | return 0; |
| 333 | } | 290 | } |
| @@ -136,11 +136,7 @@ for a list of pre-trained models to download. | @@ -136,11 +136,7 @@ for a list of pre-trained models to download. | ||
| 136 | 136 | ||
| 137 | sherpa_onnx::Microphone mic; | 137 | sherpa_onnx::Microphone mic; |
| 138 | 138 | ||
| 139 | - PaDeviceIndex num_devices = Pa_GetDeviceCount(); | ||
| 140 | - fprintf(stderr, "Num devices: %d\n", num_devices); | ||
| 141 | - | ||
| 142 | int32_t device_index = Pa_GetDefaultInputDevice(); | 139 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 143 | - | ||
| 144 | if (device_index == paNoDevice) { | 140 | if (device_index == paNoDevice) { |
| 145 | fprintf(stderr, "No default input device found\n"); | 141 | fprintf(stderr, "No default input device found\n"); |
| 146 | fprintf(stderr, "If you are using Linux, please switch to \n"); | 142 | fprintf(stderr, "If you are using Linux, please switch to \n"); |
| @@ -154,26 +150,8 @@ for a list of pre-trained models to download. | @@ -154,26 +150,8 @@ for a list of pre-trained models to download. | ||
| 154 | device_index = atoi(pDeviceIndex); | 150 | device_index = atoi(pDeviceIndex); |
| 155 | } | 151 | } |
| 156 | 152 | ||
| 157 | - for (int32_t i = 0; i != num_devices; ++i) { | ||
| 158 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 159 | - fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 160 | - info->name); | ||
| 161 | - } | ||
| 162 | - | ||
| 163 | - PaStreamParameters param; | ||
| 164 | - param.device = device_index; | ||
| 165 | - | ||
| 166 | - fprintf(stderr, "Use device: %d\n", param.device); | ||
| 167 | - | ||
| 168 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device); | ||
| 169 | - fprintf(stderr, " Name: %s\n", info->name); | ||
| 170 | - fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 171 | - | ||
| 172 | - param.channelCount = 1; | ||
| 173 | - param.sampleFormat = paFloat32; | 153 | + mic.PrintDevices(device_index); |
| 174 | 154 | ||
| 175 | - param.suggestedLatency = info->defaultLowInputLatency; | ||
| 176 | - param.hostApiSpecificStreamInfo = nullptr; | ||
| 177 | float mic_sample_rate = 16000; | 155 | float mic_sample_rate = 16000; |
| 178 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 156 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 179 | if (pSampleRateStr) { | 157 | if (pSampleRateStr) { |
| @@ -181,24 +159,9 @@ for a list of pre-trained models to download. | @@ -181,24 +159,9 @@ for a list of pre-trained models to download. | ||
| 181 | mic_sample_rate = atof(pSampleRateStr); | 159 | mic_sample_rate = atof(pSampleRateStr); |
| 182 | } | 160 | } |
| 183 | 161 | ||
| 184 | - PaStream *stream; | ||
| 185 | - PaError err = | ||
| 186 | - Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 187 | - mic_sample_rate, | ||
| 188 | - 0, // frames per buffer | ||
| 189 | - paClipOff, // we won't output out of range samples | ||
| 190 | - // so don't bother clipping them | ||
| 191 | - RecordCallback, nullptr); | ||
| 192 | - if (err != paNoError) { | ||
| 193 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 194 | - exit(EXIT_FAILURE); | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - err = Pa_StartStream(stream); | ||
| 198 | - fprintf(stderr, "Started\n"); | ||
| 199 | - | ||
| 200 | - if (err != paNoError) { | ||
| 201 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | 162 | + if (!mic.OpenDevice(device_index, mic_sample_rate, 1, |
| 163 | + RecordCallback, nullptr /* user_data */)){ | ||
| 164 | + fprintf(stderr, "portaudio error: %d\n", device_index); | ||
| 202 | exit(EXIT_FAILURE); | 165 | exit(EXIT_FAILURE); |
| 203 | } | 166 | } |
| 204 | 167 | ||
| @@ -232,11 +195,5 @@ for a list of pre-trained models to download. | @@ -232,11 +195,5 @@ for a list of pre-trained models to download. | ||
| 232 | } | 195 | } |
| 233 | t.join(); | 196 | t.join(); |
| 234 | 197 | ||
| 235 | - err = Pa_CloseStream(stream); | ||
| 236 | - if (err != paNoError) { | ||
| 237 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 238 | - exit(EXIT_FAILURE); | ||
| 239 | - } | ||
| 240 | - | ||
| 241 | return 0; | 198 | return 0; |
| 242 | } | 199 | } |
| @@ -106,11 +106,7 @@ for a list of pre-trained models to download. | @@ -106,11 +106,7 @@ for a list of pre-trained models to download. | ||
| 106 | 106 | ||
| 107 | sherpa_onnx::Microphone mic; | 107 | sherpa_onnx::Microphone mic; |
| 108 | 108 | ||
| 109 | - PaDeviceIndex num_devices = Pa_GetDeviceCount(); | ||
| 110 | - fprintf(stderr, "Num devices: %d\n", num_devices); | ||
| 111 | - | ||
| 112 | int32_t device_index = Pa_GetDefaultInputDevice(); | 109 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 113 | - | ||
| 114 | if (device_index == paNoDevice) { | 110 | if (device_index == paNoDevice) { |
| 115 | fprintf(stderr, "No default input device found\n"); | 111 | fprintf(stderr, "No default input device found\n"); |
| 116 | fprintf(stderr, "If you are using Linux, please switch to \n"); | 112 | fprintf(stderr, "If you are using Linux, please switch to \n"); |
| @@ -124,51 +120,18 @@ for a list of pre-trained models to download. | @@ -124,51 +120,18 @@ for a list of pre-trained models to download. | ||
| 124 | device_index = atoi(pDeviceIndex); | 120 | device_index = atoi(pDeviceIndex); |
| 125 | } | 121 | } |
| 126 | 122 | ||
| 127 | - for (int32_t i = 0; i != num_devices; ++i) { | ||
| 128 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 129 | - fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 130 | - info->name); | ||
| 131 | - } | ||
| 132 | - | ||
| 133 | - PaStreamParameters param; | ||
| 134 | - param.device = device_index; | ||
| 135 | - | ||
| 136 | - fprintf(stderr, "Use device: %d\n", param.device); | ||
| 137 | - | ||
| 138 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device); | ||
| 139 | - fprintf(stderr, " Name: %s\n", info->name); | ||
| 140 | - fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 141 | - | ||
| 142 | - param.channelCount = 1; | ||
| 143 | - param.sampleFormat = paFloat32; | 123 | + mic.PrintDevices(device_index); |
| 144 | 124 | ||
| 145 | - param.suggestedLatency = info->defaultLowInputLatency; | ||
| 146 | - param.hostApiSpecificStreamInfo = nullptr; | 125 | + float mic_sample_rate = 16000; |
| 147 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 126 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 148 | if (pSampleRateStr) { | 127 | if (pSampleRateStr) { |
| 149 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); | 128 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); |
| 150 | mic_sample_rate = atof(pSampleRateStr); | 129 | mic_sample_rate = atof(pSampleRateStr); |
| 151 | } | 130 | } |
| 152 | - float sample_rate = 16000; | ||
| 153 | - | ||
| 154 | - PaStream *stream; | ||
| 155 | - PaError err = | ||
| 156 | - Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 157 | - sample_rate, | ||
| 158 | - 0, // frames per buffer | ||
| 159 | - paClipOff, // we won't output out of range samples | ||
| 160 | - // so don't bother clipping them | ||
| 161 | - RecordCallback, s.get()); | ||
| 162 | - if (err != paNoError) { | ||
| 163 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 164 | - exit(EXIT_FAILURE); | ||
| 165 | - } | ||
| 166 | - | ||
| 167 | - err = Pa_StartStream(stream); | ||
| 168 | - fprintf(stderr, "Started\n"); | ||
| 169 | 131 | ||
| 170 | - if (err != paNoError) { | ||
| 171 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | 132 | + if (!mic.OpenDevice(device_index, mic_sample_rate, 1, |
| 133 | + RecordCallback, nullptr /* user_data */)){ | ||
| 134 | + fprintf(stderr, "portaudio error: %d\n", device_index); | ||
| 172 | exit(EXIT_FAILURE); | 135 | exit(EXIT_FAILURE); |
| 173 | } | 136 | } |
| 174 | 137 | ||
| @@ -213,11 +176,5 @@ for a list of pre-trained models to download. | @@ -213,11 +176,5 @@ for a list of pre-trained models to download. | ||
| 213 | Pa_Sleep(20); // sleep for 20ms | 176 | Pa_Sleep(20); // sleep for 20ms |
| 214 | } | 177 | } |
| 215 | 178 | ||
| 216 | - err = Pa_CloseStream(stream); | ||
| 217 | - if (err != paNoError) { | ||
| 218 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 219 | - exit(EXIT_FAILURE); | ||
| 220 | - } | ||
| 221 | - | ||
| 222 | return 0; | 179 | return 0; |
| 223 | } | 180 | } |
| @@ -113,17 +113,7 @@ to download models for offline ASR. | @@ -113,17 +113,7 @@ to download models for offline ASR. | ||
| 113 | 113 | ||
| 114 | sherpa_onnx::Microphone mic; | 114 | sherpa_onnx::Microphone mic; |
| 115 | 115 | ||
| 116 | - PaDeviceIndex num_devices = Pa_GetDeviceCount(); | ||
| 117 | - fprintf(stderr, "Num devices: %d\n", num_devices); | ||
| 118 | - if (num_devices == 0) { | ||
| 119 | - fprintf(stderr, | ||
| 120 | - " If you are using Linux, please try " | ||
| 121 | - "./build/bin/sherpa-onnx-vad-alsa-offline-asr\n"); | ||
| 122 | - exit(-1); | ||
| 123 | - } | ||
| 124 | - | ||
| 125 | int32_t device_index = Pa_GetDefaultInputDevice(); | 116 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 126 | - | ||
| 127 | if (device_index == paNoDevice) { | 117 | if (device_index == paNoDevice) { |
| 128 | fprintf(stderr, "No default input device found\n"); | 118 | fprintf(stderr, "No default input device found\n"); |
| 129 | fprintf(stderr, | 119 | fprintf(stderr, |
| @@ -137,33 +127,20 @@ to download models for offline ASR. | @@ -137,33 +127,20 @@ to download models for offline ASR. | ||
| 137 | fprintf(stderr, "Use specified device: %s\n", pDeviceIndex); | 127 | fprintf(stderr, "Use specified device: %s\n", pDeviceIndex); |
| 138 | device_index = atoi(pDeviceIndex); | 128 | device_index = atoi(pDeviceIndex); |
| 139 | } | 129 | } |
| 130 | + mic.PrintDevices(device_index); | ||
| 140 | 131 | ||
| 141 | - for (int32_t i = 0; i != num_devices; ++i) { | ||
| 142 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 143 | - fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 144 | - info->name); | ||
| 145 | - } | ||
| 146 | - | ||
| 147 | - PaStreamParameters param; | ||
| 148 | - param.device = device_index; | ||
| 149 | - | ||
| 150 | - fprintf(stderr, "Use device: %d\n", param.device); | ||
| 151 | - | ||
| 152 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device); | ||
| 153 | - fprintf(stderr, " Name: %s\n", info->name); | ||
| 154 | - fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 155 | - | ||
| 156 | - param.channelCount = 1; | ||
| 157 | - param.sampleFormat = paFloat32; | ||
| 158 | - | ||
| 159 | - param.suggestedLatency = info->defaultLowInputLatency; | ||
| 160 | - param.hostApiSpecificStreamInfo = nullptr; | ||
| 161 | float mic_sample_rate = 16000; | 132 | float mic_sample_rate = 16000; |
| 162 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 133 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 163 | if (pSampleRateStr) { | 134 | if (pSampleRateStr) { |
| 164 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); | 135 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); |
| 165 | mic_sample_rate = atof(pSampleRateStr); | 136 | mic_sample_rate = atof(pSampleRateStr); |
| 166 | } | 137 | } |
| 138 | + | ||
| 139 | + if (!mic.OpenDevice(device_index, mic_sample_rate, 1, RecordCallback, nullptr)) { | ||
| 140 | + fprintf(stderr, "Failed to open device %d\n", device_index); | ||
| 141 | + exit(EXIT_FAILURE); | ||
| 142 | + } | ||
| 143 | + | ||
| 167 | float sample_rate = 16000; | 144 | float sample_rate = 16000; |
| 168 | std::unique_ptr<sherpa_onnx::LinearResample> resampler; | 145 | std::unique_ptr<sherpa_onnx::LinearResample> resampler; |
| 169 | if (mic_sample_rate != sample_rate) { | 146 | if (mic_sample_rate != sample_rate) { |
| @@ -175,25 +152,6 @@ to download models for offline ASR. | @@ -175,25 +152,6 @@ to download models for offline ASR. | ||
| 175 | mic_sample_rate, sample_rate, lowpass_cutoff, lowpass_filter_width); | 152 | mic_sample_rate, sample_rate, lowpass_cutoff, lowpass_filter_width); |
| 176 | } | 153 | } |
| 177 | 154 | ||
| 178 | - PaStream *stream; | ||
| 179 | - PaError err = | ||
| 180 | - Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 181 | - mic_sample_rate, | ||
| 182 | - 0, // frames per buffer | ||
| 183 | - paClipOff, // we won't output out of range samples | ||
| 184 | - // so don't bother clipping them | ||
| 185 | - RecordCallback, nullptr); | ||
| 186 | - if (err != paNoError) { | ||
| 187 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 188 | - exit(EXIT_FAILURE); | ||
| 189 | - } | ||
| 190 | - | ||
| 191 | - err = Pa_StartStream(stream); | ||
| 192 | - if (err != paNoError) { | ||
| 193 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 194 | - exit(EXIT_FAILURE); | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | auto vad = std::make_unique<sherpa_onnx::VoiceActivityDetector>(vad_config); | 155 | auto vad = std::make_unique<sherpa_onnx::VoiceActivityDetector>(vad_config); |
| 198 | 156 | ||
| 199 | fprintf(stderr, "Started. Please speak\n"); | 157 | fprintf(stderr, "Started. Please speak\n"); |
| @@ -236,11 +194,5 @@ to download models for offline ASR. | @@ -236,11 +194,5 @@ to download models for offline ASR. | ||
| 236 | Pa_Sleep(100); // sleep for 100ms | 194 | Pa_Sleep(100); // sleep for 100ms |
| 237 | } | 195 | } |
| 238 | 196 | ||
| 239 | - err = Pa_CloseStream(stream); | ||
| 240 | - if (err != paNoError) { | ||
| 241 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 242 | - exit(EXIT_FAILURE); | ||
| 243 | - } | ||
| 244 | - | ||
| 245 | return 0; | 197 | return 0; |
| 246 | } | 198 | } |
| @@ -74,11 +74,8 @@ wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/siler | @@ -74,11 +74,8 @@ wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/siler | ||
| 74 | 74 | ||
| 75 | sherpa_onnx::Microphone mic; | 75 | sherpa_onnx::Microphone mic; |
| 76 | 76 | ||
| 77 | - PaDeviceIndex num_devices = Pa_GetDeviceCount(); | ||
| 78 | - fprintf(stderr, "Num devices: %d\n", num_devices); | ||
| 79 | 77 | ||
| 80 | int32_t device_index = Pa_GetDefaultInputDevice(); | 78 | int32_t device_index = Pa_GetDefaultInputDevice(); |
| 81 | - | ||
| 82 | if (device_index == paNoDevice) { | 79 | if (device_index == paNoDevice) { |
| 83 | fprintf(stderr, "No default input device found\n"); | 80 | fprintf(stderr, "No default input device found\n"); |
| 84 | fprintf(stderr, "If you are using Linux, please switch to \n"); | 81 | fprintf(stderr, "If you are using Linux, please switch to \n"); |
| @@ -91,35 +88,20 @@ wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/siler | @@ -91,35 +88,20 @@ wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/siler | ||
| 91 | fprintf(stderr, "Use specified device: %s\n", pDeviceIndex); | 88 | fprintf(stderr, "Use specified device: %s\n", pDeviceIndex); |
| 92 | device_index = atoi(pDeviceIndex); | 89 | device_index = atoi(pDeviceIndex); |
| 93 | } | 90 | } |
| 91 | + mic.PrintDevices(device_index); | ||
| 94 | 92 | ||
| 95 | - for (int32_t i = 0; i != num_devices; ++i) { | ||
| 96 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(i); | ||
| 97 | - fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i, | ||
| 98 | - info->name); | ||
| 99 | - } | ||
| 100 | - | ||
| 101 | - PaStreamParameters param; | ||
| 102 | - param.device = device_index; | ||
| 103 | - | ||
| 104 | - fprintf(stderr, "Use device: %d\n", param.device); | ||
| 105 | - | ||
| 106 | - const PaDeviceInfo *info = Pa_GetDeviceInfo(param.device); | ||
| 107 | - fprintf(stderr, " Name: %s\n", info->name); | ||
| 108 | - fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels); | ||
| 109 | - | ||
| 110 | - param.channelCount = 1; | ||
| 111 | - param.sampleFormat = paFloat32; | ||
| 112 | - | ||
| 113 | - param.suggestedLatency = info->defaultLowInputLatency; | ||
| 114 | - param.hostApiSpecificStreamInfo = nullptr; | ||
| 115 | float mic_sample_rate = 16000; | 93 | float mic_sample_rate = 16000; |
| 116 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); | 94 | const char *pSampleRateStr = std::getenv("SHERPA_ONNX_MIC_SAMPLE_RATE"); |
| 117 | if (pSampleRateStr) { | 95 | if (pSampleRateStr) { |
| 118 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); | 96 | fprintf(stderr, "Use sample rate %f for mic\n", mic_sample_rate); |
| 119 | mic_sample_rate = atof(pSampleRateStr); | 97 | mic_sample_rate = atof(pSampleRateStr); |
| 120 | } | 98 | } |
| 121 | - float sample_rate = 16000; | 99 | + if (!mic.OpenDevice(device_index, mic_sample_rate, 1, RecordCallback, nullptr)) { |
| 100 | + fprintf(stderr, "Failed to open microphone device %d\n", device_index); | ||
| 101 | + exit(EXIT_FAILURE); | ||
| 102 | + } | ||
| 122 | 103 | ||
| 104 | + float sample_rate = 16000; | ||
| 123 | std::unique_ptr<sherpa_onnx::LinearResample> resampler; | 105 | std::unique_ptr<sherpa_onnx::LinearResample> resampler; |
| 124 | if (mic_sample_rate != sample_rate) { | 106 | if (mic_sample_rate != sample_rate) { |
| 125 | float min_freq = std::min(mic_sample_rate, sample_rate); | 107 | float min_freq = std::min(mic_sample_rate, sample_rate); |
| @@ -130,30 +112,8 @@ wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/siler | @@ -130,30 +112,8 @@ wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/siler | ||
| 130 | mic_sample_rate, sample_rate, lowpass_cutoff, lowpass_filter_width); | 112 | mic_sample_rate, sample_rate, lowpass_cutoff, lowpass_filter_width); |
| 131 | } | 113 | } |
| 132 | 114 | ||
| 133 | - PaStream *stream; | ||
| 134 | - PaError err = | ||
| 135 | - Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */ | ||
| 136 | - mic_sample_rate, | ||
| 137 | - 0, // frames per buffer | ||
| 138 | - paClipOff, // we won't output out of range samples | ||
| 139 | - // so don't bother clipping them | ||
| 140 | - RecordCallback, nullptr); | ||
| 141 | - if (err != paNoError) { | ||
| 142 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 143 | - exit(EXIT_FAILURE); | ||
| 144 | - } | ||
| 145 | - | ||
| 146 | - err = Pa_StartStream(stream); | ||
| 147 | - | ||
| 148 | auto vad = std::make_unique<sherpa_onnx::VoiceActivityDetector>(config); | 115 | auto vad = std::make_unique<sherpa_onnx::VoiceActivityDetector>(config); |
| 149 | 116 | ||
| 150 | - fprintf(stderr, "Started\n"); | ||
| 151 | - | ||
| 152 | - if (err != paNoError) { | ||
| 153 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 154 | - exit(EXIT_FAILURE); | ||
| 155 | - } | ||
| 156 | - | ||
| 157 | int32_t window_size = config.silero_vad.window_size; | 117 | int32_t window_size = config.silero_vad.window_size; |
| 158 | bool printed = false; | 118 | bool printed = false; |
| 159 | 119 | ||
| @@ -202,11 +162,5 @@ wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/siler | @@ -202,11 +162,5 @@ wget https://github.com/snakers4/silero-vad/raw/master/src/silero_vad/data/siler | ||
| 202 | Pa_Sleep(100); // sleep for 100ms | 162 | Pa_Sleep(100); // sleep for 100ms |
| 203 | } | 163 | } |
| 204 | 164 | ||
| 205 | - err = Pa_CloseStream(stream); | ||
| 206 | - if (err != paNoError) { | ||
| 207 | - fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err)); | ||
| 208 | - exit(EXIT_FAILURE); | ||
| 209 | - } | ||
| 210 | - | ||
| 211 | return 0; | 165 | return 0; |
| 212 | } | 166 | } |
-
请 注册 或 登录 后发表评论