Gentoo Archives: gentoo-user

From: Willie Wong <wwong@×××××××××.edu>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] tone generator
Date: Thu, 24 Jan 2008 20:21:47
Message-Id: 20080124202109.GA10245@math.princeton.edu
In Reply to: [gentoo-user] tone generator by maxim wexler
1 On Thu, Jan 24, 2008 at 11:41:04AM -0800, maxim wexler wrote:
2 > Anybody know of a gentoo/linux tone generator that
3 > will output test tones, sine waves, triangle waves and
4 > the like.
5 >
6 > Prefer command line/ncurses.
7 >
8
9 One of my friends coded something like this once as a plug-in for
10 XMMS. Can't seem to find it now. Perhaps you'd have better luck than
11 I.
12
13 On the other hand, a little bit of C++ should be fairly easy. First
14 you need the 'play' program from sox. 'play' can play files in the
15 'raw' format, which is just a stream of words that gives the amplitude
16 of the waveform. For example
17
18 play -t raw -s l -f s -c 1 -r 30000 -
19
20 takes as input stdin, plays mono channel sound from a stream that is
21 signed-linear, 300000 hertz sample rate, and with amplitude described
22 by 32 bit long word. Next you just need a waveform generator. A C++
23 snipplet from something I cobbled together several years ago (yes yes,
24 my coding practice can stand much improvement, so sue me)
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <math.h>
29
30 int main()
31 {
32 int rate = 30000;
33 int coramp = 0xCEFFFFF;
34
35 int tempamp=0;
36
37 float totalamp=0;
38
39 int f_count;
40 while(1) for(f_count=0; f_count < rate; f_count++)
41 {
42 totalamp=sinf( ((float)f_count) * 440 / rate * 2 * M_PI);
43 tempamp = (int) (coramp * totalamp);
44 printf("%c%c%c%c%c%c%c%c", (char) (tempamp), (char) (tempamp >> 8), (char) (tempamp >> 16), (char) (tempamp >> 24), (char) (tempamp), (char) (tempamp >> 8), (char) (tempamp >> 16), (char) (tempamp >> 24));
45 }
46 return 0;
47 }
48
49 Yes, it is an infinite loop.
50 The sinf makes it a sine wave. You can code your own triangle wave.
51 THe 440 makes it output A-440. It is the frequency.
52 coramp is an amplifying factor.
53
54 compile it, run it like
55
56 ./a.out | play -t raw -s l -f s -c 1 -r 30000 -
57
58 And you should get A-440 out of your speakers.
59
60 W
61 --
62 Willie W. Wong wwong@××××××××××××××.edu
63 408 Fine Hall, Department of Mathematics, Princeton University, Princeton
64 A mathematician's reputation rests on the number of bad proofs he has given.
65 --
66 gentoo-user@l.g.o mailing list

Replies

Subject Author
Re: [gentoo-user] tone generator maxim wexler <blissfix@×××××.com>
Re: [gentoo-user] tone generator maxim wexler <blissfix@×××××.com>
Re: [gentoo-user] tone generator maxim wexler <blissfix@×××××.com>