def main():

    stddraw.createWindow(1024, 256)
    #stddraw.setCanvasSize(1024, 256)
    stddraw.setXscale(0, _SAMPLES_PER_REDRAW)
    stddraw.setYscale(-.75, +.75)
    stddraw.show(0)

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i-24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    #stddraw.show(0)
    t = 0

    # The main input loop.
    while True:

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()

        # Play the total.
        stdaudio.playSample(sample)

        # Plot
        stddraw.point(t % _SAMPLES_PER_REDRAW, sample);

        if (t % _SAMPLES_PER_REDRAW) == (_SAMPLES_PER_REDRAW - 1):
            stddraw.show(0);
            stddraw.clear();

        t += 1
示例#2
0
def main():

    stddraw.createWindow()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2**((i - 24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    # loopCount is used to control the frequency of calls of
    # stddraw.show().
    loopCount = 1023

    # The main input loop.
    while True:

        # Call stddraw.show() occasionally to capture keyboard events.
        if loopCount == 1023:
            stddraw.show()
            loopCount = 0
        loopCount += 1

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()

        # Play the total.
        stdaudio.playSample(sample)
def main():

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i-24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    # loopCount is used to control the frequency of calls of
    # stddraw.show().
    loopCount = 1023

    # The main input loop.
    while True:

        # Call stddraw.show() occasionally to capture keyboard events.
        if loopCount == 1023:
            stddraw.show()
            loopCount = 0
        loopCount += 1

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()

        # Play the total.
        stdaudio.playSample(sample)
def main():
    # Refresh rate (in seconds) for the keyboard.
    KEYBOARD_REFRESH_DELAY = 0.01

    # Specifies superposition window size.
    SUPERPOSITION_RANGE = 2

    # Setup the canvas and scale for the keyboard.
    stddraw.setCanvasSize(1056, 300)
    stddraw.setXscale(0, 1056)
    stddraw.setYscale(0, 300)

    # Create guitar strings for notes corresponding to the keys in keyboard.
    keyboard = 'q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/\' '
    notes = []
    for i in range(len(keyboard)):
        hz = 440 * 2**((i - 24.0) / 12.0)
        notes += [guitar_string.create(hz)]

    pressed = -1  # index of the pressed key
    start = time.clock()  # for refreshing the keyboard
    while True:
        # Check if the user has typed a key; if so, process it, ie, pluck
        # the corresponding string.
        if stddraw.hasNextKeyTyped():
            key = stddraw.nextKeyTyped()
            if key in keyboard:
                pressed = index(keyboard, key)
                guitar_string.pluck(notes[pressed])

        if pressed != -1:
            # Superimpose samples for keys that are within the
            # 2 * SUPERPOSITION_RANGE window centered at the pressed key.
            sample = 0.0
            b = max(0, pressed - SUPERPOSITION_RANGE)
            e = min(len(keyboard) - 1, pressed + SUPERPOSITION_RANGE)
            for i in range(b, e + 1):
                note = notes[i]
                sample += guitar_string.sample(note)
                guitar_string.tic(note)

            # Play the sample on standard audio.
            stdaudio.playSample(sample)

        # Display the keyboard with the pressed key in red, every
        # KEYBOARD_REFRESH_DELAY seconds.
        if time.clock() - start > KEYBOARD_REFRESH_DELAY:
            start = time.clock()
            draw_keyboard(pressed)
            stddraw.show(0.0)
示例#5
0
def main2():
    import stddraw
    import stdaudio

    #stddraw.setXscale(0, 1024)
    #stddraw.setYscale(-1, +1)
    #stddraw.show(0)

    # Play middle A for 3 seconds.
    a = GuitarString(440.0)
    a.pluck()
    seconds = 3
    for i in range(_SAMPLE_RATE * seconds):
        a.tic()
        stdaudio.playSample(a.sample())
示例#6
0
def main2():
    import stddraw
    import stdaudio

    #stddraw.setXscale(0, 1024)
    #stddraw.setYscale(-1, +1)
    #stddraw.show(0)

    # Play middle A for 3 seconds.
    a = GuitarString(440.0)
    a.pluck()
    seconds = 3
    for i in range(_SAMPLE_RATE * seconds):
        a.tic()
        stdaudio.playSample(a.sample())
示例#7
0
def multiplier(key, notes):
    for index, num in enumerate(notes):
        if num == key:
            return index


def frequency(num):
    freq = 110.00 * 2**(num / 12)
    return freq


play = GuitarString(110.00)

p = Picture('piano.png')
stddraw.picture(p)
stddraw.show(0.0)

escape = False
while not escape:
    stddraw._checkForEvents()
    while stddraw.hasNextKeyTyped():
        key = stddraw.nextKeyTyped()
        if key == chr(27):
            escape = True
        freq = frequency(multiplier(key, notes))
        play = GuitarString(freq)
        play.pluck()

    y = play.tick()
    stdaudio.playSample(y)