thenetfreaker has asked for the wisdom of the Perl Monks concerning the following question:

Hello ALL,
I'm working on a program that's capable of processing every byte of the microphone stream and playing it immediatly afterwards.
All my algorithms are still one papers around the house, and there're only 2 functions missing - The discribed in the title problem and What function is used to mix an 'n' number of "channels' bytes" into one 'Loosy' stream as "Wav to Mp3" function ?
I thought of using IO::something to do the task, but my temporary lack of the IO module is being my main problem of constructing a simple code for the first part, so thanks in advance for that.
To complete the alg. with the "mixing" a link to the resurce will be more than enough.

Tnanks again.

  • Comment on Can perl read a byte from a microphone stream ?

Replies are listed 'Best First'.
Re: Can perl read a byte from a microphone stream ?
by zentara (Cardinal) on Feb 27, 2006 at 17:28 UTC
    What you are looking for is Audio::DSP. You must have your mixer settings setup right to receive input from the mic,
    #!/usr/bin/perl use Audio::DSP; #alsamixer must be setup right, just turn everything up :-) $|++; ($buf, $chan, $fmt, $rate) = (4096, 1, 16, 22050); #($buf, $chan, $fmt, $rate) = (4096, 1, AFMT_U16_LE, 44100); #use the formats from soundcard.h, plain 16 wont work with sblive $dsp = new Audio::DSP(buffer => $buf, channels => $chan, format => $fmt, rate => $rate); $dsp->init() || die $dsp->errstr(); # this buffers print to file so that you need ~ 10 secs of audio or t +he file is 0 # like when you hit control-c after a couple of seconds # open(OUT, "| speexenc -n --rate 22050 - test.spx "); ##this works for small low Q ogg open(OUT, "| oggenc -r -R 8000 -B 8 -C 1 -q 0 - | cat > zztest.ogg ") + or warn "No oggenc $!\n"; while ( $buffer = $dsp->dread(4096) ) { syswrite( OUT, $buffer, length($buffer) ); } $dsp->close(); # If you don't set the output of the dsp with arecord, or this # module, the dsp will go to it's lowest setting # speexenc --rate 8000 --le --8bit /dev/dsp - | cat > 1.spx # it works but it sounds lousy. # to convert to wave # sox -r 44100 -u -w -c 1 out1.raw out1.wav # arecord -f S16_LE -c1 -r 22050 -t wav -d 3600 -D hw:0,0 # | speexenc -n --rate 22.05 - - > /home/zentara/`date +%d%b%y%H +%M%S`.spx

    I'm not really a human, but I play one on earth. flash japh
      Thank you a lot for such an interesting approach, though i have only question about it - I checked on CPAN, and found out that Audio::DSP is a Perl interface to *NIX digital audio device, but i need it to work on Win32, so is there a module that's capable of doing the same task as Audio::DSP under Win32 ?
        Well there is Win32::Sound, and Win32-SoundRec ,but I don't use windows, so I have no experience with it. There are various microsoft help sites-> win32 scripts On linux, you can read directly from /dev/dsp like it's a filehandle, but it must be properly initialized to the bitrate, bytesize, frequency, etc. That is all Audio::DSP does, is the initialization. So you should be able to find out how to do it on windows.

        I'm not really a human, but I play one on earth. flash japh
Re: Can perl read a byte from a microphone stream ?
by blue_cowdawg (Monsignor) on Feb 27, 2006 at 15:24 UTC
        I'm working on a program that's capable of processing every byte of the microphone stream and playing it immediatly afterwards.

    I'm not sure I 100% understand what your after here, but I'll just make this comment: If you can do what you are attempting from a "C" program then you certainly do it from Perl. If nothing else you can always write an XS module to do the work. Another thought that ripples through my mind is "why Perl?" What exacly are you trying to accomplish here?

    Quickly searching CPAN I find PDL::Audio which from my very cursory examination shows some functionality for outputting audio, but I didn't notice anything about reading audio from a microphone.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
    A reply falls below the community's threshold of quality. You may see it by logging in.