Jazz_Solo Course 5 week 1 as 3

Hi,
After finishing the Jazz_Solo assignment I downlaoded a .mid file online to create my own music. When creating the training and test set I get this error. Is the code only made for the Jazz file or did I do something wrong?


IndexError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/music21/stream/init.py in getitem(self, k)
402 try:
→ 403 match = self.elements[k]
404 except IndexError:

IndexError: tuple index out of range

During handling of the above exception, another exception occurred:

StreamException Traceback (most recent call last)
in
----> 1 X, Y, n_values, indices_values, chords = load_music_utils(‘data/orginal_metheny.mid’)
2 print(‘number of training examples:’, X.shape[0])
3 print(‘Tx (length of sequence):’, X.shape[1])
4 print(‘total # of unique values:’, n_values)
5 print(‘shape of X:’, X.shape)

~/work/W1A3/data_utils.py in load_music_utils(file)
18
19 def load_music_utils(file):
—> 20 chords, abstract_grammars = get_musical_data(file)
21 corpus, tones, tones_indices, indices_tones = get_corpus_data(abstract_grammars)
22 N_tones = len(set(corpus))

~/work/W1A3/preprocess.py in get_musical_data(data_fn)
133 def get_musical_data(data_fn):
134
→ 135 measures, chords = __parse_midi(data_fn)
136 abstract_grammars = __get_abstract_grammars(measures, chords)
137

~/work/W1A3/preprocess.py in __parse_midi(data_fn)
26 midi_data = converter.parse(data_fn)
27 # Get melody part, compress into single voice.
—> 28 melody_stream = midi_data[5] # For Metheny piece, Melody is Part #5.
29 melody1, melody2 = melody_stream.getElementsByClass(stream.Voice)
30 for j in melody2:

/opt/conda/lib/python3.7/site-packages/music21/stream/init.py in getitem(self, k)
405 raise StreamException(
406 f’attempting to access index {k} ’
→ 407 + f’while elements is of size {len(self.elements)}’
408 )
409 # setting active site as cautionary measure

StreamException: attempting to access index 5 while elements is of size 2

A MIDI file can encode several different channels of music within one file.
In the example used in the assignment, the channel with the melody is in channel 5.
For the file you uploaded, maybe the melody channel uses a different index.

Thanks for your answere. How do I get the channels from a midi file.

Even after changing the index I still get the same error. I even get it when I only try to print melody_stream out. Isnt the index defined in this line ? "melody_stream = midi_data[0] "

IndexError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/music21/stream/init.py in getitem(self, k)
402 try:
→ 403 match = self.elements[k]
404 except IndexError:

IndexError: tuple index out of range

During handling of the above exception, another exception occurred:

StreamException Traceback (most recent call last)
in
----> 1 X, Y, n_values, indices_values, chords = load_music_utils(‘data/orginal_metheny.mid’)
2 print(‘number of training examples:’, X.shape[0])
3 print(‘Tx (length of sequence):’, X.shape[1])
4 print(‘total # of unique values:’, n_values)
5 print(‘shape of X:’, X.shape)

~/work/W1A3/data_utils.py in load_music_utils(file)
18
19 def load_music_utils(file):
—> 20 chords, abstract_grammars = get_musical_data(file)
21 corpus, tones, tones_indices, indices_tones = get_corpus_data(abstract_grammars)
22 N_tones = len(set(corpus))

~/work/W1A3/preprocess.py in get_musical_data(data_fn)
133 def get_musical_data(data_fn):
134
→ 135 measures, chords = __parse_midi(data_fn)
136 abstract_grammars = __get_abstract_grammars(measures, chords)
137

~/work/W1A3/preprocess.py in __parse_midi(data_fn)
26 midi_data = converter.parse(data_fn)
27 # Get melody part, compress into single voice.
—> 28 melody_stream = midi_data[0] # For Metheny piece, Melody is Part #5.
29 melody1, melody2 = melody_stream.getElementsByClass(stream.Voice)
30 for j in melody2:

/opt/conda/lib/python3.7/site-packages/music21/stream/init.py in getitem(self, k)
405 raise StreamException(
406 f’attempting to access index {k} ’
→ 407 + f’while elements is of size {len(self.elements)}’
408 )
409 # setting active site as cautionary measure

StreamException: attempting to access index 5 while elements is of size 2

Perhaps converter.parse() did not like your midi file.
Sorry, but I don’t know any details on this package.
You may have to find and read the source code, and investigate how MIDI your file is structured.