File size: 4,776 Bytes
efec4d0
771b98d
 
 
 
0c8bdc2
 
 
51f2390
771b98d
5cea595
0c8bdc2
 
 
 
 
 
efec4d0
 
0c8bdc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efec4d0
 
 
 
 
0c8bdc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efec4d0
 
 
0c8bdc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efec4d0
0c8bdc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78db4f7
efec4d0
78db4f7
 
 
 
efec4d0
 
78db4f7
 
 
 
 
 
 
 
db67129
78db4f7
 
 
 
 
 
 
 
 
 
 
efec4d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c8bdc2
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import numpy as np
import streamlit as st
import streamlit_pianoroll
from fortepyan import MidiPiece

st.set_page_config(
    page_title="PianoRoll Demo",
    page_icon=":musical_keyboard:",
)


def main():
    piano_music_demo()


def piano_music_demo():
    piece = MidiPiece.from_file("haydn.mid")
    # TODO Improve fortepyan to make this cleaner
    piece.time_shift(-piece.df.start.min())

    st.write("## Display a PianoRoll player")
    st.write("""
    The core functionality of pianorolls includes music playback and visualization.
    If you have a MIDI file with piano music, see here for instructions on interacting with it using Streamlit components.
    """)

    code = """
    import streamlit_pianoroll
    from fortepyan import MidiPiece

    piece = MidiPiece.from_file("haydn.mid")
    streamlit_pianoroll.from_fortepyan(piece)
    """
    st.code(code, language="python")
    streamlit_pianoroll.from_fortepyan(piece)
    st.info(
        body="This component is dedicated to piano music, there's no way to interract with multiple instruments.",
        icon="🎹",
    )

    st.write("## Conditional coloring")
    st.write("""
    To create a pianoroll with different notes in separate colors,
    create two `MidiPiece` objects, each containing the notes for one color.
    """)
    st.write("#### Absolute pitch value condition")
    st.write("""
    Here's how to highlight notes with pitch above or below a certain threshold.
    Value of pitch 60 corresponds to the middle C (C4) on a piano keyboard
    ([refrence table](https://inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies)).
    """)

    code = """
    df = piece.df
    ids = df.pitch > pitch_threshold

    part_a = df[ids]
    part_b = df[~ids]
    piece_a = MidiPiece(df=part_a)
    piece_b = MidiPiece(df=part_b)

    streamlit_pianoroll.from_fortepyan(
        piece=piece_a,
        secondary_piece=piece_b,
    )
    """
    st.code(code, language="python")

    df = piece.df.copy()

    pitch_threshold = st.number_input(
        label="pitch_threshold",
        min_value=df.pitch.min(),
        max_value=df.pitch.max(),
        value=60,
    )
    ids = df.pitch > pitch_threshold

    part_a = df[ids].copy()
    part_b = df[~ids].copy()
    piece_a = MidiPiece(df=part_a)
    piece_b = MidiPiece(df=part_b)

    streamlit_pianoroll.from_fortepyan(
        piece=piece_a,
        secondary_piece=piece_b,
    )

    st.write("#### Note duration condition")
    st.write("""
    Here's how to highlight notes based on their absolute duration, which can differentiate between fast and slow notes.
    """)

    code = """
    df = piece.df

    ids = df.duration > duration_threshold

    part_a = df[ids]
    part_b = df[~ids]
    piece_a = MidiPiece(df=part_a)
    piece_b = MidiPiece(df=part_b)

    streamlit_pianoroll.from_fortepyan(
        piece=piece_a,
        secondary_piece=piece_b,
    )
    """
    st.code(code, language="python")

    df = piece.df.copy()

    duration_threshold = st.number_input(
        label="duration threshold [s]",
        min_value=0.05,
        max_value=5.,
        value=0.25,
    )

    ids = df.duration > duration_threshold

    part_a = df[ids].copy()
    part_b = df[~ids].copy()
    piece_a = MidiPiece(df=part_a)
    piece_b = MidiPiece(df=part_b)

    streamlit_pianoroll.from_fortepyan(
        piece=piece_a,
        secondary_piece=piece_b,
    )

    st.write("#### Music generation")
    st.write("""
    Here's how to show the results of a generative algorithm designed to respond to a musical input prompt.
    We can use notes with pitches below 72 (C5) as the prompt and display them alongside the generated output.
    This example performs a random shuffle of note pitch values, so the results are not musically appealing
    (your algorithms should produce better results).
    """)

    code = """
    df = piece.df.copy()

    ids = df.pitch < 72

    prompt_df = df[ids].copy()
    prompt_piece = MidiPiece(df=part_a)

    # Use your implementation here:
    generated_df = music_generation_algorithm(prompt_df)

    generated_piece = MidiPiece(df=generated_df)

    streamlit_pianoroll.from_fortepyan(
        piece=prompt_piece,
        secondary_piece=generated_piece,
    )
    """
    st.code(code, language="python")

    df = piece.df.copy()

    ids = df.pitch < 72

    part_a = df[ids].copy()
    piece_a = MidiPiece(df=part_a)

    # Fake random algorithm
    part_b = df[~ids].copy()
    part_b.pitch = np.random.permutation(part_b.pitch)
    part_b.velocity = np.random.permutation(part_b.velocity)
    piece_b = MidiPiece(df=part_b)

    streamlit_pianoroll.from_fortepyan(
        piece=piece_a,
        secondary_piece=piece_b,
    )


if __name__ == "__main__":
    main()