Python Forum
How to design a save file format?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to design a save file format?
#1
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}
		}
	}
}
Reply
#2
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
Reply
#3
(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?
Reply
#4
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()
Reply
#5
You can look also at JSON. Python json module is part of the standard library
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 312 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 539 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,200 Jul-07-2023, 05:44 PM
Last Post: flash77
  Save and Close Excel File avd88 0 2,991 Feb-20-2023, 07:19 PM
Last Post: avd88
  Save multiple Parts of Bytearray to File ? lastyle 1 936 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  How to format EasyOCR results in order to save to CSV? cubangt 0 1,248 Sep-02-2022, 02:06 PM
Last Post: cubangt
Sad Want to Save Print output in csv file Rasedul 5 10,895 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Reshape txt file into particular format using python shantanu97 0 1,422 Dec-10-2021, 11:44 AM
Last Post: shantanu97
  How can we transcode encoding file uml url format Anldra12 9 3,376 Jul-25-2021, 09:30 AM
Last Post: Anldra12
  How to save Matplot chart to temp file? Morkus 2 4,502 Jun-12-2021, 10:52 AM
Last Post: Morkus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020