This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2009-08-12 00:34:17

InvaderKap
Scratcher
Registered: 2009-08-11
Posts: 3

Why is my Scratch-generated music offbeat?

So I've thought of a rather simple way to create multi-track music in Scratch... or so it would seem from the perspective of one who was to read the code. Unfortunately, during actual execution, it seems a bit dysfunctional, and I'm not certain why. The primary concept is to have a script broadcast several messages that are each received by separate scripts, which then play the tracks simultaneously. The program that I'm trying to run is below:
[blocks]
<when green flag clicked>
<broadcast[ melody ]>
<broadcast[ chords ]>

<when I receive[ melody ]>
<play note( 73 )for( 3 )secs>
<play note( 72 )for( 6 )secs>
<play note( 73 )for( 6 )secs>

<when I receive[ chords ]>
<repeat( 4 )>
<play note( 58 )for( 1 )secs>
<play note( 61 )for( 1 )secs>
<play note( 65 )for( 1 )secs>
<end>
<repeat( 4 )>
<play note( 57 )for( 1 )secs>
<play note( 61 )for( 1 )secs>
<play note( 65 )for( 1 )secs>
<end>
[/blocks]
The script belongs to the background. All instances of "secs" are actually beats in the real program, not seconds. For some reason, when I run it, the melody is slightly faster than the chords. Does anyone know why? Could it have something to do with the fact that I'm using repeat loops?


What is the answer to this question?

Offline

 

#2 2009-08-12 01:55:51

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Why is my Scratch-generated music offbeat?

Hi InvaderKap,

using Scratch's parallelism for harmonic music is a wonderful idea, it is also one of my favorite features and in essence what turned me on to Scratch in the first place.

You're right that those "repeats" in your example blocks are responsible for getting your music out of synch, because executing any block takes up a little time.

Synchronizing parallel music threads can be achieved by breaking up your music into smaller scripts and by using a master thread which fires off the messages calling the subscripts at intervals which are measured by the master script alone. It's like having an orchestra (the subthreads) and a director (the master script). In my experience it's good to break up your music into half-measures or at least into full measures and send a message at every first (and possibly also every third beat, if you're implementing a 4/4 beat).

So, to transcribe harmonic music with 3 voices (melody, second voice and bass) you could try something like this (this is a real working example, btw):

   [master script:]

    when green flag clicked
        forever 
            broadcast "measure 1"
            rest for 2 beats
            broadcast "measure 2"
            rest for 2 beats
    end

   [melody scripts:]

    when I receive "measure 1"
        play note 64 for 1 beats
        play note 62 for 0.5 beats
        play note 62 for 0.5 beats
    end

    when I receive "measure 2"
        play note 60 for 2 beats
    end

   [bass scripts:]

    when I receive "measure 1"
        play note 48 for 1 beats
        play note 43 for 1 beats
    end

    when I receive "measure 2"
        play note 45 for 1 beats
        play note 41 for 1 beats
    end

   [voice 2 scripts:]

    when I receive "measure 1"
        play note 55 for 1 beats
        play note 59 for 1 beats
    end

    when I receive "measure 2"
        play note 52 for 1 beats
        play note 57 for 1 beats
    end

Also, feel free to look at some of my musical projects and canibalize them!


Jens Mönig

Offline

 

Board footer