Exemple #1
0
def arpeggiate(invert=False,
               descending=False,
               chord=None,
               bpm=None,
               durations=None):
    if not bpm:
        bpm = st.BPM

    # if not delay:
    #     delay = st.DELAY/2

    if chord:
        pass
    elif st.CURRENT_MODE.name in ['single_chord', 'chord_tone']:
        chord = st.CURRENT_Q_INFO["chord"]
    elif st.CURRENT_MODE.name in ['interval']:
        chord = st.CURRENT_Q_INFO["interval"]
    else:
        print("Arpeggiation not available in {} mode.".format(st.CURRENT_MODE))
        return

    arpeggiation = [x for x in chord]
    if invert:
        arpeggiation = [arpeggiation[i] for i in invert]
    elif descending:
        arpeggiation.reverse()

    # Play
    easy_play(arpeggiation, durations, bpm)
    play_wait()  # play wait
def new_question_rn(game_settings):
    gst = game_settings
    if st.NEWQUESTION:
        if st.COUNT:
            print("score: {} / {} = {:.2%}".format(st.SCORE, st.COUNT,
                                                   st.SCORE / st.COUNT))
        st.COUNT += 1
        # Find random melody/progression
        try:
            previous_note = st.CURRENT_Q_INFO['notes'][-1]
        except:
            previous_note = None
        notes = gst.scale.bounded_random_notes(gst.low, gst.high, gst.max_int,
                                               gst.notes_per_phrase,
                                               previous_note)

        # store question info
        st.CURRENT_Q_INFO = {'notes': notes}
    else:
        notes = st.CURRENT_Q_INFO['notes']

    # Play melody/progression
    # start_time = time.time()
    # i0 = len(HISTORY)
    if gst.single_notes:
        easy_play(notes, bpm=gst.bpm)
    else:
        easy_play([gst.scale.root2chord(n, gst.chord_type) for n in notes],
                  bpm=gst.bpm)

    # def midi_listen(notes):
    #     i0 = len(HISTORY)
    #     quarter_notes_per_second = 4*gst.bpm/60
    #     time.sleep(gst.notes_per_phrase*quarter_notes_per_second)
    #     # play_wait(notes, bpm=gst.bpm)
    #     return HISTORY[i0:]

    # Request user's answer
    if isinstance(gst.listener, MidiListener):
        user_response = \
            gst.listener.listen(num_notes=gst.notes_per_phrase)
        user_response_notes = parse_midi_input(user_response)
        eval_rn(user_response_notes, notes, gst)
        play_wait(3, bpm=gst.bpm)
    elif isinstance(gst.listener, MicListener):
        user_response_notes = gst.listener.listen(notes, (gst.low, gst.high),
                                                  mingus_range=True)
        eval_rn(user_response_notes, notes, gst)
    else:
        play_wait(3, bpm=gst.bpm)
Exemple #3
0
def new_question_interval():
    if st.NEWQUESTION:
        if st.COUNT:
            print("score: {} / {} = {:.2%}"
                  "".format(st.SCORE, st.COUNT, st.SCORE / st.COUNT))
        st.COUNT += 1

        # Pick Ioctave
        if st.MANY_OCTAVES:
            Ioctave = random.choice(st.OCTAVES)
        else:
            Ioctave = st.DEFAULT_IOCTAVE

        from musictools import Diatonic
        diatonic = Diatonic(key=st.KEY, Ioctave=Ioctave)

        # pick first note
        if st.FIXED_ROOT:
            first_note = diatonic.notes[st.FIXED_ROOT - 1]
        else:
            first_note = random.choice(diatonic.notes)

        # pick second note
        if st.INTERVAL_MODE == 'triads':
            number = random.choice([3, 5, 8])
            interval = diatonic.interval(number,
                                         root=first_note,
                                         ascending=True)
        elif st.INTERVAL_MODE == 'sevenths':
            number = random.choice([3, 5, 7, 8])
            interval = diatonic.interval(number,
                                         root=first_note,
                                         ascending=True)
        elif st.INTERVAL_MODE == 'ascending':
            number = random.choice(st.INTERVALS)
            interval = diatonic.interval(number,
                                         root=first_note,
                                         ascending=True)
        elif st.INTERVAL_MODE == 'descending':  # redundant for harmonic intrvls
            number = random.choice(st.INTERVALS)
            interval = diatonic.interval(number,
                                         root=first_note,
                                         ascending=False)
        elif st.INTERVAL_MODE == 'mixed':  # redundant for harmonic intervals
            number = random.choice(st.INTERVALS)
            interval = diatonic.interval(number,
                                         root=first_note,
                                         ascending=bool(random.choice([0, 1])))
        else:
            raise Exception("Can't understand.  st.INTERVAL_MODE = {}"
                            "".format(st.INTERVAL_MODE))

        # change Unison intervals to P8 intervals
        if len(interval) == 1:
            P8 = copy(interval[0])
            P8.octave += 1
            interval = NoteContainer([interval[0], P8])

        # store question info
        st.CURRENT_Q_INFO = {
            'interval': interval,
            'Ioctave': Ioctave,
            'diatonic': diatonic
        }

    else:
        interval = st.CURRENT_Q_INFO['interval']
        Ioctave = st.CURRENT_Q_INFO['Ioctave']
        diatonic = st.CURRENT_Q_INFO['diatonic']

    # Play interval
    if st.HARMONIC_INTERVALS:
        easy_play(interval)
    else:
        easy_play([x for x in interval])

    # Request user's answer
    ans = input("Enter 1-7 or note names separated by spaces: ").strip()

    if ans in menu_commands:
        menu_commands[ans].action()
    else:
        if st.NAME_INTERVAL:
            eval_interval_name(ans, interval, diatonic)
        else:
            eval_interval(ans, interval, diatonic)
    return