Beispiel #1
0
def main():
    """
    These are the various supported use cases for the `randsik.generate` function. The
    `randsik.generate` function returns a `randsik.Pattern` object. This object includes
    a save function which allows you to write the midi file to disc. It takes a file
    name as its primary argument.
    """

    # When called with no argument, generate will select a start note and mode randomly
    # combined with several other default arguments for measures, time_sig and note_lengths
    pat = randsik.generate()
    midi_file = MidiFile()
    midi_file.tracks.append(pat.track)
    midi_file.save('example.mid')

    # To have more control over what kind of pattern the generate function generates, you
    # can pass in a number of arguments include, mode (musical mode), octaves (number of
    # octaves the pattern will span) and others.
    pat = randsik.generate('C2',
                           mode='lydian',
                           octaves=1,
                           measures=2,
                           time_sig='4/4',
                           note_lengths=(randsik.QUARTER, randsik.EIGHTH))
    midi_file = MidiFile()
    midi_file.tracks.append(pat.track)
    midi_file.save(sys.argv[1])
Beispiel #2
0
def track(inst: con.Instrument, channel: int, sections: Iterator) -> MidiTrack:
    """
    Returns a randomly programmed track.
    """
    full_track = MidiTrack()

    for include, section in sections:
        if include is False:
            pattern = get_rest_measures(section.measures, inst, section.tempo,
                                        channel)
        else:
            note_lengths = get_random_note_lengths()
            pattern = generate(section.key,
                               mode=section.mode,
                               octaves=section.octaves,
                               measures=section.measures,
                               time_sig='4/4',
                               tempo=section.tempo,
                               scale_degrees=get_random_scale_degrees(
                                   section.octaves),
                               channel=channel,
                               note_lengths=note_lengths,
                               program=inst,
                               velocity=80)
        full_track += pattern.track

    return full_track
Beispiel #3
0
    def test_generate_happy_path(self):
        """
        Test the happy path for using the "generate" function to create a random pattern
        """
        pattern = generate()

        tf_str = tempfile.mktemp()
        pattern.save(tf_str)
        os.unlink(tf_str)
Beispiel #4
0
def main():
    """
    Play a random set of notes
    """
    if len(sys.argv) > 1:
        portname = sys.argv[1]
    else:
        portname = None  # Use default port

    try:
        with mido.open_output(portname, autoreset=True) as port:
            while True:
                pat = randsik.generate(mode='ionian', note='D4')
                pat.play(port, 92)
    except KeyboardInterrupt:
        pass
Beispiel #5
0
def main():
    """
    Play a random set of notes
    """
    if len(sys.argv[1]) > 1:
        config_file = sys.argv[1]
    else:
        print('Please specify config file (json format)')
        sys.exit(1)
    print('Select output to use')
    for idx, output in enumerate(mido.get_output_names(), 1):
        print(f'{idx}: {output}')
    output = input('Output [1]:') or 1

    print(output, type(output))

    try:
        portname = mido.get_output_names()[int(output) - 1]
    except (IndexError, ValueError):
        print('Not found')
        sys.exit(1)
    print('Playing... (Ctrl-C to exit)')

    try:
        with mido.open_output(portname, autoreset=True) as port:
            while True:
                with open(sys.argv[1]) as f:
                    config = json.loads(f.read())
                    pat = randsik.generate(**config['generate'])
                    for msg in pat.track:
                        if isinstance(msg, mido.Message):
                            port.send(msg)
                            time.sleep(config['tempo'])

    except KeyboardInterrupt:
        pass
Beispiel #6
0
def test_generate_happy_path():
    """
    Test the happy path for using the "generate" function to create a random pattern
    """
    pattern = generate()
    assert pattern.track