The SWI-Prolog-Janus-Python-Mido interface and timers are working fine, but now I’m testing Mido’s MIDI file creation. I’m trying to translate the following Python code fragment to SWI-Prolog with Janus calls and I need some help. The following Python script works and creates a file with one note.
from mido import Message, MidiFile, MidiTrack
mid = MidiFile(type=1)
track = MidiTrack()
mid.tracks.append(track)
track.append(Message('program_change', program=12, time=0))
track.append(Message('note_on', note=64, velocity=64, time=32))
track.append(Message('note_off', note=64, velocity=127, time=32))
mid.save('new_song.mid')
Here is what I have in Prolog using janus :
:- use_module(library(janus)).
start :-
py_call(mido:'MidiFile'(type=1), Outfile),
writeln(outfile(Outfile)),
py_call(mido:'MidiTrack'(), Track),
writeln(track(Track)),
py_call(Outfile:tracks:append(eval(mido:'MidiTrack'(Track)))),
py_call(Track:append(eval(mido:'Message'(program_change, program=12, time=0)))),
py_call(Track:append(eval(mido:'Message'(note_on, note=64, velocity=127, time=32)))),
py_call(Track:append(eval(mido:'Message'(note_off, note=64, velocity=127, time=32)))),
py_call(Outfile:save(eval(mido:'MidiFile'(file='C:/docs/new_song.mid')))).
The fourth py_call results in an error. The return value Track seems to be an empty list instead of an object.
?- start.
outfile(<py_MidiFile>(000001f317e7bad0))
track([])
ERROR: Type error: `py_callable' expected, found `[]' (an empty_list)
ERROR: In:
ERROR: [11] janus:py_call([]:append(...))
ERROR: [10] start at c:/....pl:128
ERROR: [9] toplevel_call('<garbage_collected>') at c:/program files/swipl/boot/toplevel.pl:1173
The last call results in another error. My syntax might be wrong.
?- start.
outfile(<py_MidiFile>(0000025ee796bad0))
ERROR: Python 'AttributeError':
ERROR: 'str' object has no attribute 'read'
ERROR: In:
ERROR: [11] janus:py_call(<py_MidiFile>(0000025ee796bad0):save(...))
ERROR: [9] toplevel_call('<garbage_collected>') at c:/program files/swipl/boot/toplevel.pl:1173