How can I play multiple sounds at the same time in pygame? - pygame

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

how to load multiple songs/tracks into pygame?

Is there a way to load multiple songs into Pygame? I'm not talking about sound effects like this;
crash_sound = pygame.mixer.Sound("crash.ogg")
#and
pygame.mixer.Sound.play(crash)
because I know you can have multiple different sound effects, assigned to different variables, obviously. But I'm talking about the music function in pygame (this one):
pygame.mixer.music.load("chill_music.ogg")
#and
pygame.mixer.music.stop()
because you can't assign it to variables, or anything, you just set the mixer.music thing to one ogg file and can't have more.
I need this feature, because it let's me set it to a '-1' value making it play over and over again, which I'm pretty sure you can't do with the sound effects, but I want two different songs for two different levels.
I hope it makes sense.
Thanks
I need this feature, because it let's me set it to a '-1' value making it play over and over again, which I'm pretty sure you can't do with the sound effect
Actually, you can:
play()
begin sound playback
play(loops=0, maxtime=0, fade_ms=0) -> Channel
The loops argument controls how many times the sample will be repeated after being played the first time. ... If loops is set to -1 the Sound will loop indefinitely (though you can still call stop() to stop it).
So you can use the Sound class.
but I want two different songs for two different levels
Nothing stops you from calling pygame.mixer.music.load(...) again with another sound file. It will stop playing the current file and start the new one.
Note:
The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once...
So if your music files are rather big and you don't want to store them in memory, using pygame.mixer.music is the way to go. If you don't mind loading the files completely, you can use the Sound class.

ActionScript3: How to "offset" playing of sound object(with multiple objects playing at same time)

I want to make a program where you arrange sound samples. A cursor plays the sound it sweeps over and there can be many sound samples played at once. I want the user to be able to change/intterupt the playing(aka cursor progress) with a mouse click(similar to a progress bar).
I understand there are some ways of playing multiple sounds files at once, but thats not the problem. I wonder how I can play a sound sample from an offset when a click interrupt is generated.
Which AS3 class should I look at? Any other tips are appreciated.
You can use Sound.play() supplying the offset in milliseconds. You should first calculate the offset by measuring mouse position vs the progress bar, then stop the sound if it's playing, then call theSound.play(yourOffset) and you should be set.

Play same base64 data concurrently multiple times

I am making an all-client-side audio/music editor. I have created a few tones mathematically that are stored as base64 in the <audio> src-attribute. I can play DIFFERENT tones at the same time BUT, I can only play ONE instance of ONE specific tone at the same time.
For example clicking the key to play C like crazy will sound very awkward since the C that was playing gets stopped and the new C starts. I would like there to be possibility to play several C tones at the same time!
Now I guess this could be made by having by simple copying the audio element (one or more times) and make the keypress, sort of, cycle through them. For example if the first C tone is playing and the key to play C is clicked, then play the second C audio element, and so on and so forth.
That would work... but since I am using base64 in the source I would also have to have that copied.
<audio id="C1"><source src="data:audio/wav;base64,audio_data"></source></audio>
<audio id="C2"><source src="data:audio/wav;base64,audio_data"></source></audio>
If "audio_data" would be really long then the html would become humongous and also I think that the browser would not understand that both are actually the exactly same data, so it would be come very unoptimized.
So to the concrete question: Is there a way to play the same base64 data several times at the same time without the need of copying the whole src-attribute with the base64 string in it? (Since my application is all-client so I have not the ability to save the data to a sound-file on a server)
See a simple example. It might not work in other browsers than Firefox because I have not tested:
https://jsfiddle.net/tx3hpptL/

AS3: Recording sound as they are output/played

I understand how to record microphone input in AS3 from this doc.
Is it possible to record sound exactly as they are being output/played?
The reason is I applied some sound transform (via the global SoundMixer) to sounds that are currently playing; and I also want to record this sound data while it is being played.
I just saw this question, to clarify, I am not trying to record just all sounds on the user's computer (which is not possible). My flash app has a Youtube player in it (via their AS3 API), and it's playing some sounds. I applied transforms using SoundMixer.soundTransform, and I want to record what's being played when the user is playing it.
Thanks in advance.
Just a passing suggestion.. on my desktop it seems ABLE to record sound into Flash from a different tab playing Youtube (HTML5).. I don't know how it's doing that!!
I allow microphone here.. (none actually plugged in, and speaker out has in-ear headphones)
http://code.tutsplus.com/tutorials/create-a-useful-audio-recorder-app-in-actionscript-3--active-5836
PS: Anyone trying this must reduce Windows volume since anything above 10-20% is distorted audio into the Flash app.
And this HTML5 youtube trailer was recorded fine into the Wav file produced by Flash app above
http://www.youtube.com/watch?v=MVt32qoyhi0
So after a quick search it seems my Realtek Audio is classed as a Full-Duplex soundcard and also within its own control panel I have an option called "Multi-streaming" which is enabled/ticked. I think Full-Duplex is enough to do this though. Try options within your soundcard's own settings software. Don't know about your end-users. Some hardware will do it, some wont, there is no all-round solution outside of AIR (which makes desktop apps out of your AS3 code).

Windows phone 8: how to play background next song?

I'm programming an app which is able to play music in background. I'm using mediaplayer and Song(class xnalib), well since is not possible put file into mediaplayer.queue I can't figure out how can play next song in the "playlist".
I'll try to make this scenario more clear with an example:
I play music with mediaplayer: MediaPlayer.Play(MysongArray[CurrentIndex]);
well now user put my app in background.
but when MysongArray[CurrentIndex] song is ended how play next song if my application is in background?
thanks
That's probably because you are playing a Song not a SongCollection - once it's ended it stops playing. Source: MSDN1, MSDN2. I belive (thounght I've not tried it) that you should create SongCollection (if it's possible) or Manage existing one. But this can be a hard job as #ToniPetrina answered here.
As for me I would consider using Background Audio Player and How to play.
Maybe this will help if you decide to use BAP - example how to use playlist.
You cannot do that since background player can only stream or play from isolated storage. You cannot play songs from MediaLibrary.