forked from synesthesiam/hassio-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added arecord audio system and settings
- Loading branch information
1 parent
78221cc
commit ebad05d
Showing
11 changed files
with
134 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import os | ||
import re | ||
import subprocess | ||
import tempfile | ||
|
||
class AudioRecorder: | ||
def start_recording(self, profile): | ||
pass | ||
|
||
def stop_recording(self): | ||
return [] | ||
|
||
def get_microphones(self): | ||
return {} | ||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
class PyAudioRecorder(AudioRecorder): | ||
def __init__(self): | ||
self.audio = None | ||
self.mic = None | ||
self.buffer = None | ||
|
||
def start_recording(self, device_index=None): | ||
import pyaudio | ||
|
||
def stream_callback(data, frame_count, time_info, status): | ||
if self.buffer is None: | ||
self.buffer = data | ||
else: | ||
self.buffer += data | ||
|
||
return (data, pyaudio.paContinue) | ||
|
||
self.audio = pyaudio.PyAudio() | ||
data_format = self.audio.get_format_from_width(2) # 16-bit | ||
self.mic = self.audio.open(format=data_format, | ||
channels=1, | ||
rate=16000, | ||
input_device_index=device_index, | ||
input=True, | ||
stream_callback=stream_callback) | ||
|
||
self.mic.start_stream() | ||
|
||
def stop_recording(self): | ||
self.mic.stop_stream() | ||
self.audio.terminate() | ||
|
||
return self.buffer | ||
|
||
def get_microphones(self): | ||
import pyaudio | ||
|
||
mics = {} | ||
audio = pyaudio.PyAudio() | ||
for i in range(audio.get_device_count()): | ||
info = audio.get_device_info_by_index(i) | ||
mics[i] = info['name'] | ||
|
||
audio.terminate() | ||
|
||
return mics | ||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
class ARecordAudioRecorder(AudioRecorder): | ||
def start_recording(self, profile): | ||
self.record_file = tempfile.NamedTemporaryFile(suffix='.wav', mode='wb+') | ||
self.record_proc = subprocess.Popen(['arecord', | ||
'-r', '16000', | ||
'-f', 'S16_LE', | ||
'-c', '1', | ||
'-t', 'wav', | ||
self.record_file.name], | ||
close_fds=True) | ||
|
||
def stop_recording(self): | ||
self.record_proc.terminate() | ||
self.record_file.seek(0) | ||
data = open(self.record_file.name, 'rb').read() | ||
try: | ||
os.unlink(self.record_file.name) | ||
except: | ||
pass | ||
|
||
return data | ||
|
||
def get_microphones(self): | ||
output = subprocess.check_output(['arecord', '-L'])\ | ||
.decode().splitlines() | ||
|
||
mics = {} | ||
name, description = None, None | ||
|
||
# Parse output of arecord -L | ||
for line in output: | ||
line = line.rstrip() | ||
if re.match(r'^\s', line): | ||
description = line.strip() | ||
else: | ||
if name is not None: | ||
mics[name] = description | ||
|
||
name = line.strip() | ||
|
||
return mics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=/img/favicon.png><link rel=stylesheet href=/css/bootstrap.min.css><link rel=stylesheet href=/css/fontawesome-all.min.css><link rel=stylesheet href=/css/main.css><title>Rhasspy Voice Assistant</title><link href=/css/app.acd79e7b.css rel=preload as=style><link href=/js/app.facf817a.js rel=preload as=script><link href=/js/chunk-vendors.fd9734c4.js rel=preload as=script><link href=/css/app.acd79e7b.css rel=stylesheet></head><body><noscript><strong>Please enable Javascript to continue.</strong></noscript><div id=app></div><script src=/js/jquery-3.3.1.slim.min.js></script><script src=/js/axios.min.js></script><script src=/js/vue-axios.min.js></script><script src=/js/bootstrap.min.js></script><script src=/js/chunk-vendors.fd9734c4.js></script><script src=/js/app.facf817a.js></script></body></html> | ||
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=/img/favicon.png><link rel=stylesheet href=/css/bootstrap.min.css><link rel=stylesheet href=/css/fontawesome-all.min.css><link rel=stylesheet href=/css/main.css><title>Rhasspy Voice Assistant</title><link href=/css/app.778eaa10.css rel=preload as=style><link href=/js/app.75eca726.js rel=preload as=script><link href=/js/chunk-vendors.fd9734c4.js rel=preload as=script><link href=/css/app.778eaa10.css rel=stylesheet></head><body><noscript><strong>Please enable Javascript to continue.</strong></noscript><div id=app></div><script src=/js/jquery-3.3.1.slim.min.js></script><script src=/js/axios.min.js></script><script src=/js/vue-axios.min.js></script><script src=/js/bootstrap.min.js></script><script src=/js/chunk-vendors.fd9734c4.js></script><script src=/js/app.75eca726.js></script></body></html> |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters