Pygame: How to Create a Music Object - pygame

So, first of all, my overall goal is to play a different background music depending on where the player is on the map. I want one track to fade out and the other to fade in when the player reaches a certain point.
Here are my problems:
I've created a "Fader" class, with the help of Fade Between Two Music Tracks in-progress in Pygame. This allows me to fade tracks in and out, but it doesn't specify how to adapt the code to use pygame.mixer.music instead of pygame.mixer.Sound.
If I play both tracks as a Sound, on different Channels, the program slows wayyyyyy down - rendering it unusable. Research has shown that this is because music is played one bit at a time, while sounds are loaded in all at once and then played - which obviously takes quite a bit of processing power.
However, I can't seem to play them as music - and here we come to the heart of my problem. This is my Fader class written to play the music as a Sound:
class Fader(object):
def __init__(self, filename):
self.sound = pygame.mixer.Sound(filename)
# the speed at which it will fade
self.increment = 0.5
# the volume to which it will fade
self.next_vol = 1
def fade_to(self, new_vol):
self.next_vol = new_vol
curr_volume = self.get_volume()
if self.next_vol > curr_volume:
self.set_volume(curr_volume + self.increment)
elif self.next_vol < curr_volume:
self.sound.set_volume(curr_volume - self.increment)
This doesn't work, for reasons stated above. However, I cannot for the life of me achieve the same effect with music. The code written to play music as Music is the same except for line 3, which is now:
self.sound = pygame.mixer.music(filename)
I then get an error message saying "pygame.mixer.music is not callable." I understand that to mean that I'm not creating an object with that line, so I tried:
self.sound = pygame.mixer.music.load(filename)
...as I understood that to be the line that creates the Music object. However, here I get "NoneType object has no attribute 'play'" when I try to play the track.
I can't seem to figure this out, no one else seems to have this problem, and - go figure - the pygame website is down, so I can't look at the docs.

according to the documentation on https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load
The pygame.mixer.music.load(filename) function loads the music stream into the mixer , and returns None
so to play the sound, replace
self.sound = pygame.mixer.music.load(filename)
with pygame.mixer.music.load(filename)
followed by pygame.mixer.music.play(filename)

Related

Can't set the frame rate when recording a video with VecVideoRecorder

I have a working RL model and set up that produce a video for me - however becuase the model is reasonably good, the videos are very short (reach a desitination therfore better = shorter)
Is there a way to drop the frame rate of the video output? I know it can be done with a gif. And that it can be done with ffmpeg but I can't workout how to pass it down.
I've dropped the fps in my environment from 50>10 expecting the video to be 5 times as long but that didn't work.
Save me stackoverflow you're my only hope. (appart from posting on github)
When you say that you dropped the fps from 50 to 10, I assume you changed env.metadata.video.frames_per_second, which initializes to 50 (for gym versions less than 0.22.0):
{
'render.modes': ['human', 'rgb_array'],
'video.frames_per_second': 50
}
Starting with gym 0.22.0, VideoRecorder class gets the frames_per_sec to write the video with:
env.metadata.get("render_fps", 30)
Setting env.metadata["render_fps"] = 4 should result in a slowed down video.

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.

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.

Flash CS3 (AS3) is giving my graphics (buttons and movieclips) outlines

So, I'm pretty much a beginner in Flash and Actionscript (using AS3, as I said in the title), and I'm trying to make a basic escape the room game. I haven't gotten far, and right now that's because every time I test my game (or publish preview it) the graphics get this annoying outline. Here it is when tested: http://i305.photobucket.com/albums/nn228/chokingondrama/flash.png
Every outline corresponds to some object present in the game, most of which have an alpha component of 0 since they're on different sides of the room. This didn't happen before, but once I added the code that allowed the player to change their view with the arrow (each viewpoint/wall is a different frame) these appeared.
It's a little different when published to HTML, basically it just gives each image a white background: http://i305.photobucket.com/albums/nn228/chokingondrama/html.png
Also, it would be nice if somebody could give me advice on how to make sure importing to flash won't result in lower quality.
Thanks in advance. If needed, I'll post any part of the code.
Some tips:
Don't set alpha to 0, instead use the visible property, setting movieclip.visible = false will make it a lot more efficient.
As for the importing and quality, after you import to stage or library, bring up the library (ctrl + l), and right click on the file you imported, go to properties. If it's an image, set compression to lossless, and allow smoothing.
For audio, go to file-> publish settings, and change audio stream and audio event (whichever you might use) to 128kbps.
As for your main question, I need more info, if you want you can post your source. It might be because of how you are placing your graphics on the stage.
For each of your MovieClips in question:
Try disabling button mode and see if the rectangles go away.
movieClipName.buttonMode = false;
If that doesn't help, or you really want button mode, try setting
movieClipName.tabEnabled = false;
There's a chance that since you added keyboard interaction each of your MovieClips are now expecting to be selected by the user when they press the tab key, much like any normal web form.
tabEnabled in the docs
You could also try
movieClipName.focusRect = false;
focusRect in the docs