If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
"Save any file" (Ruby)-How to extract the content?
10 posts
• Page 1 of 1
"Save any file" (Ruby)-How to extract the content?
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?
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 953 times
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: "Save any file" (Ruby)-How to extract the content?
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.
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.
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.
- 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?
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.
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.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: "Save any file" (Ruby)-How to extract the content?
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 I had a quick look at midi file format and I may cook up something...
- 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?
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 I had a quick look at midi file format and I may cook up something...
You made my day, KG
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
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: "Save any file" (Ruby)-How to extract the content?
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
https://github.com/jimm/midilib
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: "Save any file" (Ruby)-How to extract the content?
Thanks for the help, tulamide
When you guys are around, exciting things happen here
When you guys are around, exciting things happen here
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: "Save any file" (Ruby)-How to extract the content?
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.
- 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?
This works for me KG:
I don't know why we need the zero on the end but without it it comes out as sysex
I don't know why we need the zero on the end but without it it comes out as sysex
-
DaveyBoy - Posts: 131
- Joined: Wed May 11, 2016 9:18 pm
- Location: Leeds UK
Re: "Save any file" (Ruby)-How to extract the content?
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
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 925 times
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
10 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 63 guests