5

I'm new to writing questions here, so please feel free to point out how i can improve the quality of future questions!

Edit: More code included as was asked in the comments

I'm trying to read GuitarPro files into python. These files essentially contain the sheet music for songs, but contain more information than e.g. MIDI files.

I want to parse the notes and the duration of the notes into e.g. a list structure. Further, i hope other effects can be parsed from the GuitarPro files also, such as bends, slides, hammer-ons etc.

I have been trying to use the library PyGuitarPro, but get stuck:

import guitarpro
import os

# 'wet_sand.gp5' is the guitar pro file
parsed_song = guitarpro.parse('wet_sand.gp5')
song = guitarpro.gp5.GP5File(parsed_song,encoding='UTF-8')
song.readSong()

I get the following error from ReadSong() (documentation here):

Traceback (most recent call last):

  File "<ipython-input-15-e1663229852d>", line 8, in <module>
    song.readSong()

  File "C:\Python27\lib\site-packages\guitarpro\gp5.py", line 62, in readSong
    song.version = self.readVersion()

  File "C:\Python27\lib\site-packages\guitarpro\iobase.py", line 114, in readVersion
    self.version = self.readByteSizeString(30)

  File "C:\Python27\lib\site-packages\guitarpro\iobase.py", line 97, in readByteSizeString
    return self.readString(size, self.readByte())

  File "C:\Python27\lib\site-packages\guitarpro\iobase.py", line 47, in readByte
    return (self.read(*args, default=default) if count == 1 else

  File "C:\Python27\lib\site-packages\guitarpro\iobase.py", line 35, in read
    data = self.data.read(count)

AttributeError: 'Song' object has no attribute 'read'
Malte Jensen
  • 187
  • 1
  • 11
  • You can improve this question by posting the exact code that is causing the error, and the full text (copy and pate it in) of the error including the entire traceback. You should also provide enough data that someone could reproduce your problem as an [MCVE](https://stackoverflow.com/help/mcve) – Craig Nov 23 '18 at 18:15
  • If you keep getting an error, the error message (and possibly trace back) will help debug it. It’s hard to help if you don’t post the message. – Dietrich Epp Nov 23 '18 at 18:16
  • 1
    I have updated the code now with the error, hope it's better now. – Malte Jensen Nov 24 '18 at 10:03

1 Answers1

4

Looking at the the examples provided, e.g. this one. I don't think you need this portion.

song = guitarpro.gp5.GP5File(parsed_song,encoding='UTF-8')

The following should be enough, as parse already calls readSong here.

song = guitarpro.parse('wet_sand.gp5')

Finally it looks like the file-format is automatically determined by parse here.

As an example you could do something like this.

import guitarpro
song = guitarpro.parse('test.gp5')

for track in song.tracks:
    for measure in track.measures:
        for voice in measure.voices:
            for beat in voice.beats:
                for note in beat.notes:
                    print(note.durationPercent)
                    print(note.effect)
eandersson
  • 25,781
  • 8
  • 89
  • 110
  • Maybe look at some of the examples? e.g. https://github.com/Perlence/PyGuitarPro/blob/develop/examples/dfh.py#L29 – eandersson Nov 24 '18 at 11:35
  • Thanks for the answer. However I cannot get the notes, durations, and effects out from this as far as i can see (I might be wrong here). The whole point is that I only want: the notes in order, the duration of each note and an optional effect (bends, slides, etc.). Hope this makes sense. If i parsed a midi-file, i cannot get effects such as bends and slides. – Malte Jensen Nov 24 '18 at 11:40
  • Do you have an example GP5 File I could check? but I would create a new question at Stackoverflow for this. Ideally each question should be focused on a very specific problem. The traceback should have been solved by the my answer, but your follow up question just seems like a new issue to me. – eandersson Nov 25 '18 at 00:54
  • @eanderson I will start a new thread then. The questions was meant from the beginning to be: "how do i extract only notes from a .gp5 file with the package i reference", but I see that this might not have been clear enough. Please bear in mind that I'm new and I'm trying to learn how to ask good questions. Thanks for the help so far! – Malte Jensen Nov 26 '18 at 11:14