Posts: 50
Threads: 21
Joined: Dec 2019
Apr-26-2021, 04:04 PM
(This post was last modified: Apr-26-2021, 04:19 PM by philipbergwerf.)
The situation is an alternative music notation app. I've created a program in which you edit the save file directly; lilypond like notation app. But now I want to create a more graphical app that does the same. Because I want it to import midi files I need some sort of save file format that contains all information like notes, staffsize, time signature, text, dynamic text, etc... I am a beginner in this and I hope there are some starting points on how to make a save file format?
I like a save format that can be opened by a text editor and something I really liked is the way Pure Data patches are written. So I am searching for something like that. A problem to overcome is for the program to be able to detect and edit a note-length for example. In my current program, the user is doing that. I do like to hear about save files. All I found were examples of really simple game save files that only contain basic game player info.
Useful links are much appreciated!
Screenshot from my current project: https://pasteboard.co/JZ8ylCR.png
A idea I came up with:
~score{
~staff{
~right{
~beam{id, start, end}
~note{id, note, start, end}
~note{id, note, start, end}
}
~left{
~beam{id, start, end}
~note{id, note, start, end}
~note{id, note, start, end}
}
~textanddynamics{
~dynamictext{id, text, start, xoffset, yoffset, bgcolor}
~crescendo{id, start, end}
}
}
}
Posts: 4,801
Threads: 77
Joined: Jan 2018
Apr-26-2021, 04:44 PM
(This post was last modified: Apr-26-2021, 04:44 PM by Gribouillis.)
philipbergwerf Wrote:something I really liked is the way Pure Data patches are written My search engine found only this specification of Pd file format. Using this format would mean writing a parser and a code generator for the format, which doesn't seem to be very easy because there is apparently no formal grammar specification.
I don't know much about music or music software programming, but you could perhaps simply store the data in Python structures such as dicts lists and tuples and write the repr() of these python objects in a file. It would be easy to write and to read for a python program. It could also be opened in a text editor.
For example the following Python tuple contains the same information as the above example, but it is directly loadable by a python program.
(
'score',
(
'staff',
(
'right',
('beam', id, start, end),
('note', id, note, start, end),
('note', id, note, start, end),),
(
'left',
('beam', id, start, end),
('note', id, note, start, end),
('note', id, note, start, end),),
(
'textanddynamics',
('dynamictext', id, text, start, xoffset, yoffset, bgcolor),
('creschendo', id, start, end)),))
philipbergwerf likes this post
Posts: 50
Threads: 21
Joined: Dec 2019
(Apr-26-2021, 04:44 PM)Gribouillis Wrote: philipbergwerf Wrote:something I really liked is the way Pure Data patches are written My search engine found only this specification of Pd file format. Using this format would mean writing a parser and a code generator for the format, which doesn't seem to be very easy because there is apparently no formal grammar specification.
I don't know much about music or music software programming, but you could perhaps simply store the data in Python structures such as dicts lists and tuples and write the repr() of these python objects in a file. It would be easy to write and to read for a python program. It could also be opened in a text editor.
For example the following Python tuple contains the same information as the above example, but it is directly loadable by a python program.
(
'score',
(
'staff',
(
'right',
('beam', id, start, end),
('note', id, note, start, end),
('note', id, note, start, end),),
(
'left',
('beam', id, start, end),
('note', id, note, start, end),
('note', id, note, start, end),),
(
'textanddynamics',
('dynamictext', id, text, start, xoffset, yoffset, bgcolor),
('creschendo', id, start, end)),)) If I want to read a python list from a txt file; How? Lets say we have this file as txt:
[item1, item2, item3] how do I read it as python list? can you make a simple example?
Posts: 4,801
Threads: 77
Joined: Jan 2018
Apr-26-2021, 07:27 PM
(This post was last modified: Apr-26-2021, 07:27 PM by Gribouillis.)
Here is a complete example using pprint() to write the file and ast.literal_eval() to evaluate the contents of the file. There are some limitations to this but it will work if the file contains only basic python data types and the structure is not too deeply nested. Furthermore it is safe against malicious data
import io
import ast
from pprint import pprint
mylist = [
'score',
(
'staff',
(
'right',
('beam', 877, 0, 12),
('note', 766, 'A', 13, 34),
('note', 988, 'C', 22, 100),),
(
'left',
('beam', 824, 7, 22),
('note', 'foo', 33, 876),
('note', 'bar', 55, 997),),
(
'textanddynamics',
('dynamictext', 'spam', 'corge', 31, 7, 8, 'blue'),
('crescendo', 'grault', 15, 188)),)]
def save(afile, obj):
pprint(obj, stream=afile, indent=4)
def load(afile):
s = afile.read().strip()
return ast.literal_eval(s)
def main():
myfile = io.StringIO()
save(myfile, mylist)
# let us see how the list was saved in a file
print('We saved:')
print(myfile.getvalue())
# let us now load the list from the file and see what we get
myfile.seek(0)
obj = load(myfile)
print()
print('We loaded:')
pprint(obj)
print('saved == loaded ?', mylist == obj)
if __name__ == '__main__':
main()
Posts: 8,166
Threads: 160
Joined: Sep 2016
You can look also at JSON. Python json module is part of the standard library
Posts: 4,801
Threads: 77
Joined: Jan 2018
Also there seems to be an ecosystem of Python modules that interact with Pure Data files. I found this module on Pypi that lets you write Pure Data patches from Python. It may be the way to go.
|