0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry pi 1 model bで、with pixel その5

Last updated at Posted at 2017-02-12

概要

Raspberry pi 1 model bで、USBオーディオトングルやってみた。

写真

MVC-012S.JPG

環境

Raspberry pi 1 model b(element14)
raspbian 2016-11-25 jessie with pixel
sd_card MF-RUMSD08GL elecom
HDMI to VGA adapter(amazon)
usb keyboard(nec)
usb mouse(nec)
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.
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

再生

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

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 = 2, 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)
data = wf.readframes(CHUNK)
while data != '':
    stream.write(data)
    data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?