rhino/demo/c/pvrecorder/example/demo.c
Go to the documentation of this file.
1 /*
2  Copyright 2021 Picovoice Inc.
3 
4  You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5  file accompanying this source.
6 
7  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8  an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  specific language governing permissions and limitations under the License.
10 */
11 
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include "pv_recorder.h"
18 
19 static volatile bool is_interrupted = false;
20 
21 void interrupt_handler(int _) {
22  (void) _;
23  is_interrupted = true;
24 }
25 
26 static void print_usage(const char *program) {
27  fprintf(stderr, "usage: %s --show_audio_devices\n"
28  " %s audio_device_index path_to_raw_file\n", program, program);
29 }
30 
31 int main(int argc, char *argv[]) {
32  fprintf(stdout, "pv_recorder version: %s\n", pv_recorder_version());
33  if ((argc != 2) && (argc != 3)) {
34  print_usage(argv[0]);
35  exit(1);
36  }
37 
38  signal(SIGINT, interrupt_handler);
39 
40  if (strcmp(argv[1], "--show_audio_devices") == 0) {
41  char **devices = NULL;
42  int32_t count = 0;
43 
44  // List devices
46  if (status != PV_RECORDER_STATUS_SUCCESS) {
47  fprintf(stderr, "Failed to get audio devices with: %s.\n", pv_recorder_status_to_string(status));
48  exit(1);
49  }
50 
51  fprintf(stdout, "Printing devices...\n");
52  for (int32_t i = 0; i < count; i++) {
53  fprintf(stdout, "index: %d, name: %s\n", i, devices[i]);
54  }
55 
57  return 0;
58  }
59 
60  const int32_t device_index = (int32_t) strtol(argv[1], NULL, 10);
61  const char *path_to_raw_file = NULL;
62 
63  if (argc == 3) {
64  path_to_raw_file = argv[2];
65  }
66 
67  // Use PV_Recorder
68  fprintf(stdout, "Initializing pv_recorder...\n");
69 
70  const int32_t frame_length = 512;
71  pv_recorder_t *recorder = NULL;
72  pv_recorder_status_t status = pv_recorder_init(device_index, frame_length, 100, true, &recorder);
73  if (status != PV_RECORDER_STATUS_SUCCESS) {
74  fprintf(stderr, "Failed to initialize device with %s.\n", pv_recorder_status_to_string(status));
75  exit(1);
76  }
77 
78  const char *selected_device = pv_recorder_get_selected_device(recorder);
79  fprintf(stdout, "Selected device: %s.\n", selected_device);
80 
81  fprintf(stdout, "Start recording...\n");
82  status = pv_recorder_start(recorder);
83  if (status != PV_RECORDER_STATUS_SUCCESS) {
84  fprintf(stderr, "Failed to start device with %s.\n", pv_recorder_status_to_string(status));
85  exit(1);
86  }
87 
88  int16_t *pcm = malloc(frame_length * sizeof(int16_t));
89  if (!pcm) {
90  fprintf(stderr, "Failed to allocate pcm memory.\n");
91  exit(1);
92  }
93 
94  FILE *file = NULL;
95  if (path_to_raw_file) {
96  file = fopen(path_to_raw_file, "wb");
97  if (!file) {
98  fprintf(stderr, "Failed to open file.\n");
99  exit(1);
100  }
101  }
102 
103  while (!is_interrupted) {
104  status = pv_recorder_read(recorder, pcm);
105  if (status != PV_RECORDER_STATUS_SUCCESS) {
106  fprintf(stderr, "Failed to read with %s.\n", pv_recorder_status_to_string(status));
107  exit(1);
108  }
109  if (file) {
110  const size_t length = fwrite(pcm, sizeof(int16_t), frame_length, file);
111  if (length != frame_length) {
112  fprintf(stderr, "Failed to write raw bytes to file.\n");
113  exit(1);
114  }
115  }
116  }
117 
118  if (file) {
119  fclose(file);
120  }
121 
122  fprintf(stdout, "Stop recording...\n");
123  status = pv_recorder_stop(recorder);
124  if (status != PV_RECORDER_STATUS_SUCCESS) {
125  fprintf(stderr, "Failed to stop device with %s.\n", pv_recorder_status_to_string(status));
126  exit(1);
127  }
128 
129  fprintf(stdout, "Deleting pv_recorder...\n");
130  pv_recorder_delete(recorder);
131  free(pcm);
132 
133  return 0;
134 }
main
int main(int argc, char *argv[])
Definition: rhino/demo/c/pvrecorder/example/demo.c:31
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
pv_recorder_delete
PV_API void pv_recorder_delete(pv_recorder_t *object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:162
pv_recorder_init
PV_API pv_recorder_status_t pv_recorder_init(int32_t device_index, int32_t frame_length, int32_t buffer_size_msec, bool log_overflow, pv_recorder_t **object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:51
pv_recorder_free_device_list
PV_API void pv_recorder_free_device_list(int32_t count, char **devices)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:314
pv_recorder_read
PV_API pv_recorder_status_t pv_recorder_read(pv_recorder_t *object, int16_t *pcm)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:213
is_interrupted
static volatile bool is_interrupted
Definition: rhino/demo/c/pvrecorder/example/demo.c:19
pv_recorder_stop
PV_API pv_recorder_status_t pv_recorder_stop(pv_recorder_t *object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:192
pv_recorder_status_t
pv_recorder_status_t
Definition: porcupine/demo/c/pvrecorder/include/pv_recorder.h:31
pv_recorder_get_selected_device
const PV_API char * pv_recorder_get_selected_device(pv_recorder_t *object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:249
count
size_t count
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_common/ma_test_common.c:31
pv_recorder
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:28
pv_recorder_version
const PV_API char * pv_recorder_version(void)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:343
interrupt_handler
void interrupt_handler(int _)
Definition: rhino/demo/c/pvrecorder/example/demo.c:21
print_usage
static void print_usage(const char *program)
Definition: rhino/demo/c/pvrecorder/example/demo.c:26
python.test_porcupine.argv
argv
Definition: test_porcupine.py:158
pv_recorder_status_to_string
const PV_API char * pv_recorder_status_to_string(pv_recorder_status_t status)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:323
pv_recorder_start
PV_API pv_recorder_status_t pv_recorder_start(pv_recorder_t *object)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:172
PV_RECORDER_STATUS_SUCCESS
@ PV_RECORDER_STATUS_SUCCESS
Definition: porcupine/demo/c/pvrecorder/include/pv_recorder.h:32
pv_recorder_get_audio_devices
PV_API pv_recorder_status_t pv_recorder_get_audio_devices(int32_t *count, char ***devices)
Definition: porcupine/demo/c/pvrecorder/src/pv_recorder.c:256


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:48