raspberry pi 3 에 stretch 그 3

개요
raspberry pi 3에서 stretch 해 보았습니다.
USB 오디오 톤글 해봤다.

사진


환경
Raspberry Pi 3 모델 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)

이상.

좋은 웹페이지 즐겨찾기