Find the length of a wav file in Ruby

I needed a way to find out quickly the length of the wav file. in a previous version, I was simply guessing it from the file size, saying that a wav file is about a megabyte per minute. but that ain't the proper way. Thanks to this site giving me the wav file header. I could quickly write my own in Ruby.

def wavinfo(input)

    def bytes_to_int(bytes)
            bytes.unpack("L").first
    end

    # try to load a file form the filesystem unless we got a data stream as a parameter
    unless input[0..3] == 'RIFF'
            file = File.new(input,"r")
            input = file.read(45)
            file.close
    end

    return {
            :filesize => (8 + bytes_to_int(input[4..7])),
            :wavefmt => input[8..15],
            :format => bytes_to_int(input[16..19]),
            :pcm => bytes_to_int(input[20..21]),
            :channels => bytes_to_int(input[22..23]),
            :frequency => bytes_to_int(input[24..27]),
            :bytes_per_second => bytes_to_int(input[28..31]),
            :bytes_by_capture => bytes_to_int(input[32..33]),
            :bits_per_sample => bytes_to_int(input[34..35]),
            :always_data => input[36..39],
            :bytes_in_data => bytes_to_int(input[40..43]),
            :wav_length => bytes_to_int(input[40..43]).to_f / bytes_to_int(input[28..31]),
    }

end

hope this helps you

Tags , , , , ,

Posted in , , | Posted on 16 Feb 2011 06:52by somekool | no comments

Do you HATE your programming language?

As many of you already know, Google recently released their Code search. After reading about Code search ideas and found Expression of which programming language sucks more. For instance, Python sucks. (thx, paolo). I thought, actually its not about which language suck the most. but what language are the programmer the less Happy with. So I checked a few of them. I'm sure the huge number next to C is related to the fact tons of C code is open sourced and C is also the oldest. but there is a huge difference there.

But finding "qt sucks" in kdelibs source is a bit of a shame. at least for me who literaly love Qt and KDE. Qt (total 11), KDE (total 2)

I am a bit disapointed to find almost none MFC Sucks. It's probably just because there is not much open MFC code. Well, I hope so.

But what I found very interesting and why I am actually posting this. Is because ..... (roulement de tambour)..... No one, programming in Ruby thinks that ruby sucks. For few very simple and straight reasons.... ruby rocks, i love ruby and ruby is fantastic

so go ahead and learn Ruby

and don't forget that people DO love their job and more

Tags , , , , , , , , , , , , , , , , , ,

Posted in , , | Posted on 12 Oct 2006 19:22by somekool | no comments

Bad programming practices(tm)

Your coming into a forum or an IRC channel, you ask a simple question, yet uncommon. and people are screaming. “Stop, Don’t do it!”, “it’s evil”, “these are bad programming practices, prove me you can’t do without”. And such, before you had time to say more than two lines.

But why is everyone trying to preach absolutely nothing without knowing a thing. They have no clue what I am working on. What would I do with this bit of information and what particular problem I’m trying to solve. but yet, they know it’s evil and I should not do it. Hell, I’m not even sure what I’m gonna do yet. I’m looking for different solutions. What exactly is evil? I have not thought of anything yet.

for people who know C and used to pointer basics have a quick look at the following example.

void foo(&var) {
  var = 42;
}
x = 0;
printf("%d\n", x);
foo(x);
printf("%d\n", x);

I was simply asking if passing a variable by reference in such a way would also be possible in [Ruby](http://ruby-lang.org). I have been refered, among other things, to an excellent page documenting [Ruby bindings](http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print). I am not sure yet if bindings is the only way to go in order to achieve something as simple as this. but what I do know, though: “ITS EVIL”…

actually, I knew Ruby bindings before. But I wanted to use something simpler, more embedded into the syntax. I thought hearing something different, more C-like that I could use. but getting actual help through all the flames was a challenge on its own. so it’s all I got.

yet, I’m not sure I’ll use it for my particular problem, for one, I was looking for something different and for two, well, its EVIL apprently. And god knows I don’t do EVIL ;)

I got another example, this one happen few months ago. I’m heading into the ruby channel again asking for a ruby+japanese related questions. no anwser, total silent. I figured nobody knows as they are almost all english and don’t deal with japanese stuff. but since Ruby programming language in born in Japan and the japanese community is quite active, I thought asking in Japanese. suddenly I got an answer.

the first one, in japanese was simply answering my question. politely and perfectly. the second one? just english screaming. “This is an english channel, talk in english, this is not a japanese or internation channel, stop, its EVIL.”

god, what the hell. you use computers and ruby you should at least appreciate the multi-cultural side the internet offers us. well, he was probably scared I was flaming about his grand-mother. what else could I think?

anyone has a decent explanation for such behavours? am I the one who is weird/wrong?

Tags , , , , , ,

Posted in , , , , | Posted on 12 Sep 2006 02:01by somekool | no comments

Couple of Rails links

Here is a couple of links about rubyonrails performance and m2m features I ran into today. I just want to keep them as I do not have time to go through them all fully and assimilate everthing.

about performance

about has_many :through

My favorite tooltip js library

Tags , , , , , , ,

Posted in , , , | Posted on 05 Sep 2006 22:28by somekool | no comments

kikkanji

Here is my latest toy. I made myself a Kanji studying tool. It took me only few hours to build using QtRuby. I find it pretty amazing what I built in this short amount of time even though I don't have much experience with this technology. It is a really impressive technology.

I build up a small kexi (sqlite3) database, which I open with ActiveRecord. I could have used Qt's SQL module, but I'm so used to AR that I did not bother.

I named it kikkanji because I first intended to create a kicker panel applet. but, panel applets are not yet possible with QtRuby. so I had to wait. I instead used Qt Designer and created myself a main window.

kikkanji

download now

comments and feature request are definitely welcome.

Tags , , , , , , , ,

Posted in , , , , , , , , , | Posted on 16 May 2006 21:53by somekool | 1 comment

Subscribe

my last twitter posts

Personal Links

Tags

Categories

Archives

my Xbox 360 gamertag

Copyright © Mathieu Jobin's Life and Thoughts