saving/loading data to a file

Home Forums General Programming saving/loading data to a file

Viewing 20 posts - 1 through 20 (of 48 total)
  • Author
    Posts
  • #728
    msepsis
    Participant
    • Topics: 219
    • Replies: 732
    • Total: 951
    • ★★★

    I’m working on a number of different things at once, it’d save me a bit of trial and error if I knew beforehand how to save data pulled from a midi message to a file, then how to load that data from a file within my panel.

    First, say I’m storing bytes7 to 134 to a variable called WDATA after a specific midi message is received. I’ve got all this down. Here’s how I’m storing the data:

    [code:1gydx2y0] WDATA = midiMessage:getLuaData():getRange(7,134)[/code:1gydx2y0]

    What now do I need to do in order to save the contents of "WDATA" to a file? Can I/How designate within the code what file extension to use? Here I’d like to save the data to my own ".mww" file.

    Then suppose I load one of these mww files to my panel. How do I pass the data from the file to a variable named "WDATAedited"?

    Many thanks!

    Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen

    #4828
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    An example on how to read,write,ask for files (this will ask you for a file to save, make sure it has the right extensions, write some data, then try to read the data from that file and display it on screen):
    [code:1ki5rrnd]

    function saveToFile(dataToSave)

    — By default the dialog window will ask for confirmation on overwrites, so we don’t need to check
    — You can replace the File("").getSpecialLocation(File.userDesktopDirectory) with some other file location
    — that can be remembered across calls so that you hint the user his last browsed dir or some other location

    file = utils.saveFileWindow ("Save file", File("").getSpecialLocation(File.userDesktopDirectory), "*.mww", true)

    if not file:hasFileExtension(String ("mww")) then
    — this is stupid the File() should not be necessary but luabind does not handle type conversions well

    file = File (file:withFileExtension(String ("mww")))
    end

    if file:hasWriteAccess() then
    — We can write to that location

    file:replaceFileContentWithData(dataToSave)
    end

    end

    function loadFromFile()

    file = utils.openFileWindow("Open file", File("").getSpecialLocation(File.userDesktopDirectory), "*.mww", true)

    if file:existsAsFile() then
    — We can read it now
    return file, file:loadFileAsData()
    end

    return nil
    end

    — This is a test write some bytes to the file
    saveToFile (CtrlrLuaMemoryBlock ({0x57, 0x48, 0x41, 0x54, 0x20, 0x57, 0x4f, 0x55, 0x4c, 0x44, 0x20, 0x4a, 0x4f, 0x48, 0x4e, 0x20, 0x4d, 0x43, 0x43, 0x4c, 0x41, 0x49, 0x4e, 0x20, 0x44, 0x4f, 0x3f}))

    — This is a test to read some data
    fileObject,data = loadFromFile()
    utils.infoWindow (string.format ("File read from: %s", stringToLua(fileObject:getFullPathName())), data:toSafeString())
    [/code:1ki5rrnd]

    #4829
    dasfaker
    Keymaster
    • Topics: 80
    • Replies: 793
    • Total: 873
    • ★★★

    I’ve modified you example to fit my needs and now I have the following

    [code:35wcogil] configFile = utils.saveFileWindow ("Config File", File("").getSpecialLocation(File.userDesktopDirectory), "ini", true)

    config_data = CtrlrLuaMemoryBlock()
    config_data:append(expansionDValue)
    config_data:append(expansionEValue)
    console (config_data:toHexString(1))

    if configFile:existsAsFile() then
    configFile:replaceFileContentWithData(config_data)
    else
    configFile:create()
    configFile:replaceFileContentWithData(config_data)
    end[/code:35wcogil]

    This works fine, the file is created and store the data, but I don’t want to ask for the file, so I change the code to this

    [code:35wcogil] configFile = File("").getSpecialLocation(File.userApplicationDataDirectory):getChildFile(String("Config.ini"))

    config_data = CtrlrLuaMemoryBlock()
    config_data:append(expansionDValue)
    config_data:append(expansionEValue)
    console (config_data:toHexString(1))

    if configFile:existsAsFile() then
    configFile:replaceFileContentWithData(config_data)
    else
    configFile:create()
    configFile:replaceFileContentWithData(config_data)
    end[/code:35wcogil]
    This time, the file is created but when I try to store the data Ctrlr crash. Any idea?

    #4830
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    Read the comments!
    [code:2kdv0a13]
    — this is stupid the File() should not be necessary but luabind does not handle type conversions well
    [/code:2kdv0a13]

    so your code needs the first line to be changed:
    [code:2kdv0a13]
    configFile = File (File("").getSpecialLocation(File.userApplicationDataDirectory):getChildFile(String("Config.ini")))
    [/code:2kdv0a13]

    #4831
    dasfaker
    Keymaster
    • Topics: 80
    • Replies: 793
    • Total: 873
    • ★★★

    Shit, it was in front of my face but I didn’t saw it. Thx

    #4832
    msepsis
    Participant
    • Topics: 219
    • Replies: 732
    • Total: 951
    • ★★★

    LMFAO..
    I dunno. what WOULD John McCain do??

    nice easter egg, atom. thanks man.

    Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen

    #4833
    msepsis
    Participant
    • Topics: 219
    • Replies: 732
    • Total: 951
    • ★★★

    ugh.
    So I thought I had it, then ctrlr crashed and I lost all my customization to your script above..
    Like Dasfaker I don’t want to save a file and load it with predefined contents all in one shot.

    I need to pass the contents of my variable "WDATA" to the file created with this script.

    WDATA is defined by:

    WDATA = midiMessage:getLuaData():getRange(7,134).

    What do I need to do in order to alter your line:
    [code:23ebywxp]saveToFile (CtrlrLuaMemoryBlock ({0x57, 0x48, 0x41, 0x54, 0x20, 0x57, 0x4f, 0x55, 0x4c, 0x44, 0x20, 0x4a, 0x4f, 0x48, 0x4e, 0x20, 0x4d, 0x43, 0x43, 0x4c, 0x41, 0x49, 0x4e, 0x20, 0x44, 0x4f, 0x3f}))[/code:23ebywxp]
    to instead save the contents of variable "WDATA" to the saved file?

    ugh.. alergy/cold medicine is doing wonders to my ability to concentrate today <img loading=” title=”Neutral” />

    Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen

    #4834
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    [code:2ihljg1y]
    saveToFile(WDATA);
    [/code:2ihljg1y]

    saveToFile is a function i wrote that writes CtrlrLuaMemoryBlock to a file

    the actual data saving hapens here:
    [code:2ihljg1y]
    file:replaceFileContentWithData(dataToSave)
    [/code:2ihljg1y]

    if you have a valid FIle() object you can use that method replaceFileContentWithData() to save contents of a CtrlrLuaMemoryBlock to that file (it overwrites the contents of the file).

    #4835
    msepsis
    Participant
    • Topics: 219
    • Replies: 732
    • Total: 951
    • ★★★

    I haven’t had much time to work on this but still having trouble. Not working. Here’s what I’ve got based on what you’ve provided, atom:

    [code:85c53zlq]saveSomeData = function(modulator, newValue)
    function saveToFile(WDATA)

    file = utils.saveFileWindow ("Save file", File("").getSpecialLocation(File.userDesktopDirectory), "*.mww", true)

    if not file:hasFileExtension(String ("mww")) then
    file = File (file:withFileExtension(String ("mww")))
    end

    if file:hasWriteAccess() then
    file:replaceFileContentWithData(dataToSave)
    end

    end

    — write the contents of "WDATA" variable to the file
    saveToFile (WDATA)

    end[/code:85c53zlq]

    any further help here would be great.. I have no experience with using CtrlrLuaMemoryBlock… I have a hunch I’m missing a step where I need to convert my variable’s value to a hex string?? Anything in my code above stand out as blatantly wrong? I might have misunderstood your instructions, atom..
    Thx!

    Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen

    #4836
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    Good hint, don’t define a function inside a function do it outside the funtion’s body (after the END that ends your function).
    Assuming your function is defined as:
    [code:17qky3q2]
    function = saveToFile(WDATA)
    end
    [/code:17qky3q2]

    you can’t call:
    [code:17qky3q2]
    file:replaceFileContentWithData(dataToSave)
    [/code:17qky3q2]

    cause dataToSave is an invalid variable call
    [code:17qky3q2]
    file:replaceFileContentWithData(WDATA)
    [/code:17qky3q2]

    cause that’s what you are passing as the first argument of the function

    #6385
    SWB
    Participant
    • Topics: 35
    • Replies: 157
    • Total: 192
    • ★★

    I have copied the example code written by atom in this thread. I have a button on my panel which calls function ‘test1’ and this function calls ‘saveToFile’ with the data from the example:
    function test1()
    saveToFile (CtrlrLuaMemoryBlock ({0x57, 0x48, 0x41, 0x54, 0x20, 0x57, 0x4f, 0x55, 0x4c, 0x44, 0x20, 0x4a, 0x4f, 0x48, 0x4e, 0x20, 0x4d, 0x43, 0x43, 0x4c, 0x41, 0x49, 0x4e, 0x20, 0x44, 0x4f, 0x3f}))
    end
     When executed it shows a dialog which asks for a filename to save (with extension ‘mww’). So far so good. But when I click OK I get ‘a nil value’ error. See attached screen print. What could be the problem?

    Attachments:
    You must be logged in to view attached files.
    #6388
    SWB
    Participant
    • Topics: 35
    • Replies: 157
    • Total: 192
    • ★★

    BTW, I did this in Win7. I tried this also in OSX. I started Ctrlr without any panel (trashed preferences first) and then opened this panel. Immediately (so not even showing my panel) it shows a Save file dialog window. I enter a name, click OK and Ctrlr crashes…

    #6390
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    Well the error says it all, you are trying to call a method that’s not there, use replaceWithData the example above is before i updated the API to use direct JUCE calls (it’s 4 months old).

    #6393
    SWB
    Participant
    • Topics: 35
    • Replies: 157
    • Total: 192
    • ★★

    OK, thanks for the quick reply! Maybe time to update the documentation that comes with the Win install of Ctrlr?

    #6402
    SWB
    Participant
    • Topics: 35
    • Replies: 157
    • Total: 192
    • ★★

    I have the feeling I’m still stumbling in the dark, or at best in the twilight zone 😉 with Ctrlr. I don’t seem to get the hang of it, especially when I try to do real programming stuff. No problems when dealing with Lua scripting relating to knobs, sliders and MIDI. But for example this file saving and loading stuff is really frustrating me, because nowhere can I find information on how to write the right code lines. I used ‘replaceWithData’ but then I get an error message which to me looks like something from outer space ;-). See attached screen print.

    Attachments:
    You must be logged in to view attached files.
    #6406
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    Show me your code i’ll tell you what’s wrong, provide all the functions needed, use pastebin.com for more then 3 lines of code.

    #6407
    SWB
    Participant
    • Topics: 35
    • Replies: 157
    • Total: 192
    • ★★

    OK, code added to pastebin.com. (Expires after one day.) Title: ‘save/load data in CTRLR’. (I didn’t know this site, nice and handy, thanks!)

    #6408
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    Yeah ok but post the link to the code ?

    #6419
    SWB
    Participant
    • Topics: 35
    • Replies: 157
    • Total: 192
    • ★★

    Hopefully the right link: http://pastebin.com/VDMPkTKu

    #6421
    atom
    Keymaster
    • Topics: 159
    • Replies: 2945
    • Total: 3104
    • ★★★★★

    Replace CtrlrLuaMemoryBlock with MemoryBlock like i wrote there were API changed to Ctrlr.

Viewing 20 posts - 1 through 20 (of 48 total)
  • The forum ‘Programming’ is closed to new topics and replies.
There is currently 0 users and 73 guests online
No users are currently active
Forum Statistics
Threads: 2,495, Posts: 17,374, Members: 77,605
Most users ever online was 12 on January 22, 2019 3:47 pm
Ctrlr