How to gradually increase or decrease an FMSound frequency over time?

Rob Rothwell r.j.rothwell at gmail.com
Fri Apr 21 02:27:43 UTC 2006


Here is essentially what I want to do, which gives a "smooth" (stepped)
progression from f1 to f2.  stepsPerSecond sort of controls the "smoothness"
of the progression.

    f1 := 330.50.
    f2 := 440.90.
    f := f1.
    t := 0.
    sec := 15.
    stepsPerSecond := 10.0.
    df := (f2-f1) / (sec*stepsPerSecond).
    ss := SequentialSound new.
    f := f-df.

"   Sweep smoothly from f1 to just shy of f2 over the specified time,
re-calculating the amount of time for each step
    based on the integral number of frequencies that can be completed within
the estimated step size.
    We should end up just shy of the target frequency, at the end of a
complete cycle..."

    1 to: (sec*stepsPerSecond) do: [
        :i |
        dt := (((i asFloat)/stepsPerSecond)-(t asFloat)).
        f := (f + df).
        cycles := (f*dt) roundTo: 1.0.
        dt := cycles / f.
        Transcript show: f; tab; show: dt; cr.
        ss add: (FMSound new setPitch: f dur: dt loudness: 0.5).
        t := t + dt.
    ].

"   Add the the final frequency and play it briefly..."
    Transcript show: f2; cr.
     ss add: (FMSound new setPitch: f2 dur: 1.0 loudness: 0.5).

    ss storeWAVOnFileNamed: 'test.wav'.
    ss play.

I would do something similar with the times if I wanted to "step" sharply
from one frequency to another without the slide, but without introducing an
audible "click."  Basically I just need to make sure that the transition
from one frequency to another happens on an integral number of wave cycles.

However, this seems somewhat inelegant given the apparent richness of the
sound classes.  It *feels* like I am doing something "by hand" that is
probably already built into the system.  It seems like the PitchEnvelope
class *should* work, if I understood it better!  Furthermore, without
understanding the VolumeEnvelope class, I would do something like the above
to fade-in and fade-out, changing volume levels instead of frequencies.

Thanks,
Rob

On 4/20/06, Hans-Martin Mosner <hmm at heeg.de> wrote:

>Have you looked at the PitchEnvelope class? It allows you to sweep a
>sound between frequencies.
>You would need to take into account that it's working on a logarithmic
>scale, so it does not interpolate frequencies linearly, but musical
>notes. In many cases this would be the desired behaviour, I don't know
>enough about your application to say whether it is acceptable here as well.
>
>Cheers,
>Hans-Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20060420/81a9dc76/attachment.htm


More information about the Squeak-dev mailing list