z-uo commited on
Commit
108f44c
1 Parent(s): 668c8a0

music demo

Browse files
Files changed (5) hide show
  1. app.py +58 -0
  2. examples/test.xml +183 -0
  3. examples/test.xml.mid +0 -0
  4. packages.txt +1 -0
  5. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+
4
+ import streamlit as st
5
+ from PIL import Image
6
+
7
+ import pretty_midi
8
+ import music21
9
+ import note_seq
10
+ from scipy.io import wavfile
11
+
12
+ import numpy as np
13
+
14
+ st.title('Music Demo')
15
+ st.markdown('This example need a .xml music file as input and show partiture, piano roll and a simply wav. You can generate it from MuseScore or similar software.')
16
+
17
+ example_1 = st.button('Test Example')
18
+
19
+ uploaded_file = st.file_uploader("Choose a .xml music file", type='xml')
20
+
21
+ if uploaded_file is not None or example_1:
22
+ if uploaded_file is not None:
23
+ # save file
24
+ with open(os.path.join(os.getcwd(), uploaded_file.name), "wb") as f:
25
+ f.write(uploaded_file.getbuffer())
26
+ file_path = os.path.join(os.getcwd(), uploaded_file.name)
27
+ else:
28
+ file_path = 'examples/test.xml'
29
+
30
+ # Show partiture with music21
31
+ music = music21.converter.parse(file_path)
32
+ streaming_partiture = str(music.write('lily.png'))
33
+ image = Image.open(streaming_partiture)
34
+ st.text('Partiture')
35
+ st.image(image)
36
+
37
+ # save to mid
38
+ midi_path = file_path + '.mid'
39
+ music.write('midi', fp=midi_path)
40
+
41
+ # plot piano rol
42
+ unconditional_ns = note_seq.midi_file_to_note_sequence(midi_path)
43
+ piano_roll = note_seq.plot_sequence(unconditional_ns, show_figure=False)
44
+ st.text('Piano Roll')
45
+ st.bokeh_chart(piano_roll, use_container_width=True)
46
+
47
+ # generate wav
48
+ with st.spinner(f"Sinthezizing to wav"):
49
+ midi_data = pretty_midi.PrettyMIDI(midi_path)
50
+ audio_data = midi_data.synthesize()
51
+ audio_data = np.int16(
52
+ audio_data / np.max(np.abs(audio_data)) * 32767 * 0.9
53
+ ) # -- Normalize for 16 bit audio https://github.com/jkanner/streamlit-audio/blob/main/helper.py
54
+ virtualfile = io.BytesIO()
55
+ wavfile.write(virtualfile, 44100, audio_data)
56
+
57
+ st.audio(virtualfile)
58
+ st.markdown("Download the audio by right-clicking on the media player")
examples/test.xml ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.1 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
3
+ <score-partwise version="3.1">
4
+ <work>
5
+ <work-title>Title</work-title>
6
+ </work>
7
+ <identification>
8
+ <creator type="composer">Composer</creator>
9
+ <encoding>
10
+ <software>MuseScore 3.6.2</software>
11
+ <encoding-date>2022-03-12</encoding-date>
12
+ <supports element="accidental" type="yes"/>
13
+ <supports element="beam" type="yes"/>
14
+ <supports element="print" attribute="new-page" type="yes" value="yes"/>
15
+ <supports element="print" attribute="new-system" type="yes" value="yes"/>
16
+ <supports element="stem" type="yes"/>
17
+ </encoding>
18
+ </identification>
19
+ <defaults>
20
+ <scaling>
21
+ <millimeters>6.99911</millimeters>
22
+ <tenths>40</tenths>
23
+ </scaling>
24
+ <page-layout>
25
+ <page-height>1696.94</page-height>
26
+ <page-width>1200.48</page-width>
27
+ <page-margins type="even">
28
+ <left-margin>85.7252</left-margin>
29
+ <right-margin>85.7252</right-margin>
30
+ <top-margin>85.7252</top-margin>
31
+ <bottom-margin>85.7252</bottom-margin>
32
+ </page-margins>
33
+ <page-margins type="odd">
34
+ <left-margin>85.7252</left-margin>
35
+ <right-margin>85.7252</right-margin>
36
+ <top-margin>85.7252</top-margin>
37
+ <bottom-margin>85.7252</bottom-margin>
38
+ </page-margins>
39
+ </page-layout>
40
+ <word-font font-family="Edwin" font-size="10"/>
41
+ <lyric-font font-family="Edwin" font-size="10"/>
42
+ </defaults>
43
+ <part-list>
44
+ <score-part id="P1">
45
+ <part-name>Piano</part-name>
46
+ <part-abbreviation>Pno.</part-abbreviation>
47
+ <score-instrument id="P1-I1">
48
+ <instrument-name>Piano</instrument-name>
49
+ </score-instrument>
50
+ <midi-device id="P1-I1" port="1"></midi-device>
51
+ <midi-instrument id="P1-I1">
52
+ <midi-channel>1</midi-channel>
53
+ <midi-program>1</midi-program>
54
+ <volume>78.7402</volume>
55
+ <pan>0</pan>
56
+ </midi-instrument>
57
+ </score-part>
58
+ </part-list>
59
+ <part id="P1">
60
+ <measure number="1" width="587.65">
61
+ <print>
62
+ <system-layout>
63
+ <system-margins>
64
+ <left-margin>50.00</left-margin>
65
+ <right-margin>0.00</right-margin>
66
+ </system-margins>
67
+ <top-system-distance>70.00</top-system-distance>
68
+ </system-layout>
69
+ </print>
70
+ <attributes>
71
+ <divisions>2</divisions>
72
+ <key>
73
+ <fifths>0</fifths>
74
+ </key>
75
+ <time>
76
+ <beats>4</beats>
77
+ <beat-type>4</beat-type>
78
+ </time>
79
+ <clef>
80
+ <sign>G</sign>
81
+ <line>2</line>
82
+ </clef>
83
+ </attributes>
84
+ <note default-x="73.72" default-y="-15.00">
85
+ <pitch>
86
+ <step>C</step>
87
+ <octave>5</octave>
88
+ </pitch>
89
+ <duration>2</duration>
90
+ <voice>1</voice>
91
+ <type>quarter</type>
92
+ <stem>down</stem>
93
+ <lyric number="1" default-x="6.50" default-y="-42.97" relative-y="-30.00">
94
+ <syllabic>single</syllabic>
95
+ <text>when</text>
96
+ </lyric>
97
+ </note>
98
+ <note default-x="194.22" default-y="-15.00">
99
+ <pitch>
100
+ <step>C</step>
101
+ <octave>5</octave>
102
+ </pitch>
103
+ <duration>2</duration>
104
+ <voice>1</voice>
105
+ <type>quarter</type>
106
+ <stem>down</stem>
107
+ <lyric number="1" default-x="6.50" default-y="-42.97" relative-y="-30.00">
108
+ <syllabic>single</syllabic>
109
+ <text>the</text>
110
+ </lyric>
111
+ </note>
112
+ <note default-x="314.72" default-y="-15.00">
113
+ <pitch>
114
+ <step>C</step>
115
+ <octave>5</octave>
116
+ </pitch>
117
+ <duration>1</duration>
118
+ <voice>1</voice>
119
+ <type>eighth</type>
120
+ <stem>down</stem>
121
+ <beam number="1">begin</beam>
122
+ <lyric number="1" default-x="6.50" default-y="-42.97" relative-y="-30.00">
123
+ <syllabic>single</syllabic>
124
+ <text>sun</text>
125
+ </lyric>
126
+ </note>
127
+ <note default-x="390.04" default-y="-15.00">
128
+ <pitch>
129
+ <step>C</step>
130
+ <octave>5</octave>
131
+ </pitch>
132
+ <duration>1</duration>
133
+ <voice>1</voice>
134
+ <type>eighth</type>
135
+ <stem>down</stem>
136
+ <beam number="1">end</beam>
137
+ <lyric number="1" default-x="6.50" default-y="-42.97" relative-y="-30.00">
138
+ <syllabic>begin</syllabic>
139
+ <text>li</text>
140
+ </lyric>
141
+ </note>
142
+ <note default-x="465.35" default-y="-30.00">
143
+ <pitch>
144
+ <step>G</step>
145
+ <octave>4</octave>
146
+ </pitch>
147
+ <duration>2</duration>
148
+ <voice>1</voice>
149
+ <type>quarter</type>
150
+ <stem>up</stem>
151
+ <lyric number="1" default-x="6.50" default-y="-42.97" relative-y="-30.00">
152
+ <syllabic>end</syllabic>
153
+ <text>ght</text>
154
+ </lyric>
155
+ </note>
156
+ </measure>
157
+ <measure number="2" width="391.38">
158
+ <note default-x="26.70" default-y="-25.00">
159
+ <pitch>
160
+ <step>A</step>
161
+ <octave>4</octave>
162
+ </pitch>
163
+ <duration>4</duration>
164
+ <voice>1</voice>
165
+ <type>half</type>
166
+ <stem>up</stem>
167
+ <lyric number="1" default-x="6.50" default-y="-42.97" relative-y="-30.00">
168
+ <syllabic>single</syllabic>
169
+ <text>now</text>
170
+ </lyric>
171
+ </note>
172
+ <note>
173
+ <rest/>
174
+ <duration>4</duration>
175
+ <voice>1</voice>
176
+ <type>half</type>
177
+ </note>
178
+ <barline location="right">
179
+ <bar-style>light-heavy</bar-style>
180
+ </barline>
181
+ </measure>
182
+ </part>
183
+ </score-partwise>
examples/test.xml.mid ADDED
Binary file (134 Bytes). View file
 
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ lilypond
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+
3
+ music21
4
+ pretty_midi
5
+ note_seq
6
+ bokeh==2.4.1
7
+
8
+ scipy
9
+ numpy