Home › Forums › General › Programming › saving/loading data to a file
- This topic has 47 replies, 8 voices, and was last updated 7 years, 9 months ago by goodweather.
-
AuthorPosts
-
October 1, 2012 at 9:42 pm #728
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
October 1, 2012 at 10:30 pm #4828An 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 locationfile = 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 wellfile = File (file:withFileExtension(String ("mww")))
endif file:hasWriteAccess() then
— We can write to that locationfile:replaceFileContentWithData(dataToSave)
endend
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()
endreturn 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]October 2, 2012 at 2:30 pm #4829I’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?October 2, 2012 at 2:42 pm #4830Read 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]October 2, 2012 at 6:38 pm #4831Shit, it was in front of my face but I didn’t saw it. Thx
October 3, 2012 at 12:30 am #4832LMFAO..
I dunno. what WOULD John McCain do??nice easter egg, atom. thanks man.
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
October 3, 2012 at 9:43 pm #4833ugh.
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 ” title=”Neutral” />
Monstrum Media | Music, Sound & Software Design, Chicago / San Francisco listen
October 3, 2012 at 10:03 pm #4834[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).
October 8, 2012 at 11:18 pm #4835I 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")))
endif file:hasWriteAccess() then
file:replaceFileContentWithData(dataToSave)
endend
— 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
October 8, 2012 at 11:21 pm #4836Good 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
February 15, 2013 at 4:58 pm #6385I 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.February 15, 2013 at 5:06 pm #6388BTW, 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…
February 15, 2013 at 5:35 pm #6390Well 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).
February 15, 2013 at 8:06 pm #6393OK, thanks for the quick reply! Maybe time to update the documentation that comes with the Win install of Ctrlr?
February 16, 2013 at 3:31 pm #6402I 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.February 16, 2013 at 5:21 pm #6406Show 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.
February 16, 2013 at 5:59 pm #6407OK, 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!)
February 16, 2013 at 6:04 pm #6408Yeah ok but post the link to the code ?
February 16, 2013 at 7:08 pm #6419Hopefully the right link: http://pastebin.com/VDMPkTKu
February 16, 2013 at 7:19 pm #6421Replace CtrlrLuaMemoryBlock with MemoryBlock like i wrote there were API changed to Ctrlr.
-
AuthorPosts
- The forum ‘Programming’ is closed to new topics and replies.