pvrecorder/sdk/python/demo.py
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 
13 import argparse
14 import struct
15 import wave
16 from pvrecorder import PvRecorder
17 
18 
19 def main():
20  parser = argparse.ArgumentParser()
21 
22  parser.add_argument(
23  "--show_audio_devices",
24  help="List of audio devices currently available for use.",
25  action="store_true")
26 
27  parser.add_argument(
28  "--audio_device_index",
29  help="Index of input audio device.",
30  type=int,
31  default=-1)
32 
33  parser.add_argument(
34  "--output_path",
35  help="Path to file to store raw audio.",
36  default=None)
37 
38  args = parser.parse_args()
39 
40  if args.show_audio_devices:
41  devices = PvRecorder.get_audio_devices()
42  for i in range(len(devices)):
43  print(f"index: {i}, device name: {devices[i]}")
44  else:
45  device_index = args.audio_device_index
46  output_path = args.output_path
47 
48  recorder = PvRecorder(device_index=device_index, frame_length=512)
49  print(f"pvrecorder.py version: {recorder.version}")
50 
51  recorder.start()
52  print(f"Using device: {recorder.selected_device}")
53 
54  if output_path is not None:
55  wavfile = wave.open(output_path, "w")
56  wavfile.setparams((1, 2, 16000, 512, "NONE", "NONE"))
57 
58  try:
59  while True:
60  pcm = recorder.read()
61  if output_path is not None:
62  wavfile.writeframes(struct.pack("h" * len(pcm), *pcm))
63 
64  except KeyboardInterrupt:
65  print("Stopping...")
66  finally:
67  recorder.delete()
68  if output_path is not None:
69  wavfile.close()
70 
71 
72 if __name__ == "__main__":
73  main()
python.demo.main
def main()
Definition: porcupine/demo/c/pvrecorder/sdk/python/demo.py:19
main
Definition: main.py:1


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