概要
raspberry pi 3 でstretchやってみた。
USBオーディオトングルやってみた。
環境
Raspberry Pi 3 model B v1.2 element14
2017-09-07-raspbian-stretch
usb audio C-Media Electronics
接続確認
lsusb
Bus 001 Device 007: ID 03ee:6402 Mitsumi
Bus 001 Device 006: ID 0409:0019 NEC Corp. 109 Japanese Keyboard with Bus-Powered Hub
Bus 001 Device 005: ID 0409:55aa NEC Corp. Hub
Bus 001 Device 004: ID 0d8c:000c C-Media Electronics, Inc. Audio Adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
カード、デバイスの確認
aplay -l
**** ハードウェアデバイス PLAYBACK のリスト ****
カード 0: ALSA [bcm2835 ALSA], デバイス 0: bcm2835 ALSA [bcm2835 ALSA]
サブデバイス: 8/8
サブデバイス #0: subdevice #0
サブデバイス #1: subdevice #1
サブデバイス #2: subdevice #2
サブデバイス #3: subdevice #3
サブデバイス #4: subdevice #4
サブデバイス #5: subdevice #5
サブデバイス #6: subdevice #6
サブデバイス #7: subdevice #7
カード 0: ALSA [bcm2835 ALSA], デバイス 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
サブデバイス: 1/1
サブデバイス #0: subdevice #0
カード 1: Set [C-Media USB Headphone Set], デバイス 0: USB Audio [USB Audio]
サブデバイス: 1/1
サブデバイス #0: subdevice #0
arecord -l
**** ハードウェアデバイス CAPTURE のリスト ****
カード 0: Set [C-Media USB Headphone Set], デバイス 0: USB Audio [USB Audio]
サブデバイス: 1/1
サブデバイス #0: subdevice #0
再生
aplay -D plughw:1,0 test.wav
音量
amixer sset Mic 20 -c 1
録音
arecord -f S16_LE -D plughw:1,0 test.wav
pyaudioのインストール
sudo aptitude install python-pyaudio
設定
sudo nano /etc/modprobe.d/sound.conf
options snd_usb_audio index=0
options snd_hda_intel index=1
確認
ブラウザでyoutube再生OKです。
pyaudioで録音
import pyaudio
import wave
chunk = 4096
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = input('time ')
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, input_device_index = 0, frames_per_buffer = chunk)
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
stream.close()
data = ''.join(all)
out = wave.open('test.wav','w')
out.setnchannels(1)
out.setsampwidth(2)
out.setframerate(RATE)
out.writeframes(data)
out.close()
p.terminate()
pyaudioで再生
import pyaudio
import wave
CHUNK = 4096
filename = "test.wav"
wf = wave.open(filename, 'rb')
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(wf.getsampwidth()), channels = wf.getnchannels(), rate = wf.getframerate(), output = True, ouput_device_index = 0)
data = wf.readframes(CHUNK)
while data != '':
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
リアルタイム信号処理
import pyaudio
CHUNK = 1024 * 8
RATE = 44100
p = pyaudio.PyAudio()
stream = p.open(format = pyaudio.paInt16, channels = 1, rate = RATE, frames_per_buffer = CHUNK, input = True, output = True, input_device_index = 0, ouput_device_index = 0)
def filter(input):
ret = input
return ret
while stream.is_active():
input = stream.read(CHUNK)
input = filter(input)
output = stream.write(input)
以上。