roszcz commited on
Commit
efec4d0
1 Parent(s): 674a9fb

add music generation sample

Browse files
Files changed (1) hide show
  1. app.py +37 -1
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import streamlit as st
2
  import streamlit_pianoroll
3
  from fortepyan import MidiPiece
@@ -14,6 +15,8 @@ def main():
14
 
15
  def piano_music_demo():
16
  piece = MidiPiece.from_file("haydn.mid")
 
 
17
 
18
  st.write("## Display a PianoRoll player")
19
  st.write("""
@@ -41,6 +44,11 @@ def piano_music_demo():
41
  create two `MidiPiece` objects, each containing the notes for one color.
42
  """)
43
  st.write("#### Absolute pitch value condition")
 
 
 
 
 
44
 
45
  code = """
46
  df = piece.df
@@ -79,6 +87,9 @@ def piano_music_demo():
79
  )
80
 
81
  st.write("#### Note duration condition")
 
 
 
82
 
83
  code = """
84
  df = piece.df
@@ -100,7 +111,7 @@ def piano_music_demo():
100
  df = piece.df.copy()
101
 
102
  duration_threshold = st.number_input(
103
- label="duration threshold",
104
  min_value=0.05,
105
  max_value=5.,
106
  value=0.25,
@@ -118,6 +129,31 @@ def piano_music_demo():
118
  secondary_piece=piece_b,
119
  )
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  if __name__ == "__main__":
123
  main()
 
1
+ import numpy as np
2
  import streamlit as st
3
  import streamlit_pianoroll
4
  from fortepyan import MidiPiece
 
15
 
16
  def piano_music_demo():
17
  piece = MidiPiece.from_file("haydn.mid")
18
+ # TODO Improve fortepyan to make this cleaner
19
+ piece.time_shift(-piece.df.start.min())
20
 
21
  st.write("## Display a PianoRoll player")
22
  st.write("""
 
44
  create two `MidiPiece` objects, each containing the notes for one color.
45
  """)
46
  st.write("#### Absolute pitch value condition")
47
+ st.write("""
48
+ Here's how to highlight notes with pitch above or below a certain threshold.
49
+ Value of pitch 60 corresponds to the middle C (C4) on a piano keyboard
50
+ ([refrence table](https://inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies)).
51
+ """)
52
 
53
  code = """
54
  df = piece.df
 
87
  )
88
 
89
  st.write("#### Note duration condition")
90
+ st.write("""
91
+ Here's how to highlight notes based on their absolute duration, which can differentiate between fast and slow notes.
92
+ """)
93
 
94
  code = """
95
  df = piece.df
 
111
  df = piece.df.copy()
112
 
113
  duration_threshold = st.number_input(
114
+ label="duration threshold [s]",
115
  min_value=0.05,
116
  max_value=5.,
117
  value=0.25,
 
129
  secondary_piece=piece_b,
130
  )
131
 
132
+ st.write("#### Music generation prompting")
133
+ st.write("""
134
+ Here's how to highlight show results of a generative algorithm that is supposed to work musically with an input from.
135
+ We can take the notes with pitch below 72 (C5) as a prompt, and display it together with the generation.
136
+ This sample performs random shuffle of the note pitch values, so the results are not muscially great (your algorithms should be better).
137
+ """)
138
+
139
+ df = piece.df.copy()
140
+
141
+ ids = df.pitch < 72
142
+
143
+ part_a = df[ids].copy()
144
+ piece_a = MidiPiece(df=part_a)
145
+
146
+ # Fake random algorithm
147
+ part_b = df[~ids].copy()
148
+ part_b.pitch = np.random.permutation(part_b.pitch)
149
+ part_b.velocity = np.random.permutation(part_b.velocity)
150
+ piece_b = MidiPiece(df=part_b)
151
+
152
+ streamlit_pianoroll.from_fortepyan(
153
+ piece=piece_a,
154
+ secondary_piece=piece_b,
155
+ )
156
+
157
 
158
  if __name__ == "__main__":
159
  main()