play sound if object is detected - python

I'm working on an object detection model using raspberry Pi. I've used Google's Object Detection API to detect models, My question is how to play sound when an object of a specific class(say human (i.e 'id' : 22))is detected.
I've tried a little and the code I came to is this,
if 22 in classes:
threading.Thread(play_sound()).start()
def play_sound():
pygame.init()
pygame.mixer.music.load("")
pygame.mixer.music.play(1,0.0)
pygame.time.wait(5000)
pygame.mixer.stop()
In this code, the problem I'm getting is
Sound starts playing even before the object is detected, I tried debugging but don't know why.
I'm starting the same thread again
If I use different threads, the pi runs out of resources and the whole execution stops
Is there any way to get this to work?
Thanks in advance

Don't use threads (you don't need them), don't use pygame.time.wait, and don't use pygame.mixer.music if you don't want to use it for background music.
Use a Sound object (and maybe provide a maxtime if you want to it's play function).
So your code should look more like this:
pygame.init()
detected_sound = pygame.mixer.Sound('filename')
...
if 22 in classes:
# use loops=-1 if the sound's length is less than 5 seconds
# so it's repeated until we hit the maxtime of 5000ms
detected_sound.play(loops=-1, maxtime=5000)
...

Related

Projectile Sound Keeps Playing Rapidly How To Fix? [duplicate]

I have the music that is always run in the background and some activities that would play sound when triggered. The music works fine.
pygame.mixer.music.load(os.path.join(SOUND_FOLDER, 'WateryGrave.ogg'))
The problem I have is that when there are 2 or more activities triggering sounds, then only one would be played (not including the background music) and the rest are muted. Is there any solution to this?
you can add sounds to different channels using the mixer:
pygame.mixer.Channel(0).play(pygame.mixer.Sound('sound\gun_fire.wav'))
pygame.mixer.Channel(1).play(pygame.mixer.Sound('sound\enemy_hit.wav'))
Within each channel you can still only play one sound at a time, but you can group sounds into different channels if they would need to play at the same time.
You can add more channels like this:
pygame.mixer.set_num_channels(10) # default is 8
A simple example. For the docs on Channels, go to:
https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Channel

Record sound without blocking Pygame UI

I am making a simple Python utility that shows the tempo of a song (BPM) that is playing. I record short fragments of a few seconds to calculate the tempo over. The problem is that now I want to show this on a display using a Pygame UI, but when I'm recording sound, the UI does not respond. I want to make it so that the UI will stay responsive during the recording of the sound, and then update the value on the screen once the tempo over a new fragment has been calculated. How can I implement this?
I have looked at threading but I'm not sure this is the appropriate solution for this.
I'd use the python threading library.
Use the pygame module in the main thread (just the normal python shell, effectively) an create a separate thread for the function that determines BPM.
This BPM can then be saved to a global variable that can be accessed by PyGame for displaying.

Pygame New Object Every Second

In pygame I need to make a flappy bird clone. I need to make recurring pipes so I want to do this by drawing new ones at the end of the screen every couple of seconds. I know there is a pygame.set_timer function but how do I implement this so that it counts in seconds and draws every couple of seconds
If you posted your entire code I might be able to help you. Since you didn't post your code you should try something like this.
import time
while True:
if (bird went past tube):
return True
...create(...)
else:
return False

Play do-re-mi musical notes (each lasting 1 sec) on Python

My son plays a string instrument that sounds terribly out of tune. I would prefer him to learn some simple Python programming to play do-re-mi notes that really sound like do-re-mi.
As a start, I would like to play do-re-mi with each note lasting 1 second duration on Python. Can this be easily done on Python? Easy means the code can be explained to a 10-year-old kid or at least not so difficult that he is put off.
I am using Python 2.7 on Windows 7.
Sounds like you are trying to program a synthesizer using python... Not a simple task. However if you do not require any smooth transition from one note to the next, and no tempo change etc. i.e. just have the program recite the notes in "Close Contact of 3rd kind" fashion, I guess you could store the digital notes in regular sound files. Have an array of these filenames, in what order you want to play them, then just issue the command to play the notes one at a time with a wait in between. There are a number of python sound libraries: Snack sound, pyMedia, pyglet, winsound (build-in to the windows python), pygame, wxPython (probably a lot more)
I like the simplicity of pygame:
import pygame
import time
notes = ['note1.mp3', 'note2.mp3', 'note3.mp3', 'note4.mp3']
pygame.init()
for note in notes:
pygame.mixer.music.load(note)
pygame.mixer.music.play()
time.sleep(2)
Here it might take a little bit to learn but try PyAudio. You may have to def some functions to generate the sound waves (represented by strings). Other options include winsound, Nsound, sunau, etc it depends how you want to play the sound.

Can't get winsound.Beep to work

I am trying to make some Beep noises with the winsound.Beep command. However when I am using winsound.Beep(500, 500) I do not hear anything. With winsound.MessageBeep and winsound.PlaySound however, I DO get to play sounds. Any ideas what I should do?
What I am trying to do: I want to write a little practicing program for training intervals: the computer sounds a first tone, then a second tone and you will have to guess what the tone interval is. For this I need pitched tones, tones for which I can set the frequency. I want to keep it as simple as possible, any pitched sound will do. I do not want to have to collect a set of .wav files or whatever. I want to make use of a tone generator which I think is available on most soundcards. winsound.Beep seems like something that can do this trick, but any other suggestions are welcome.
I had the exact same problem. winsound.Beep used to work just fine and then it suddenly stopped working (or that's what I thought). The problem was that someone (or some update) had turned off the System sounds, which prevented windows from playing the Beep sound, either manually or through my program. Try right clicking on the Speaker symbol, Open volume mixer and check whether System sounds is off or minimum volume.
I hope that helps!
Are you sure that your computer has a beep? A lot of recent computer remove the beep because it was annoying and most computer today have soundcard to play wav sound instead (the other sound that you are able to play).
You can also check to validate if it's activated on this page
IMO, I think that using the beep for other things than debugging is not a good idea.
EDIT
Mayby you can try this code to create a sound using a base wav with synth algorythm

Categories

Resources