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

For general discussion related FlowStone
Post Reply
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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?
Attachments
(save_load any file).fsm
(146.2 KiB) Downloaded 1029 times
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Post 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.
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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.
Last edited by kortezzzz on Tue May 29, 2018 5:22 pm, edited 1 time in total.
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Post 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...
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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:
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Post 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
"There lies the dog buried" (German saying translated literally)
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post by kortezzzz »

Thanks for the help, tulamide :)

When you guys are around, exciting things happen here 8-)
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Post 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.
User avatar
DaveyBoy
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

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

Post 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 :?
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

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

Post 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 :)
Attachments
( midi file containers ).fsm
(31.88 KiB) Downloaded 1002 times
Post Reply