This content originally appeared on DEV Community and was authored by Code_Jedi
Before we start, I'm just going to say that this obviously isn't supposed to be a tutorial that will help you improve your python skills, and I'm not intending it to be, I intend this to be a tutorial on how to build a fun beginner project.
Let's get started!
First, your going to need to install pygame, a python library used for making games, sound effects and more...
pip3 install pygame
Next, create a python file and put these lines of code at the beginning:
import pygame
pygame.init()
from pygame import mixer
- This code first imports the pygame library, initiates it and finally imports mixer from pygame
Before you start using mixer to create sound effects with python, you're going to need to put these lines of code first:
running = True
if __name__ == "__main__":
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Let me explain...
- The first 3 lines of code will make sure that your python script will run forever
- _The last 3 lines of code are then going to make sure to contradict the
while running:
loop only if the user willingly exits from the python launcher window
Now you can start making sounds in python!
For this tutorial I'm going to be using the "noice" sound effect which I downloaded from https://www.voicy.network/clips/7QSw-IHgGkKvHmhECMVrtQ.
After you've downloaded your sound, add these lines of code into the for event in pygame.event.get():
loop like so:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
press = pygame.key.get_pressed()
if press[pygame.K_n]:
b = mixer.Sound("Noice.mp3")
b.play()
Let me explain...
- "press" is going to detect keyboard inputs from the user
- _ Next, the
if press[pygame.K_n]:
statement is going to detect if the user has pressed the "n" key on their keyboard. If they have, your python script is going to play the specified sound effect using mixer's "Sound" function.
Time to test!
Here's what your code should look like so far:
import pygame
pygame.init()
from pygame import mixer
running = True
if __name__ == "__main__":
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
press = pygame.key.get_pressed()
if press[pygame.K_n]:
b = mixer.Sound("Noice.mp3")
b.play()
Now if you run your code, you should be able to play your sound effect when pressing a key on your keyboard.
You can then add more keys and sounds to turn your keyboard into a full sound FX board:
import pygame
pygame.init()
from pygame import mixer
running = True
if __name__ == "__main__":
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
press = pygame.key.get_pressed()
if press[pygame.K_n]:
b = mixer.Sound("sound1.mp3")
b.play()
if press[pygame.K_k]:
b = mixer.Sound("sound2.mp3")
b.play()
if press[pygame.K_y]:
b = mixer.Sound("sound3.mp3")
b.play()
if press[pygame.K_t]:
b = mixer.Sound("sound4.mp3")
b.play()
if press[pygame.K_r]:
b = mixer.Sound("sound5.mp3")
b.play()
if press[pygame.K_f]:
b = mixer.Sound("sound6.mp3")
b.play()
If you're a beginner who likes discovering new things about python, try my weekly python newsletter
That's it for this tutorial!
Byeeeee?
This content originally appeared on DEV Community and was authored by Code_Jedi
Code_Jedi | Sciencx (2021-07-30T19:34:19+00:00) Turn your keyboard into a sound FX board with pygame. Retrieved from https://www.scien.cx/2021/07/30/turn-your-keyboard-into-a-sound-fx-board-with-pygame/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.