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

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

Related

Python Open Source Video Player To Dynamically Play a Stream of Consecutive Videos

any help is much appreciated. I've been looking for a few days for something to meet the following use case. I need to open a single video player window which will play a dynamic stream of videos selected by an accompanying program. I'm on windows 10 but could probably find a way to use linux if needed.
So the flow would be as such:
Video 1 opens in the video player window.
On a queue sent by the same script which started the previous step, Video 1 is paused/stopped and Video 2 is played in this same window with a seamless frame to frame zero transition time.
The script in the background does some additional processing to decide whether it wants to next play Video 3, Video 4, or Video 5. Let's say the script chooses Video 5.
Now on conclusion of Video 2 we want to tell this same window to play video 5, again with seamless frame to frame zero transition or black screen.
Does anyone know how to do this? I've not been able to figure it out with python-vlc, OpenCV, or anything else! Please Help! Thanks!
I found this PyQt5 code (How to play multiple clips of a video one after another see ekhumoro's answer), which appears to play one video after another. However, the media is given to the window before calling app.exec()... How do I update the media source dynamically as the window is already open and playing media? AKA after app.exec() has been called?

play sound if object is detected

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)
...

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.

move a stereoscopic video on the screen

I would like to move a (stereoscopic) video on a computer screen automatically. Think of the video as the ball in a Pong game. The problem is that it should be a stereoscopic 3D video. So the video size itself is kind of small. I did this kind of movements with pictures or drawing object, but I don't know how to do it with video material!
Does somebody know how I can do this? I already searched for video tools in python like pygame or pyglet. I have an external player Bino 3d which can open the desired video. But how can I make it move around the screen?
Or is there a tool in other programming languages like c/c++ or Matlab which can help?
By the way, the program will be on a Linux OS.
I'll be grateful for any help or hints!
Anna
I'd try to use a decent video client (mplayer, vlc). They can present the video in lots of ways, hopefully your stereoscopic issue can be solved by them.
Then I would let the client present a single window (not fullscreen) which I then would move around using window manager controls.
If you must not have window decorations around the video or if the output shall be a specific window, I think mplayer at least can be told to use an existing window to perform the output in. Maybe that's an approach then.

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