コード例 #1
0
ファイル: montreal.py プロジェクト: jonathanmarmor/montreal
    def __init__(self, piece):
        """
        self.duration = total song duration
        self.bars =
            bar.parts =
                part['instrument_name']
                part['notes']
                    note['pitch']
                    note['duration'] = total note duration


        """
        self.piece = piece
        self.prev_root = random.randint(0, 11)
        self.harmony_generator = get_harmony_generator()
        # TODO choose roots

        self.vln_all_notes = self.piece.instruments.vln.all_notes
        self.vln_register = self.choose_violin_register()

        self.form = form.choose()
        self.bars = self.form.bars

        self.duration_beats = self.form.duration
        self.tempo = random.randint(55, 80)
        self.bars[0].tempo = self.tempo
        self.duration_minutes = self.duration_beats / float(self.tempo)

        for name in self.form.bar_types:
            bar_type = self.form.bar_types[name]
            bar_type.harmonic_rhythm = harmonic_rhythm.choose(
                bar_type.duration)

            bar_type.parts[1]['notes'] = [{
                'duration': harm_dur,
                'pitch': self.choose_harmony()
            } for harm_dur in bar_type.harmonic_rhythm]

            bar_type.parts[0]['notes'] = self.choose_melody_notes(
                bar_type.duration, bar_type.parts[1]['notes'])

        for bar in self.bars:
            bar.parts = bar.type_obj.parts
コード例 #2
0
ファイル: montreal.py プロジェクト: jonathanmarmor/montreal
    def __init__(self, piece):
        """
        self.duration = total song duration
        self.bars =
            bar.parts =
                part['instrument_name']
                part['notes']
                    note['pitch']
                    note['duration'] = total note duration


        """
        self.piece = piece
        self.prev_root = random.randint(0, 11)
        self.harmony_generator = get_harmony_generator()
        # TODO choose roots

        self.vln_all_notes = self.piece.instruments.vln.all_notes
        self.vln_register = self.choose_violin_register()

        self.form = form.choose()
        self.bars = self.form.bars

        self.duration_beats = self.form.duration
        self.tempo = random.randint(55, 80)
        self.bars[0].tempo = self.tempo
        self.duration_minutes = self.duration_beats / float(self.tempo)

        for name in self.form.bar_types:
            bar_type = self.form.bar_types[name]
            bar_type.harmonic_rhythm = harmonic_rhythm.choose(bar_type.duration)

            bar_type.parts[1]['notes']= [{
                'duration': harm_dur,
                'pitch': self.choose_harmony()
            } for harm_dur in bar_type.harmonic_rhythm]

            bar_type.parts[0]['notes'] = self.choose_melody_notes(bar_type.duration, bar_type.parts[1]['notes'])

        for bar in self.bars:
            bar.parts = bar.type_obj.parts
