On Tue, 2012-02-07 at 13:59 +0530, Arun Raghavan wrote: > Nice. Do you have a test program for this? Makes it a lot easier to > verify that things are working when more changes happen in the future > and to catch leaks. We could drop it in vala/examples or some such for > reference.
I have attached two small Vala programs which will test/demonstrate
audio playback (SimplePiano) and recording (TapDetector) using
libpulse-simple. Compile with "valac --pkg=libpulse-simple $FILE".
SimplePiano: Plays a short song when started. You can supply your own
song as the first argument.
TapDetector: Detects when you tap/knock on your microphone.
Is this what you wanted?
Best regards
Alexander Kurtz
/*** This file is part of PulseAudio. Copyright 2012 Alexander Kurtz <[email protected]> PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. PulseAudio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PulseAudio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. ***/ using PulseAudio; class SimplePiano : Object { SampleSpec spec = SampleSpec(){ format = SampleFormat.S16NE, rate = 44100, channels = 1 }; Simple simple; public SimplePiano(){ this.simple = new Simple(null, null, Stream.Direction.PLAYBACK, null, "stream", spec, null); } public void play_song(string song) throws ConvertError { foreach(string note in song.split(" ")) play_sine(get_frequency_from_number(get_number_from_note(note)), 0.2); simple.drain(); } int get_number_from_note(string note) throws ConvertError { if(/^[A-G][#b]?[0-9]/.match(note) == false) throw new ConvertError.ILLEGAL_SEQUENCE("Invalid note " + note); int number = 0; switch(note[0]){ case 'C': number = -8; break; case 'D': number = -6; break; case 'E': number = -4; break; case 'F': number = -3; break; case 'G': number = -1; break; case 'A': number = 1; break; case 'B': number = 3; break; } if(note[1] == '#') number += (note[2] - '0') * 12 + 1; else if(note[1] == 'b') number += (note[2] - '0') * 12 - 1; else number += (note[1] - '0') * 12; return number; } /* See http://en.wikipedia.org/wiki/Piano_key_frequencies */ double get_frequency_from_number(int number){ return 440.0 * Math.pow(2.0, (number - 49.0) / 12.0); } void play_sine(double frequency, double time){ int16[] buffer = new int16[(int) (time * spec.rate)]; for(int i = 0; i < buffer.length; i++) buffer[i] = (int16) (int16.MAX * Math.sin((double) i / (double) spec.rate * frequency * 2 * Math.PI)); simple.write(buffer, buffer.length * sizeof(int16)); } public static int main(string[] arguments){ string song = arguments.length > 1 ? arguments[1] : "E5 D#5 E5 D#5 E5 B4 D5 C5 A4 A4"; SimplePiano piano = new SimplePiano(); try { piano.play_song(song); return 0; } catch (ConvertError e){ stderr.printf("%s: %s\n", arguments[0], e.message); return 1; } } }
/*** This file is part of PulseAudio. Copyright 2012 Alexander Kurtz <[email protected]> PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. PulseAudio is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PulseAudio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. ***/ using PulseAudio; class TapDetector : Object { SampleSpec spec = SampleSpec(){ format = SampleFormat.S16NE, rate = 44100, channels = 1 }; Simple simple; public TapDetector(){ simple = new Simple(null, null, Stream.Direction.RECORD, null, "stream", spec, null); } public void detect(){ while(true){ int16[] data = new int16[10000]; simple.read(data, data.length * sizeof(int16)); for(int i = 0; i < data.length; i++){ if(data[i] > int16.MAX / 2){ stdout.printf("TAP\n"); break; } } } } public static int main(string[] arguments){ TapDetector detector = new TapDetector(); detector.detect(); return 0; } }
signature.asc
Description: This is a digitally signed message part
_______________________________________________ pulseaudio-discuss mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/pulseaudio-discuss
