[154] | 1 | //roarsin.c: |
---|
| 2 | |
---|
[669] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of roarclients a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
[1103] | 25 | #include <roaraudio.h> /* libroar */ |
---|
| 26 | |
---|
| 27 | #ifdef ROAR_HAVE_LIBM |
---|
| 28 | |
---|
[154] | 29 | #include <math.h> /* sin() */ |
---|
| 30 | #include <stdio.h> /* *printf*() */ |
---|
| 31 | |
---|
[887] | 32 | double rect (double x) { |
---|
| 33 | x /= 2*M_PI; |
---|
| 34 | x -= (int)x; |
---|
| 35 | |
---|
| 36 | if ( x < 0.5 ) |
---|
| 37 | return 1; |
---|
| 38 | else |
---|
| 39 | return -1; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | double saw (double x) { |
---|
| 43 | x /= 2*M_PI; |
---|
| 44 | x -= (int)x; |
---|
| 45 | |
---|
| 46 | return 2*x - 1; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | double tri (double x) { |
---|
| 50 | x /= 2*M_PI; |
---|
| 51 | x -= (int)x; |
---|
| 52 | |
---|
| 53 | if ( x < 0.5 ) |
---|
| 54 | return 4* x - 1; |
---|
| 55 | else |
---|
| 56 | return -4*(x-0.5) + 1; |
---|
| 57 | } |
---|
| 58 | |
---|
[888] | 59 | double trap (double x) { |
---|
| 60 | x /= 2*M_PI; |
---|
| 61 | x -= (int)x; |
---|
| 62 | |
---|
| 63 | if ( x < 0.125 || x > 0.875 ) { |
---|
| 64 | return -1; |
---|
| 65 | } else if ( x < 0.625 && x > 0.375 ) { |
---|
| 66 | return 1; |
---|
| 67 | } else if ( x < 0.5 ) { |
---|
| 68 | return 8*(x-0.375) + 1; |
---|
| 69 | } else { |
---|
| 70 | return -8*(x-0.625) + 1; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
[887] | 74 | int main (int argc, char * argv[]) { |
---|
[154] | 75 | int rate = ROAR_RATE_DEFAULT; |
---|
[887] | 76 | int bits = 16; |
---|
[154] | 77 | int channels = 1; /* mono */ |
---|
| 78 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 79 | float freq = 523.2; /* middle C */ |
---|
| 80 | float t = 0; /* current time */ |
---|
| 81 | float length = 5; /* 5 sec */ |
---|
[887] | 82 | float step; /* how much time per sample we have to encode ... */ |
---|
[154] | 83 | int fh; |
---|
| 84 | int i; |
---|
[887] | 85 | int16_t out[1024]; |
---|
| 86 | double (*func)(double x) = sin; |
---|
| 87 | |
---|
| 88 | for (i = 1; i < argc; i++) { |
---|
| 89 | if ( !strcmp(argv[i], "--freq") ) { |
---|
| 90 | freq = atof(argv[++i]); |
---|
| 91 | } else if ( !strcmp(argv[i], "--time") ) { |
---|
| 92 | length = atof(argv[++i]); |
---|
| 93 | } else if ( !strcmp(argv[i], "--sin") ) { |
---|
| 94 | func = sin; |
---|
[888] | 95 | } else if ( !strcmp(argv[i], "--rect") ) { |
---|
| 96 | func = rect; |
---|
[887] | 97 | } else if ( !strcmp(argv[i], "--saw") ) { |
---|
| 98 | func = saw; |
---|
| 99 | } else if ( !strcmp(argv[i], "--tri") ) { |
---|
| 100 | func = tri; |
---|
[888] | 101 | } else if ( !strcmp(argv[i], "--trap") ) { |
---|
| 102 | func = trap; |
---|
[887] | 103 | } else { |
---|
| 104 | return 2; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | step = M_PI*2*freq/rate; |
---|
[154] | 109 | |
---|
| 110 | if ( (fh = roar_simple_play(rate, channels, bits, codec, NULL, "sine gen")) == -1 ) { |
---|
| 111 | fprintf(stderr, "Error: can not open playback!\n"); |
---|
| 112 | exit(1); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | while (t < 2*M_PI*freq*length) { |
---|
| 116 | for (i = 0; i < 1024; i++) { |
---|
[887] | 117 | out[i] = 32767*func(t); |
---|
[154] | 118 | t += step; |
---|
| 119 | } |
---|
[887] | 120 | write(fh, out, 2048); |
---|
[154] | 121 | } |
---|
| 122 | |
---|
| 123 | roar_simple_close(fh); |
---|
| 124 | |
---|
| 125 | return 0; |
---|
| 126 | } |
---|
| 127 | |
---|
[1103] | 128 | #else |
---|
| 129 | int main (void) { |
---|
| 130 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
| 131 | return 1; |
---|
| 132 | } |
---|
| 133 | #endif |
---|
| 134 | |
---|
[154] | 135 | //ll |
---|