Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

Pygame playing sounds without input...

Mon Jun 10, 2019 4:06 pm

Hello!
I am working on a python script that involves Pygame and I am stumbling upon something... This is the code----

Code: Select all

import time
import os
import rpi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(29,GPIO.IN)
GPIO.setup(31,GPIO.IN)
GPIO.setup(32,GPIO.IN)
GPIO.setup(33,GPIO.IN)
GPIO.setup(35,GPIO.IN)
GPIO.setup(36,GPIO.IN)
GPIO.setup(37,GPIO.IN)
GPIO.setup(38,GPIO.IN)

import pygame
pygame.mixer.init(44100,-16,8,2048)

Drum1 = ('/home/pi/Drum1.wav')
Drum2 = ('/home/pi/Drum2.wav')
Drum3 = ('/home/pi/DrumSp.wav')
BassDrum = ('/home/pi/BassDrum.wav')
Cym1 = ('/home/pi/Cymbal1.wav')
Cym2 = ('/home/pi/Cymbal2.wav')
Cym3 = ('/home/pi/Cymbal3.wav')
Cym4 = ('/home/pi/Cymbal4.wav')

ch1 = pygame.mixer.Channel(0)
ch2 = pygame.mixer.Channel(1)
ch3 = pygame.mixer.Channel(2)
ch4 = pygame.mixer.Channel(3)
ch5 = pygame.mixer.Channel(4)
ch6 = pygame.mixer.Channel(5)
ch7 = pygame.mixer.Channel(6)
ch8 = pygame.mixer.Channel(7)

try:
    while True:
        while True:
            if GPIO.input(29) ==1:
        ch1.play(pygame.mixer.Sound(Drum1))
                time.sleep(0.02)
                break
            if GPIO.input(31) ==1:
        ch2.play(pygame.mixer.Sound(Drum2))
                time.sleep(0.02)
                break
            if GPIO.input(32) ==1:
        ch3.play(pygame.mixer.Sound(Drum3))
                time.sleep(0.02)
                break
            if GPIO.input(33) ==1:
          ch4.play(pygame.mixer.Sound(Cym1))
                time.sleep(0.02)
                break
            if GPIO.input(35) ==1:
          ch5.play(pygame.mixer.Sound(Cym2))
                time.sleep(0.02)
                break
            if GPIO.input(36) ==1:
          ch6.play(pygame.mixer.Sound(Cym3))
                time.sleep(0.02)
                break
            if GPIO.input(37) ==1:
          ch7.play(pygame.mixer.Sound(Cym4))
                time.sleep(0.02)
                break
            if GPIO.input(38) ==1: ch8.play(pygame.mixer.Sound(BassDrum))
                time.sleep(0.02)
                break

finally:
    GPIO.cleanup()
When I run the script, I do not get ANY errors, but pygame starts playing many sounds at once, without me even giving the input to the GPIOs using the buttons.
If anyone of you people can direct me on this topic, it would really be appreciated...
Thank you!

Edit: This has been solved...
I forgot to connect the Ground to the GPIO Pins.
Last edited by Shaurya on Tue Jun 11, 2019 1:05 pm, edited 1 time in total.

gordon77
Posts: 4296
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame playing sounds without input...

Mon Jun 10, 2019 5:46 pm

Try using pull up / down eg..

GPIO.setup(29,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

User avatar
Burngate
Posts: 6095
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Pygame playing sounds without input...

Mon Jun 10, 2019 6:01 pm

(from someone who may not know what he's talking about)
A snipet of your code:

Code: Select all

 ... 
try:
    while True:
        while True:
            if GPIO.input(29) ==1:
        ch1.play(pygame.mixer.Sound(Drum1))
                time.sleep(0.02)
                break
            if GPIO.input(31) ==1:
 ...
The indentation is important in Python, and the "ch1.play line" is indented less than the "if" statement, so is going to happen whether-or-no

gordon77
Posts: 4296
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame playing sounds without input...

Mon Jun 10, 2019 6:49 pm

Burngate is correct, l just put it down as some formatting error on here as the indentation is all over the place and you said you had no errors...

Try..

Code: Select all

  try:
    while True:
        while True:
            if GPIO.input(29) ==1:
                ch1.play(pygame.mixer.Sound(Drum1))
                time.sleep(0.02)
                break
            if GPIO.input(31) ==1:
                ch2.play(pygame.mixer.Sound(Drum2))
                time.sleep(0.02) 
                
                
etc...

gordon77
Posts: 4296
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pygame playing sounds without input...

Tue Jun 11, 2019 8:26 am

I am not sure how your script didn't show any errors..

Try this...

Code: Select all

import time
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(29,GPIO.IN)
GPIO.setup(31,GPIO.IN)
GPIO.setup(32,GPIO.IN)
GPIO.setup(33,GPIO.IN)
GPIO.setup(35,GPIO.IN)
GPIO.setup(36,GPIO.IN)
GPIO.setup(37,GPIO.IN)
GPIO.setup(38,GPIO.IN)

import pygame
pygame.mixer.init(44100,-16,8,2048)

Drum1 = ('/home/pi/Drum1.wav')
Drum2 = ('/home/pi/Drum2.wav')
Drum3 = ('/home/pi/DrumSp.wav')
BassDrum = ('/home/pi/BassDrum.wav')
Cym1 = ('/home/pi/Cymbal1.wav')
Cym2 = ('/home/pi/Cymbal2.wav')
Cym3 = ('/home/pi/Cymbal3.wav')
Cym4 = ('/home/pi/Cymbal4.wav')

ch1 = pygame.mixer.Channel(0)
ch2 = pygame.mixer.Channel(1)
ch3 = pygame.mixer.Channel(2)
ch4 = pygame.mixer.Channel(3)
ch5 = pygame.mixer.Channel(4)
ch6 = pygame.mixer.Channel(5)
ch7 = pygame.mixer.Channel(6)
ch8 = pygame.mixer.Channel(7)

try:
    while True:
        while True:
            if GPIO.input(29) ==1:
                ch1.play(pygame.mixer.Sound(Drum1))
                time.sleep(0.02)
                break
            if GPIO.input(31) ==1:
                ch2.play(pygame.mixer.Sound(Drum2))
                time.sleep(0.02)
                break
            if GPIO.input(32) ==1:
                ch3.play(pygame.mixer.Sound(Drum3))
                time.sleep(0.02)
                break
            if GPIO.input(33) ==1:
                ch4.play(pygame.mixer.Sound(Cym1))
                time.sleep(0.02)
                break
            if GPIO.input(35) ==1:
                ch5.play(pygame.mixer.Sound(Cym2))
                time.sleep(0.02)
                break
            if GPIO.input(36) ==1:
                ch6.play(pygame.mixer.Sound(Cym3))
                time.sleep(0.02)
                break
            if GPIO.input(37) ==1:
                ch7.play(pygame.mixer.Sound(Cym4))
                time.sleep(0.02)
                break
            if GPIO.input(38) ==1:
                ch8.play(pygame.mixer.Sound(BassDrum))
                time.sleep(0.02)
                break

finally:
    GPIO.cleanup()

Shaurya
Posts: 15
Joined: Sat Mar 02, 2019 7:37 am

Re: Pygame playing sounds without input...

Tue Jun 11, 2019 1:04 pm

I am really really sorry.
I forgot to connect the ground pin and the GPIO pins recieved a floating voltage...
I am really sorry for the inconvenience...

Return to “Python”