Page 1 of 1

"Save any file" (Ruby)-How to extract the content?

Posted: Sun May 27, 2018 9:09 pm
by kortezzzz
Just another Ruby challenge :)

I've attached a nice Ruby module that have been posted few month a go on the forum. This module can "load and save any file", as it's title says. Well, the obvious question is how do we extract the file's content to FS, considering a given condition that it's extractable (integers, floats, strings, midi, you got the idea)?...

Any ideas?

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Mon May 28, 2018 2:29 am
by KG_is_back
what you need is to know how the file is structured. You read the file as a binary string ("rb" option). Then you use String method called .unpack to extract the data from the string in form of a ruby array. You can reverse the process too - use pack method on array to create binary string, which you can then save as a binary file.
There is extensive documentation on the pack method and how to use it.

Here is an example of save/load method that saves/loads 2x 16bit integers and arbitrarily long string of floats.

Code: Select all


def saveFile(path,int1,int2,floatarray)
file=File.new(path,"wb") #opens or creates new file in "write binary" mode
data=[int1,int2,*floatarray].pack("ssf*") #converts the data of ruby variables into binary string
file.write(data) #writes data to file
file.close #closes the file
end

def loadFile(path)
file=File.open(path,"rb") #opens the file in "read binary" mode
data=file.read #reads the data and puts it into "data" variable as a string
file.close
int1,int2,*floatarray=data.unpack("ssf*") #converts binary string into data of ruby variables
return [int1,int2,floatarray] #returns
end



To load something like a MIDI file, you need to find documentation on how are midi files structured. Then you must write subrutine than can extract that data from raw binary string of the file.

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Mon May 28, 2018 4:22 pm
by kortezzzz
Many thanks, KG

I understand what the code does, but there is I no chance I will write the full code of loading\saving a file correctly by my self. I do like to load\save midi files and there were few posts that shaw how to do load it, but they are beyond my skills.

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Mon May 28, 2018 10:35 pm
by KG_is_back
kortezzzz wrote:Many hanks, KG

I understand what the code does, but there is I no chance I will write the full code of loading\saving a file correctly by my self. I do like to load\save midi files and there were few posts that shaw how to do load it, but they are beyond my skills.


Sounds like a fun project :-D I had a quick look at midi file format and I may cook up something...

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Tue May 29, 2018 6:11 pm
by kortezzzz
KG_is_back wrote:
kortezzzz wrote:Many hanks, KG

I understand what the code does, but there is I no chance I will write the full code of loading\saving a file correctly by my self. I do like to load\save midi files and there were few posts that shaw how to do load it, but they are beyond my skills.


Sounds like a fun project :-D I had a quick look at midi file format and I may cook up something...


You made my day, KG :D
I think many members here are interested in this. That would be an amazing share. Thank you for your efforts.

And I'll be waching the forum like a hawk in the next few days :lol:

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Tue May 29, 2018 10:29 pm
by tulamide
There already is a good library for midi file reading and writing. It was programmed in pure Ruby, so can be used easily. Just copy the content of the lib folder from the link below to your Ruby extension folder. In Ruby then just use 'require midilib'. (This not just for you, kortezzzz, but KG might save a lot of time by altering existing stuff, compared to starting at zero)

https://github.com/jimm/midilib

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Tue May 29, 2018 11:04 pm
by kortezzzz
Thanks for the help, tulamide :)

When you guys are around, exciting things happen here 8-)

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Wed May 30, 2018 12:11 am
by KG_is_back
Flowstone only works with Channel Events and Sysex Events. I'm not sure how to handle System Common events and Meta Events, like tempo, song-start/end etc.

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Wed May 30, 2018 12:41 am
by DaveyBoy
This works for me KG:

Image

I don't know why we need the zero on the end but without it it comes out as sysex :?

Re: "Save any file" (Ruby)-How to extract the content?

Posted: Wed May 30, 2018 9:31 am
by kortezzzz
Here are 2 block I've collected from the forum. One is MyCo's share and I have no idea who is the other developer, so I can not credit.

Those are excellent starting points for decent midi file handling. MyCo's one only extract the full data when the internal sequancer starts to play. Seem like there is no "offline" access to the midi data.

The second one saves midi file according to given notes (in array) but no other midi data input allowed.

Was great if we could combine those two together :)