Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 30, 2021 07:34 pm GMT

Turn your keyboard into a sound FX board with pygame

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 pygamepygame.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 = Trueif __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 pygamepygame.init()from pygame import mixerrunning = Trueif __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 pygamepygame.init()from pygame import mixerrunning = Trueif __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

minecraft in python

That's it for this tutorial!

Byeeeee


Original Link: https://dev.to/code_jedi/turn-your-keyboard-into-a-sound-fx-board-with-pygame-5b8m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To