コード例 #3
0
ファイル: song.py プロジェクト: jonathanmarmor/shortstories
    def __init__(self, piece, movement):
        """
        self.duration = total song duration
        self.bars =
            bar.parts =
                part['instrument_name']
                part['notes']
                    note['pitch']
                    note['duration'] = total note duration


        """
        self.piece = piece
        self.movement = movement
        self.prev_root = random.randint(0, 11)

        self.instruments = self.piece.instruments

        self.melody_register = self.instruments.soloists_shared_register()

        history = {}
        for name in self.instruments.names:
            history[name] = []

        self.form = form.choose()

        print self.form.form_string

        self.bars = self.form.bars
        self.duration_beats = self.form.duration

        if self.movement <= 5:
            # Medium
            tempo_options = range(56, 66, 2)
        elif self.movement in [6, 8, 10, 12]:
            # Fast
            tempo_options = range(66, 78, 2)
        elif self.movement in [7, 9, 11]:
            # Slow
            tempo_options = range(42, 54, 2)
        elif self.movement in [13, 14]:
            # Slower
            tempo_options = range(36, 42, 2)
        elif self.movement > 14:
            # Grind to a halt
            tempo_options = range(22, 34, 2)

        self.tempo = random.choice(tempo_options)

        self.bars[0].tempo = self.tempo
        self.duration_minutes = self.duration_beats / float(self.tempo)

        root = random.randint(0, 12)
        self.harmony_history = [[(p * root) % 12 for p in get_chord_type()]]

        # for each bar_type pick a transposition pattern
        # for each bar of that type, assign the next transposition in the
        # pattern
        # make a sub register of the soloists' common register for the
        # bar_type, that has a buffer to allow the melody to be transposed the
        # amount that it will be transposed
        trans_patterns = {
            'descending': [
                [0, -1, -2, -3, -4],
                [0, -2, -1, -3, -2],
                [0, -1, -3, -5, -7],
                [0, -2, -4, -6, -8],
                [0, -2, -4, -5, -7],
            ],
            'ascending': [
                [0, 1, 2, 3, 4],
                [0, 2, 1, 3, 2],
                [0, 2, 4, 5, 7],
                [0, 2, 3, 5, 7],
                [0, 2, 4, 6, 8],
            ]
        }
        for name in self.form.bar_types:
            bar_type = self.form.bar_types[name]

            bar_type.direction = random.choice(trans_patterns.keys())
            options = trans_patterns[bar_type.direction]
            bar_type.trans_pattern = random.choice(options)
            bar_type.trans_pattern = bar_type.trans_pattern[:bar_type.count + 1]

            if bar_type.direction == 'descending':
                bar_type.furthest_transposition = min(bar_type.trans_pattern)
                bar_type.register = self.melody_register[abs(bar_type.furthest_transposition):]
            else:
                bar_type.furthest_transposition = max(bar_type.trans_pattern)
                bar_type.register = self.melody_register[:-bar_type.furthest_transposition]

        bar_type_counter = Counter()
        for bar in self.bars:
            bar_type = self.form.bar_types[bar.type]
            i = bar_type_counter[bar.type]
            bar.transposition = bar_type.trans_pattern[i]
            bar_type_counter[bar.type] += 1

        for name in self.form.bar_types:
            bar_type = self.form.bar_types[name]
            bar_type.harmonic_rhythm = harmonic_rhythm.choose(bar_type.duration)

            # Harmony
            bar_type.harmony = []
            for harm_dur in bar_type.harmonic_rhythm:

                chord_type = get_chord_type()

                # print 'CHORD TYPE:', chord_type

                harmony = animal_play_harmony.choose_next_harmony(
                    self.harmony_history,
                    chord_type
                )
                self.harmony_history.append(harmony)

                # print 'HARMONY:', harmony

                h = {
                    'duration': harm_dur,
                    'pitch': harmony
                }
                bar_type.harmony.append(h)

            # Melody
            bar_type.melody = self.choose_melody_notes(
                bar_type.duration,
                bar_type.harmony,
                bar_type
            )

        # Turn Bar Types into Bars

        size = 1
        for bar in self.bars:

            bar.parts = []

            bar.melody = bar.type_obj.melody
            bar.harmony = bar.type_obj.harmony

            if not self.piece.duet_options:
                self.piece.duet_options = [
                    ('ob', 'sax'),
                    ('fl', 'sax'),
                    ('cl', 'sax'),
                    ('ob', 'tpt'),
                    ('sax', 'tpt'),
                    ('cl', 'tpt'),
                    ('fl', 'tpt'),
                    ('fl', 'ob'),
                    ('fl', 'cl'),
                    ('ob', 'cl'),
                ]

            soloist_options = [
                'ob',
                'cl',
                'sax',
                'fl',
                'tpt',
            ]
            soloist_weights = [
                35,
                26,
                16,
                13,
                10,
            ]
            soloists = []

            if size == 1:
                soloist = weighted_choice(soloist_options, soloist_weights)
                soloists.append(soloist)
                soloist_options.remove(soloist)
            elif size == 2:
                duet = random.choice(self.piece.duet_options)
                self.piece.duet_options.remove(duet)
                for soloist in duet:
                    soloists.append(soloist)
                    soloist_options.remove(soloist)

            # transposition = weighted_choice(
            #     [-2, -1, 1, 2],
            #     [10, 12, 8, 12]
            # )

            transposition = self.add_soloists_melody(soloists, bar)

            if transposition != 0:
                harmony = []
                for note in bar.harmony:
                    harmony.append({
                        'pitch': [p + transposition for p in note['pitch']],
                        'duration': note['duration']
                    })
                bar.harmony = harmony

            # Violin
            violin_lowest = self.piece.instruments.vln.lowest_note.ps
            violin_highest = self.piece.instruments.vln.highest_note.ps
            if not history['vln']:
                p = random.randint(violin_lowest + 7, violin_highest - 18)
                rand_interval = random.randint(-7, 7)
                violin_prev_dyad = [p, p + rand_interval]

                history['vln'].append(violin_prev_dyad)

            violin = []
            for chord in bar.harmony:
                pitch = next_violin_dyad(
                    history['vln'][-1],
                    chord['pitch'],
                    violin_lowest,
                    violin_highest
                )

                violin.append({
                    'duration': chord['duration'],
                    'pitch': pitch,
                })
                history['vln'].append(pitch)

            # Vibraphone
            vib_lowest = int(self.piece.instruments.vib.lowest_note.ps)
            vib_highest = int(self.piece.instruments.vib.highest_note.ps)
            if not history['vib']:
                prev_vib_chord = random_vibraphone_voicing(
                    vib_lowest,
                    vib_highest
                )
                history['vib'].append(prev_vib_chord)

            vibraphone = []
            for harm in bar.harmony:

                vib_pitches = next_vibraphone_chord(
                    history['vib'][-1],
                    harm['pitch'],
                    vib_lowest,
                    vib_highest
                )

                vibraphone.append({
                    'duration': harm['duration'],
                    'pitch': vib_pitches,
                })

            # Bass
            bass_lowest = self.piece.instruments.bs.lowest_note.ps
            bass_highest = self.piece.instruments.bs.highest_note.ps
            if not history['bs']:
                bass_prev_pitch = random.randint(bass_lowest, bass_lowest + 18)
                history['bs'].append(bass_prev_pitch)

            bass = []
            for chord in bar.harmony:
                pitch = next_bass_note(
                    history['bs'][-1],
                    chord['pitch'],
                    bass_lowest,
                    bass_highest
                )

                bass.append({
                    'duration': chord['duration'],
                    'pitch': pitch,
                })
                history['bs'].append(pitch)

            bar.parts.extend([
                {
                    'instrument_name': 'vln',
                    'notes': violin,
                },
                {
                    'instrument_name': 'vib',
                    'notes': vibraphone,
                },
                {
                    'instrument_name': 'bs',
                    'notes': bass,
                },
            ])

            if size > 1:
                num_accompanists = 2  # random.randint(2, len(soloist_options))
                accompanists = random.sample(soloist_options, num_accompanists)
                for acc in accompanists:
                    soloist_options.remove(acc)

                    lowest = self.piece.instruments.d[acc].lowest_note.ps
                    highest = self.piece.instruments.d[acc].highest_note.ps

                    if not history[acc]:
                        quarter_of_register = (highest - lowest) / 4
                        lower_limit = int(lowest + quarter_of_register)
                        higher_limit = int(highest - quarter_of_register)

                        p = random.randint(lower_limit, higher_limit)
                        rand_interval = random.randint(-4, 4)
                        prev_dyad = [p, p + rand_interval]

                        history[acc].append(prev_dyad)

                    acc_notes = []
                    for chord in bar.harmony:
                        pitch = next_simple_accompaniment_dyad(
                            history[acc][-1],
                            chord['pitch'],
                            lowest, highest
                        )

                        acc_notes.append({
                            'duration': chord['duration'],
                            'pitch': pitch,
                        })
                        history[acc].append(pitch)

                    bar.parts.append({
                        'instrument_name': acc,
                        'notes': acc_notes,
                    })

            # Put rests in instruments that aren't playing in this bar
            bar_of_rests = [{
                'pitch': 'rest',
                'duration': bar.duration,
            }]
            for inst in soloist_options:
                bar.parts.append({
                    'instrument_name': inst,
                    'notes': bar_of_rests,
                })

            size = 1 if size > 1 else 2