File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed
Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -160,10 +160,18 @@ def tempo_segments(self) -> List[float]:
160160
161161 return tempo_segments
162162
163+ def get_layer (self , id : int ) -> pynbs .Layer :
164+ """Return the layer with the given ID."""
165+ # The song may not have a layer object for every layer with a note block
166+ try :
167+ return self .layers [id ]
168+ except IndexError :
169+ return pynbs .Layer (id = id )
170+
163171 def weighted_notes (self ) -> Iterator [Note ]:
164172 """Return all notes in this song with their layer velocity and panning applied."""
165173 for note in self .notes :
166- layer = self .layers [ note .layer ]
174+ layer = self .get_layer ( note .layer )
167175 custom_instrument_id = note .instrument - self .header .default_instruments
168176 if custom_instrument_id >= 0 :
169177 instrument = self .instruments [custom_instrument_id ]
@@ -190,7 +198,7 @@ def notes_by_layer(
190198 song. If `group_by_name` is true, notes in layers with identical names will be grouped."""
191199 groups = {}
192200 for note in self .weighted_notes ():
193- layer = self .layers [ note .layer ]
201+ layer = self .get_layer ( note .layer )
194202 group_name = layer .name if group_by_name else f"{ layer .id } { layer .name } "
195203 if group_name not in groups :
196204 groups [group_name ] = []
You can’t perform that action at this time.
0 commit comments