본문 바로가기

코딩/파이썬과 라즈베리파이

[Raspberry Pi] 라즈베리 파이 + 파이썬 코딩 021. pygame 라이브러리로 wav 오디오 재생

pygame 라이브러리

pygame 라이브러리는 SDL을 기반으로 하는 (주로) 2D 게임 개발을 위한 파이썬 라이브러리이다.

 

언젠가 나만의 인디 게임을 만들어 보겠단 생각으로 한동안 pygame을 파보다가 음악과 그림의 벽에 막혀서 ...

 

pygame 기반 작품 중에서 DafluffyPotato의 작품이 완성도가 높다고 생각한다. 

https://dafluffypotato.com/

 

 

DaFluffyPotato

game developer, pixel artist, tutorialist, and computer science enthusiast developing games since 2013

dafluffypotato.com

재생할 wav 오디오 파일

라즈베리 파이 아래의 경로에서 소리 파일을 쉽게 찾을 수 있다.

 

  • /usr/share/python_games
  • /usr/share/scratch/Media/Sounds

코드 작성

아래는 Kitten.wav 오디오 파일을 1.5초 간격으로 무한 재생하는 코드이다.

 

import pygame.mixer
from pygame.mixer import Sound
from time import sleep

pygame.mixer.init()
wav_sound = Sound('/usr/share/scratch/Media/Sounds/Animal/Kitten.wav')

while True:
    wav_sound.play()
    sleep(1.5)