Classes and functions used for visualization
DualPianoRoll
dataclass
Bases: PianoRoll
Extends the PianoRoll class to represent a dual-layer piano roll visualization.
The DualPianoRoll class enhances the basic piano roll visualization by allowing for the representation of additional information, such as masking in machine learning contexts, through the use of dual color mapping.
Attributes:
| Name | Type | Description |
|---|---|---|
base_cmap |
Union[str, ListedColormap]
|
The colormap for the base layer of the piano roll. |
marked_cmap |
Union[str, ListedColormap]
|
The colormap for the marked layer of the piano roll. |
mark_key |
str
|
The key used to determine markings in the MIDI data. |
Methods:
| Name | Description |
|---|---|
__post_init__ |
Initializes the dual-layer piano roll with specified colormaps. |
_build_image |
Builds the dual-layer piano roll image from the MIDI data, applying color mappings. |
Source code in fortepyan/view/pianoroll/structures.py
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
FigureResolution
dataclass
Represents the resolution configuration for a figure.
Attributes:
| Name | Type | Description |
|---|---|---|
w_pixels |
int
|
The width of the figure in pixels. |
h_pixels |
int
|
The height of the figure in pixels. |
dpi |
int
|
The dots per inch (resolution) of the figure. |
Properties
w_inches (float): The width of the figure in inches, calculated from pixels and dpi. h_inches (float): The height of the figure in inches, calculated from pixels and dpi. figsize (tuple[float, float]): The size of the figure as a tuple of width and height in inches.
Source code in fortepyan/view/pianoroll/structures.py
PianoRoll
dataclass
Represents a piano roll visualization of a MIDI piece.
The PianoRoll class provides a visual representation of MIDI data as a traditional piano roll, which is often used in music software. This representation includes the ability to mark the current time, set start and end times for the visualization, and dynamically build the piano roll image based on the MIDI data.
Attributes:
| Name | Type | Description |
|---|---|---|
midi_piece |
MidiPiece
|
The MIDI piece to be visualized. |
current_time |
float
|
The current time position in the MIDI piece. |
time_start |
float
|
The start time for the piano roll visualization. |
time_end |
float
|
The end time for the piano roll visualization. |
roll |
array
|
The numpy array representing the piano roll image. |
RESOLUTION |
int
|
The resolution of the piano roll image. |
N_PITCHES |
int
|
The number of pitches to be represented in the piano roll. |
Methods:
| Name | Description |
|---|---|
__post_init__ |
Initializes the piano roll image and tick preparations. |
lowest_pitch |
Returns the lowest pitch present in the MIDI piece. |
highest_pitch |
Returns the highest pitch present in the MIDI piece. |
_build_image |
Builds the piano roll image from the MIDI data. |
_prepare_ticks |
Prepares the tick marks and labels for the piano roll visualization. |
Source code in fortepyan/view/pianoroll/structures.py
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 | |
draw_piano_roll(ax, piano_roll, time=0.0, cmap='GnBu')
Draws a piano roll visualization on a Matplotlib axis.
This function visualizes the piano roll of a MIDI piece on a given Matplotlib axis. It includes options to highlight notes played at a specific time and to customize the color mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
The Matplotlib axis on which to draw the piano roll. |
required |
piano_roll
|
PianoRoll
|
The PianoRoll object representing the MIDI piece. |
required |
time
|
float
|
The specific time at which to highlight notes. Defaults to 0.0. |
0.0
|
cmap
|
str
|
The color map to use for the visualization. Defaults to "GnBu". |
'GnBu'
|
Returns:
| Type | Description |
|---|---|
Axes
|
plt.Axes: The modified Matplotlib axis with the piano roll visualization. |
Source code in fortepyan/view/pianoroll/main.py
draw_pianoroll_with_velocities(midi_piece, time_end=None, title=None, cmap='GnBu', figres=None)
Draws a pianoroll representation of a MIDI piece with an additional plot for velocities.
This function creates a two-part plot with the upper part displaying the pianoroll and the lower part showing the velocities of the notes. Customizable aspects include the end time for the plot, the title, color mapping, and figure resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
midi_piece
|
MidiPiece
|
The MIDI piece to be visualized. |
required |
time_end
|
float
|
End time for the plot. Defaults to None, meaning full duration is used. |
None
|
title
|
str
|
Title for the plot. Defaults to None. |
None
|
cmap
|
str
|
Color map for the pianoroll and velocities. Defaults to "GnBu". |
'GnBu'
|
figres
|
FigureResolution
|
Custom figure resolution settings. Defaults to None,
which initiates a default |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
A matplotlib figure object with the pianoroll and velocity plots. |
Source code in fortepyan/view/pianoroll/main.py
draw_velocities(ax, piano_roll, cmap='GnBu')
Draws a velocity plot for a MIDI piece on a Matplotlib axis.
This function visualizes the velocities of notes in a MIDI piece using a scatter and line plot. The color and style of the plot can be customized using a colormap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
The Matplotlib axis on which to draw the velocity plot. |
required |
piano_roll
|
PianoRoll
|
The PianoRoll object representing the MIDI piece. |
required |
cmap
|
str
|
The color map to use for the visualization. Defaults to "GnBu". |
'GnBu'
|
Returns:
| Type | Description |
|---|---|
Axes
|
plt.Axes: The modified Matplotlib axis with the velocity plot. |
Source code in fortepyan/view/pianoroll/main.py
sanitize_midi_piece(piece)
Trims a MIDI piece to a maximum duration threshold for manageability.
If the duration of the MIDI piece exceeds a predefined threshold, it trims the piece to fit within this limit. This function is useful to avoid excessively long playtimes which might be impractical for visualization or analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
piece
|
MidiPiece
|
The MIDI piece to be sanitized. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MidiPiece |
MidiPiece
|
The sanitized MIDI piece, trimmed if necessary. |
Source code in fortepyan/view/pianoroll/main.py
sanitize_xticks(ax, piece)
Adjusts the x-axis ticks and labels for a MIDI piece plot for improved readability.
This function computes and sets appropriate tick marks and labels on the x-axis of a Matplotlib plot based on the duration of a MIDI piece. It ensures the plot is easy to read and interpret by adjusting the frequency and format of the x-axis ticks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
The Matplotlib axes object to be modified. |
required |
piece
|
MidiPiece
|
The MIDI piece based on which the axis ticks and labels are adjusted. |
required |
Source code in fortepyan/view/pianoroll/main.py
Classes used for animated visualizations of piano rolls.
PianoRollScene
A class for creating and managing the scene of a piano roll animation.
Attributes:
| Name | Type | Description |
|---|---|---|
piece |
MidiPiece
|
The MIDI piece to be visualized. |
title |
str
|
Title of the piano roll scene. |
cmap |
str
|
Color map used for the visualization, default is "GnBu". |
axes |
list
|
List containing the matplotlib axes for the piano roll and velocity plots. |
content_dir |
Path
|
Directory path for storing temporary files. |
frame_paths |
list
|
List of paths where individual frame images are saved. |
figure |
Figure
|
The matplotlib figure object for the scene. |
roll_ax |
Axes
|
The axes for the piano roll plot. |
velocity_ax |
Axes
|
The axes for the velocity plot. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
piece
|
MidiPiece
|
The MIDI piece to be visualized. |
required |
title
|
str
|
Title of the piano roll scene. |
required |
cmap
|
str
|
Color map used for the visualization. Defaults to "GnBu". |
'GnBu'
|
Source code in fortepyan/view/animation/pianoroll.py
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
animate_part(part)
Animates a part of the MIDI piece.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
part
|
DataFrame
|
DataFrame containing time and counter information for frames. |
required |
Source code in fortepyan/view/animation/pianoroll.py
clean_figure()
draw(time)
Prepares the figure for drawing and invokes drawing of all axes for a specific time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
float
|
The time at which to draw the figure. |
required |
Source code in fortepyan/view/animation/pianoroll.py
draw_all_axes(time)
Draws both the piano roll and velocity plots at a specified time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
float
|
The time at which to draw the plots. |
required |
Source code in fortepyan/view/animation/pianoroll.py
draw_piano_roll(time)
Draws the piano roll plot at a specified time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
float
|
The time at which to draw the piano roll. |
required |
Source code in fortepyan/view/animation/pianoroll.py
draw_velocities(time)
Draws the velocity plot at a specified time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
float
|
The time at which to draw the velocity plot. |
required |
Source code in fortepyan/view/animation/pianoroll.py
prepare_animation_steps(framerate=30)
Prepare the data required for the animation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
framerate
|
int
|
Framerate for the animation (default is 30). |
30
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame containing time and counter for each frame. |
Source code in fortepyan/view/animation/pianoroll.py
render(framerate=30)
Render the animation using a single process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
framerate
|
int
|
Framerate for the animation, defaults to 30. |
30
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
Directory containing the generated animation content. |
Source code in fortepyan/view/animation/pianoroll.py
render_mp(framerate=30)
Renders the animation using multi-processing to speed up the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
framerate
|
int
|
Framerate for the animation, defaults to 30. |
30
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
Directory containing the generated animation content. |
Source code in fortepyan/view/animation/pianoroll.py
save_frame(savepath='tmp/tmp.png')
Saves the current state of the figure to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
savepath
|
str
|
Path where the image should be saved. Defaults to "tmp/tmp.png". |
'tmp/tmp.png'
|