달력

5

« 2024/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2016. 2. 8. 04:51

사운드 보드(파이썬) 라즈베리파이/GPIO2016. 2. 8. 04:51

이제 버튼을 누르면 녹음된 소리를 재생해주는 사운드 보드를 만들어 보자.

사운드 보드를 만드려면 .wav형식의 소리파일이 필요하다.

여러가지 재미있는 소리를 첨부파일에 올려놓았으니 이용하세요~~

 

 

1. 홈 디렉터리에 새로운 디렉터리를 만든다. 디렉터리명은 soundboard

만든 폴더에 새로운 파일 하나를 만든다. soundboard.py

첨부한 소리파일도 soundboard 디렉터리에 넣는다.

 

2. soundboard.py파일을 열고 코드 입력

import pygame.mixer

from time import sleep

import RPi.GPIO as GPIO

from sys import exit

 

GPIO.setmode(GPIO.BCM)

GPIO.setup(25, GPIO.IN)

GPIO.setup(24, GPIO.IN)

GPIO.setup(23, GPIO.IN)

GPIO.setup(18, GPIO.IN)

GPIO.setup(22, GPIO.IN)

GPIO.setup(27, GPIO.IN)

GPIO.setup(17, GPIO.IN)

 

pygame.mixer.init(48000, -16, 1, 1024) //파이게임의 믹서 초기화

 

soundA = pygame.mixer.Sound("/home/pi/soundboard/robot_explode.wav") //소리 로드

soundB = pygame.mixer.Sound("/home/pi/soundboard/drumsolo.wav")

soundC = pygame.mixer.Sound("/home/pi/soundboard/laugh.wav")

soundD = pygame.mixer.Sound("/home/pi/soundboard/playgame.wav")

soundE = pygame.mixer.Sound("/home/pi/soundboard/drumLick.wav")

soundF = pygame.mixer.Sound("/home/pi/soundboard/piano4.wav")

soundG = pygame.mixer.Sound("/home/pi/soundboard/drum1.wav")

 

soundChannelA = pygame.mixer.Channel(1) //7개채널설정. 서로 다른 소리를 동시에 재생할 수 있도록 소리 하나에 채널 하나씩

soundChannelB = pygame.mixer.Channel(2)

soundChannelC = pygame.mixer.Channel(3)

soundChannelD = pygame.mixer.Channel(4)

soundChannelE = pygame.mixer.Channel(5)

soundChannelF = pygame.mixer.Channel(6)

soundChannelG = pygame.mixer.Channel(7)

 

print "Soundboard Ready." //사운드보드의 준비 완료 상태 표시

 

while True:

     try:

        if(GPIO.input(25) == True): //핀에 HIGH 신호가 입력되면 다음 행 실행

            soundChannelA.play(soundA) //소리 재생

        if(GPIO.input(24) == True):

            soundChannelA.play(soundB)

        if(GPIO.input(23) == True):

            soundChannelA.play(soundC)

        if(GPIO.input(18) == True):

            soundChannelA.play(soundD)

        if(GPIO.input(22) == True):

            soundChannelA.play(soundE)

        if(GPIO.input(27) == True):

            soundChannelA.play(soundF)

        if(GPIO.input(17) == True):

            soundChannelA.play(soundG)

        sleep(.01)

     except KeyboardInterrupt: //사용자가 Ctrl + C를 누를 때 아무런 메시지도 보여주지 않고 깔끔하게 스크립트가 종료됨

         exit()

 

3. 커맨드라인에서 cd /home/pi/soundboard 를 입력하여 soundboard.py를 저장한 폴더로 이동하여 소스파일을 실행한다.

pi@ raspberrypi ~ /soundboard $ sudo python soundboard.py

 

4. "Soundboard Ready"라는 메시지 확인 후 버튼을 누르면 샘플 소리 재생

 

5. 라즈베리 파이 설정에 따라 소리파일 출력이 HDMI를 통해 출력 될수도 있고 오디오 잭을 통해 출력 될 수도 있다.

출력 방향을 변경하려면 Ctrl + C를 눌러 스크립트를 빠져나와서 다음 명령을 실행하면 오디오 출력 단자를 사용할 수 있다.

pi@ raspberrypi ~/soundboard $ sudo amixer cset numid=3 1

오디오를 HDMI모니터로 출력하려면 다음 구문 입력

pi@ raspberrypi ~/soundboard $ sudo amixer cset numid=3 2

:
Posted by youjin.A