[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] seeking in a .WAV with pygame.mixer.music? and "pynudge"



Hullo,

If your sound was hello.wav, and contained you saying "hello my name is Denise".

# load the sound 
sound = pygame.mixer.Sound('hello.wav')

sound2 = sound_from_pos(sound, 0.5)

Now sound2 would contain a sound begining at 0.5 seconds into
hello.wav.  It would sound like " name is Denise".  Or maybe "ello my
name is Denise".

Here are some extra comments for the function.

def sound_from_pos(sound, start_pos, samples_per_second = None, inplace = 1):
    """  returns a sound which begins at the start_pos.
         start_pos - in seconds from the begining.
         samples_per_second - 
    """

    # see if we want to reuse the sound data or not.
    if inplace:
        a1 = pygame.sndarray.samples(sound)
    else:
        a1 = pygame.sndarray.array(sound)

    # see if samples per second has been given.  If not, query the mixer.
    #   eg. it might be set to 22050
    if samples_per_second is None:
        samples_per_second = pygame.mixer.get_init()[0]

    # figure out the start position in terms of samples.
    start_pos_in_samples = int(start_pos * samples_per_second)

    # cut the begining off the sound at the start position.
    a2 = a1[start_pos_in_samples:]

    # make the Sound instance from the array.
    sound2 = pygame.sndarray.make_sound(a2)

    return sound2



Try downloading the example, and play around with it.  Run it from
with the examples directory.
http://rene.f0o.com/~rene/sound_array_demos.py

Try printing the sound arrays, and have a look at the data.

If you want, try making a function which trims a sound to a maximum length.

def trim_sound(sound, end_pos, samples_per_second = None, inplace = 1):
    raise NotImplementedError


Clear as mud now?





On 7/19/05, D. Hartley <denise.hartley@xxxxxxxxx> wrote:
> Rene,
> 
> Thanks for posting this.  Sorry to be dense, but could you explain
> what this ends up sounding like? (i.e., a non-technical "for instance,
> if your sound was _____, it would sound like a _____ and then do ____"
> kind of thing?)
> 
> I'm trying to understand how to work with music in programming, but I
> don't understand TOO much about music files in general yet so I'm just
> looking for a "for dummies" overview sentence or two of what the
> output is :)
> 
> Thanks so much!
> 
> ~Denise
> 
> On 7/18/05, Rene Dudfield <renesd@xxxxxxxxx> wrote:
> > Below is a function which makes a new Sound from another one at a
> > different start_pos.
> >
> > eg, called like this:
> > sound = pygame.mixer.Sound(os.path.join('data', 'car_door.wav'))
> > sound2 = sound_from_pos(sound, 0.2)
> >
> > ps. I just commited this to examples/sound_array_demos.py in cvs.
> >
> > So you can load up your .wav, and start playing it at any position.
> >
> >
> > Cheers.
> >
> >
> > def sound_from_pos(sound, start_pos, samples_per_second = None, inplace = 1):
> >    """  returns a sound which begins at the start_pos.
> >         start_pos - in seconds from the begining.
> >         samples_per_second -
> >    """
> >    if inplace:
> >        a1 = pygame.sndarray.samples(sound)
> >    else:
> >        a1 = pygame.sndarray.array(sound)
> >
> >    if samples_per_second is None:
> >        samples_per_second = pygame.mixer.get_init()[0]
> >
> >    start_pos_in_samples = int(start_pos * samples_per_second)
> >    a2 = a1[start_pos_in_samples:]
> >    sound2 = pygame.sndarray.make_sound(a2)
> >
> >    return sound2
> >
> >
> >
> >
> > On 7/19/05, James Hofmann <jwhinfinity@xxxxxxxxx> wrote:
> > > I looked for a way....also looked in the standard
> > > Python libraries for something along those lines.
> > > Nothing really seems to provide that kind of low-level
> > > interface. Pymedia might provide that but the
> > > documentation is very sketchy... I had a hard time
> > > trying to figure out what it is and isn't capable of.
> > >
> > > I might work on my own problem some more today, and if
> > > I find out anything I'll post it.
> > >
> > > > Is there any way to address and index the sound data
> > > > in a mixer
> > > > object?  I've tried dissecting it, but it seems like
> > > > there's no PyGame
> > > > interface...  soo, if I needed to access it, I'd
> > > > have to go to the SDL
> > > > level.
> > > >
> > >
> > >
> > >
> > >
> > > ____________________________________________________
> > > Start your day with Yahoo! - make it your home page
> > > http://www.yahoo.com/r/hs
> > >
> > >
> >
>