Search code examples
pythonguitar

Python: Reading GuitarPro (.gp5) files


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'

Solution

  • 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)