TomRB22 commited on
Commit
3bf476b
1 Parent(s): ebc3f07

Made in-script documentation more readable

Browse files
Files changed (1) hide show
  1. audio.py +68 -36
audio.py CHANGED
@@ -33,14 +33,18 @@ _SCALING_FACTORS = pd.Series(
33
  def midi_to_notes(midi_file: str) -> pd.DataFrame:
34
  """
35
  Convert midi file to "song map" (dataframe where each note is broken
36
- into its components)
37
 
38
- Parameters:
39
- midi_file (str): Path to the midi file.
 
 
 
40
 
41
- Returns:
42
- pd.Dataframe: 3xN matrix where each column is a note, composed of
43
- pitch, duration and step.
 
44
  """
45
 
46
  pm = pretty_midi.PrettyMIDI(midi_file)
@@ -64,7 +68,8 @@ def midi_to_notes(midi_file: str) -> pd.DataFrame:
64
  # Put notes in a dataframe
65
  notes_df = pd.DataFrame({name: np.array(value) for name, value in notes.items()})
66
  notes_df = notes_df[:_CAP] # Cap the song to match the model's architecture
67
- return notes_df / _SCALING_FACTORS # Scale
 
68
 
69
 
70
  def display_audio(pm: pretty_midi.PrettyMIDI, seconds=-1) -> display.Audio:
@@ -72,13 +77,19 @@ def display_audio(pm: pretty_midi.PrettyMIDI, seconds=-1) -> display.Audio:
72
  Display a song in PrettyMIDI format as a display.Audio object.
73
  This method specially comes in useful in a jupyter notebook.
74
 
75
- Parameters:
76
- pm (str): PrettyMidi object containing a song.
77
- seconds (int): Time fraction of the song to be displayed. When
78
- set to -1, the full length is taken.
79
-
80
- Returns:
81
- display.Audio: Song as an object allowing for display.
 
 
 
 
 
 
82
  """
83
 
84
  waveform = pm.fluidsynth(fs=_SAMPLING_RATE)
@@ -88,7 +99,9 @@ def display_audio(pm: pretty_midi.PrettyMIDI, seconds=-1) -> display.Audio:
88
  else:
89
  waveform_short = waveform[:seconds*_SAMPLING_RATE]
90
 
91
- return display.Audio(waveform_short, rate=_SAMPLING_RATE)
 
 
92
 
93
 
94
  def map_to_wav(song_map: pd.DataFrame, out_file: str, velocity: int=50) -> pretty_midi.PrettyMIDI:
@@ -96,14 +109,22 @@ def map_to_wav(song_map: pd.DataFrame, out_file: str, velocity: int=50) -> prett
96
  Convert "song map" to midi file (reverse process with respect to
97
  midi_to_notes) and (optionally) save it, generating a PrettyMidi object in the process.
98
 
99
- Parameters:
100
- song_map (pd.DataFrame): 3xN matrix where each column is a note, composed of
101
- pitch, duration and step.
102
- out_file (str): Path or file to write .mid file to. If None, no saving is done.
103
- velocity: Note loudness, i. e. the hardness a piano key is struck with.
104
-
105
- Returns:
106
- pretty_midi.PrettyMIDI: PrettyMIDI object containing the song's representation.
 
 
 
 
 
 
 
 
107
  """
108
 
109
  # Get song map as dataframe
@@ -151,20 +172,31 @@ def generate_and_display(model: VAE,
151
  """
152
  Generate a song, (optionally) save it and display it.
153
 
154
- Parameters:
155
- model (VAE): Instance of VAE to generate the song with.
156
- out_file (str): Path or file to write .mid file to. If None, no saving is done.
157
- z_sample (tf.Tensor): Song encoding used to generate a song. If None, perform
158
- generate an unconditioned piece.
159
- velocity: Note loudness, i. e. the hardness a piano key is struck with.
160
- seconds (int): Time fraction of the song to be displayed. When
161
- set to -1, the full length is taken.
162
-
163
- Returns:
164
- display.Audio: Song as an object allowing for display.
 
 
 
 
 
 
 
 
 
 
165
  """
166
 
167
  song_map = model.generate(z_sample)
168
  wav = map_to_wav(song_map, out_file, velocity)
169
-
170
- return display_audio(wav, seconds)
 
 
33
  def midi_to_notes(midi_file: str) -> pd.DataFrame:
34
  """
35
  Convert midi file to "song map" (dataframe where each note is broken
36
+ into its components).
37
 
38
+ Parameters
39
+ ----------
40
+
41
+ midi_file : str
42
+ Path to the midi file.
43
 
44
+ Returns
45
+ -------
46
+ song_map : pd.Dataframe
47
+ 3xN matrix where each column is a note, composed of pitch, duration and step.
48
  """
49
 
50
  pm = pretty_midi.PrettyMIDI(midi_file)
 
68
  # Put notes in a dataframe
69
  notes_df = pd.DataFrame({name: np.array(value) for name, value in notes.items()})
70
  notes_df = notes_df[:_CAP] # Cap the song to match the model's architecture
71
+ song_map = notes_df / _SCALING_FACTORS # Scale
72
+ return song_map
73
 
74
 
75
  def display_audio(pm: pretty_midi.PrettyMIDI, seconds=-1) -> display.Audio:
 
77
  Display a song in PrettyMIDI format as a display.Audio object.
78
  This method specially comes in useful in a jupyter notebook.
79
 
80
+ Parameters
81
+ ----------
82
+
83
+ pm : str
84
+ PrettyMidi object containing a song.
85
+ seconds : int
86
+ Time fraction of the song to be displayed.
87
+ Default ``-1``, for which the full length is taken.
88
+
89
+ Returns
90
+ -------
91
+ display_obj : display.Audio
92
+ Song as an object allowing for display.
93
  """
94
 
95
  waveform = pm.fluidsynth(fs=_SAMPLING_RATE)
 
99
  else:
100
  waveform_short = waveform[:seconds*_SAMPLING_RATE]
101
 
102
+ display_obj = display.Audio(waveform_short, rate=_SAMPLING_RATE)
103
+
104
+ return display_obj
105
 
106
 
107
  def map_to_wav(song_map: pd.DataFrame, out_file: str, velocity: int=50) -> pretty_midi.PrettyMIDI:
 
109
  Convert "song map" to midi file (reverse process with respect to
110
  midi_to_notes) and (optionally) save it, generating a PrettyMidi object in the process.
111
 
112
+ Parameters
113
+ ----------
114
+
115
+ song_map : pd.DataFrame
116
+ 3xN matrix where each column is a note, composed of pitch, duration and step.
117
+ out_file : str
118
+ Path or file to write .mid file to. If None, no saving is done.
119
+ velocity : int
120
+ Note loudness, i. e. the hardness a piano key is struck with.
121
+ Default ``50``.
122
+
123
+ Returns
124
+ -------
125
+
126
+ pm : pretty_midi.PrettyMIDI
127
+ PrettyMIDI object containing the song's representation.
128
  """
129
 
130
  # Get song map as dataframe
 
172
  """
173
  Generate a song, (optionally) save it and display it.
174
 
175
+ Parameters
176
+ ----------
177
+ model : VAE
178
+ Instance of VAE to generate the song with.
179
+ out_file : str
180
+ Path or file to write .mid file to.
181
+ Default ``None``, for which no saving is done.
182
+ z_sample : tf.Tensor
183
+ Song encoding used to generate a song.
184
+ Default ``None``, for which an unconditioned piece is generated.
185
+ velocity : int
186
+ Note loudness, i. e. the hardness a piano key is struck with.
187
+ Default ``50``.
188
+ seconds : int
189
+ Time fraction of the song to be displayed.
190
+ Default ``-1``, for which the full length is taken.
191
+
192
+ Returns
193
+ -------
194
+ display_obj : display.Audio
195
+ Song as an object allowing for display.
196
  """
197
 
198
  song_map = model.generate(z_sample)
199
  wav = map_to_wav(song_map, out_file, velocity)
200
+ display_obj = display_audio(wav, seconds)
201
+
202
+ return display_obj