#!/usr/bin/ruby # soundtest -- demonstrate the interface to the # microphone, speaker, and buttons on the IPAQ SNDCTL_DSP_SPEED = 0xc0045002 SNDCTL_DSP_CHANNELS = 0xc0045006 SNDCTL_DSP_SETFMT = 0xc0045005 AFMT_U8 = 8 AFMT_S16_LE = 16 # We: wait for a button press # record from the microphone # when the button is released # we play the recorded data # open the touchscreen keyFile = File.open("/dev/touchscreen/key", "r") ch = keyFile.getc if (ch == 1) puts "Button pressed" # button on upper left side of IPAQ # if a button is pressed, open the microphone dsp = File.open("/dev/dsp", "r") dsp.ioctl(SNDCTL_DSP_SPEED, [16000].pack("i")) dsp.ioctl(SNDCTL_DSP_CHANNELS, [2].pack("i")) # do we need this? only for storing as a file, not for immediate playback # dsp.ioctl(SOUND_PCM_READ_RATE, [0].pack("i")) dsp.ioctl(SNDCTL_DSP_SETFMT, [AFMT_S16_LE].pack("i")) samples = [] while !Kernel.select([keyFile], nil, nil, 0) or keyFile.getc != 129 # read from the microphone until the key is released samples.push dsp.read(2000) end puts "Button unpressed" # close the microphone for reading dsp.close # open the speakers # this should be open-write-only, but not clear how to do that? dsp = File.open("/dev/dsp", "a") dsp.ioctl(SNDCTL_DSP_CHANNELS, [2].pack("i")) dsp.ioctl(SNDCTL_DSP_SPEED, [16000].pack("i")) dsp.ioctl(SNDCTL_DSP_SETFMT, [AFMT_U8].pack("i")) dsp.ioctl(SNDCTL_DSP_SETFMT, [AFMT_S16_LE].pack("i")) # and play back all the samples samples.each { |x| dsp.write x } dsp.close end