From: Mathieu Bouchard Date: 2001-03-13T17:04:25+09:00 Subject: [ruby-talk:12560] Re: audio recording from soundcard On Tue, 13 Mar 2001, Sm�rk wrote: > hi, > is it possible to record audio from a sound card with ruby? Hi, I have not tried that yet, but I just wrote this code to play a 440 Hz tone to a sound card. Requires Linux, though. module LinuxSound PCM_WRITE_BITS = 0xc0045005 PCM_WRITE_CHANNELS = 0xc0045006 PCM_WRITE_RATE = 0xc0045002 end class IO; def ioctl_int_ptr(arg1,arg2) tmp = arg2 * 2**32 foo = [2*tmp.id + 16].pack("l").unpack("P4")[0] # if it crashes, just insert foo.reverse! here. tmp_ptr = (foo[0]+0x100*foo[1])+0x10000*(foo[2]+0x100*foo[3]) ioctl(arg1,tmp_ptr+4) end end sound = File.open("/dev/dsp","w") sound.ioctl_int_ptr(LinuxSound::PCM_WRITE_BITS,8) sound.ioctl_int_ptr(LinuxSound::PCM_WRITE_CHANNELS,1) sound.ioctl_int_ptr(LinuxSound::PCM_WRITE_RATE,44100) wavelength = 100 sample = "" wavelength.times {|x| y = Math.sin(x*2*Math::PI/wavelength) sample << (128+127*y).to_i.chr } sample *= 16 while true sound.write sample end matju