Search code examples
tensorflowmagenta

NoteSequence Protobuf Decode Error


I'm trying to import a NoteSequence file into Magenta and am getting 'google.protobuf.message.DecodeError: Unexpected end-group tag.'

in.seq

id: "/id/midi/tmp/3d8d5785f488ffd8875f10a12858c6f6c2152068"
filename: "example.mid"
collection_name: "tmp"
ticks_per_beat: 220
time_signatures {
  numerator: 4
  denominator: 4
}
key_signatures {
}
tempos {
  bpm: 240.0
}
notes {
  pitch: 60
  velocity: 100
  end_time: 0.2375
}
notes {
  pitch: 62
  velocity: 100
  start_time: 0.25
  end_time: 0.4875
}
notes {
  pitch: 64
  velocity: 100
  start_time: 0.5
  end_time: 0.7375
}
notes {
  pitch: 65
  velocity: 100
  start_time: 0.75
  end_time: 0.9875
}
notes {
  pitch: 67
  velocity: 100
  start_time: 1.0
  end_time: 1.2375
}
notes {
  pitch: 69
  velocity: 100
  start_time: 1.25
  end_time: 1.4875
}
notes {
  pitch: 71
  velocity: 100
  start_time: 1.5
  end_time: 1.7375
}
notes {
  pitch: 72
  velocity: 100
  start_time: 1.75
  end_time: 1.9875
}
total_time: 1.9875

get_seq.py

from os import path  
import sys  
import os  

sys.path.append( path.relpath("../../bazel-out/local-fastbuild/bin/magenta/convert_midi_dir_to_note_sequences.runfiles/") )  

import midi_io  
import note_sequence_io  
import pretty_midi  
import protobuf  
import tensorflow as tf  

file_path = path.relpath("../../out/conv/in.seq")  

sys.path.append( path.relpath("../../bazel-genfiles/magenta/protobuf") )  
import music_pb2  

def read_file_generator(filename):  
    f = open(filename, 'r')  

    for line in f:  
            yield line.rstrip('\n')  
    f.close()  

generator = read_file_generator(file_path)  

for i in generator:  
    print music_pb2.NoteSequence.FromString(i) 

This should print lines like:
d451 3640 21d7 bad4 089d e736 4038 0140

How do I retrieve the expected protobufs?


Solution

  • FromString expects a serialized protobuf, not one in ASCII format. You can parse from ASCII with a function such as this:

    from google.protobuf import text_format
    
    def parse_test_proto(proto_type, proto_string):
      instance = proto_type()
      text_format.Merge(proto_string, instance)
      return instance