microphone.cc
2.8 KB
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
// sherpa-onnx/csrc/microphone.cc
//
// Copyright (c) 2022-2023 Xiaomi Corporation
#include "sherpa-onnx/csrc/microphone.h"
#include <stdio.h>
#include <stdlib.h>
namespace sherpa_onnx {
Microphone::Microphone() {
PaError err = Pa_Initialize();
if (err != paNoError) {
fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err));
exit(-1);
}
}
Microphone::~Microphone() {
CloseDevice();
PaError err = Pa_Terminate();
if (err != paNoError) {
fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err));
}
}
int Microphone::GetDeviceCount() const { return Pa_GetDeviceCount(); }
int Microphone::GetDefaultInputDevice() const {
return Pa_GetDefaultInputDevice();
}
void Microphone::PrintDevices(int device_index) const {
int num_devices = Pa_GetDeviceCount();
fprintf(stderr, "Num devices: %d\n", num_devices);
for (int i = 0; i != num_devices; ++i) {
const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
fprintf(stderr, " %s %d %s\n", (i == device_index) ? "*" : " ", i,
info->name);
}
}
bool Microphone::OpenDevice(int index, int sample_rate, int channel,
PaStreamCallback cb, void *userdata) {
if (index < 0 || index >= Pa_GetDeviceCount()) {
fprintf(stderr, "Invalid device index: %d\n", index);
return false;
}
const PaDeviceInfo *info = Pa_GetDeviceInfo(index);
if (!info) {
fprintf(stderr, "No device info found for index: %d\n", index);
return false;
}
CloseDevice();
fprintf(stderr, "Use device: %d\n", index);
fprintf(stderr, " Name: %s\n", info->name);
fprintf(stderr, " Max input channels: %d\n", info->maxInputChannels);
PaStreamParameters param;
param.device = index;
param.channelCount = channel;
param.sampleFormat = paFloat32;
param.suggestedLatency = info->defaultLowInputLatency;
param.hostApiSpecificStreamInfo = nullptr;
PaError err =
Pa_OpenStream(&stream, ¶m, nullptr, /* &outputParameters, */
sample_rate,
0, // frames per buffer
paClipOff, // we won't output out of range samples
// so don't bother clipping them
cb, userdata);
if (err != paNoError) {
fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err));
return false;
}
err = Pa_StartStream(stream);
fprintf(stderr, "Started\n");
if (err != paNoError) {
fprintf(stderr, "portaudio error: %s\n", Pa_GetErrorText(err));
CloseDevice();
return false;
}
return true;
}
void Microphone::CloseDevice() {
if (stream) {
PaError err = Pa_CloseStream(stream);
if (err != paNoError) {
fprintf(stderr, "Pa_CloseStream error: %s\n", Pa_GetErrorText(err));
}
stream = nullptr;
}
}
} // namespace sherpa_onnx