how to load multiple songs/tracks into pygame? - 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.

Related

How can I play multiple sounds at the same time in 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

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/

Sound plays multiple times at once

I'm having trouble with sound in Flash. I may have went about coding the wrong way, because most of my codes are on frames.
So, I have these two variables
var outsideDay:Sound = new daysong();
var outsideNight:Sound = new nightsong();
And I want to play these songs on a specific frame. However, the sounds play sporadically, like 50 times at once. I think it's because I have other codes that link to the frames with a Enter_Frame function. How can I get the sounds to loop and not play multiple times at once?
Have you dropped the sound anywhere on the timeline in any frame? If so remove that frame.
Also, if you have your code declared on a keyframe that does not have a stop(); call on it, likely it is hitting that frame over and over again, when it "enters" it. Try adding stop(); either at the beginning of your code or at an ending key-frame, wherever it makes most sense for your project.
After trying those two things, another method I have learned to love that may come in handy is:
flash.media.SoundMixer.stopAll();
This will stop all sounds so that whatever sounds you start to play after making this call will not have other previously started sounds to contend with.
This sound tutorial may also be of use to you.
Let us know how it goes, or if any of this stuff helped.

SWF file seek information

Is it possible to get and set seek position of a swf file (either in ActionScript 2/3 or ActiveX API of Flash Player) ?
I tried to use frame number at first (to determine which part of the movie is being played) however sometimes whole animation has 2 frames and it does not help.
Any ideas?
Thanks.
if your loaded SWF contains all animation on the stage timeline ( i mean the top level ) then the progress you are seeking is:
var progress:Number = swf.currentFrame / swf.totalFrames;
if you want to set seek:
swf.gotoAndPlay ( progress * swf.totalFrames )
Also it can be that the all animation is not on the top level, but in the movieClip, which address you know, i mean stage.getChildByName ( 'something ).getChildeByName ('something )...
but if you have an swf where are multiple movieclips, and they are playing independantly, then nothing i can help....
Not without coding something in to the SWF as far as I know. Because animations in Flash can be in embedded MovieClips, or entirely scripted in a single frame, there is no easy way to detect the total playing time or how far an animation has progressed. Any get/seek functionality, or even a progress broadcast, would have to be scripted in the SWF and accessed through ExternalInterface.
Please be more specific about your problem. If your swf file is only 2 frames, then what is it that you are trying to seek through? Does your swf load a video, for example, and this is what you are trying to seek through? (In which case you can get properties of a video object's playback position and state).
Without more information, it's hard to provide a clear answer.