[154] | 1 | //roarsin.c: |
---|
| 2 | |
---|
[669] | 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
[669] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[669] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[1103] | 26 | #include <roaraudio.h> /* libroar */ |
---|
| 27 | |
---|
| 28 | #ifdef ROAR_HAVE_LIBM |
---|
| 29 | |
---|
[154] | 30 | #include <math.h> /* sin() */ |
---|
| 31 | #include <stdio.h> /* *printf*() */ |
---|
| 32 | |
---|
[887] | 33 | double rect (double x) { |
---|
| 34 | x /= 2*M_PI; |
---|
| 35 | x -= (int)x; |
---|
| 36 | |
---|
| 37 | if ( x < 0.5 ) |
---|
| 38 | return 1; |
---|
| 39 | else |
---|
| 40 | return -1; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | double saw (double x) { |
---|
| 44 | x /= 2*M_PI; |
---|
| 45 | x -= (int)x; |
---|
| 46 | |
---|
| 47 | return 2*x - 1; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | double tri (double x) { |
---|
| 51 | x /= 2*M_PI; |
---|
| 52 | x -= (int)x; |
---|
| 53 | |
---|
| 54 | if ( x < 0.5 ) |
---|
| 55 | return 4* x - 1; |
---|
| 56 | else |
---|
| 57 | return -4*(x-0.5) + 1; |
---|
| 58 | } |
---|
| 59 | |
---|
[888] | 60 | double trap (double x) { |
---|
| 61 | x /= 2*M_PI; |
---|
| 62 | x -= (int)x; |
---|
| 63 | |
---|
| 64 | if ( x < 0.125 || x > 0.875 ) { |
---|
| 65 | return -1; |
---|
| 66 | } else if ( x < 0.625 && x > 0.375 ) { |
---|
| 67 | return 1; |
---|
| 68 | } else if ( x < 0.5 ) { |
---|
| 69 | return 8*(x-0.375) + 1; |
---|
| 70 | } else { |
---|
| 71 | return -8*(x-0.625) + 1; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
[887] | 75 | int main (int argc, char * argv[]) { |
---|
[154] | 76 | int rate = ROAR_RATE_DEFAULT; |
---|
[887] | 77 | int bits = 16; |
---|
[154] | 78 | int channels = 1; /* mono */ |
---|
| 79 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 80 | float freq = 523.2; /* middle C */ |
---|
| 81 | float t = 0; /* current time */ |
---|
| 82 | float length = 5; /* 5 sec */ |
---|
[887] | 83 | float step; /* how much time per sample we have to encode ... */ |
---|
[154] | 84 | int fh; |
---|
| 85 | int i; |
---|
[887] | 86 | int16_t out[1024]; |
---|
| 87 | double (*func)(double x) = sin; |
---|
| 88 | |
---|
| 89 | for (i = 1; i < argc; i++) { |
---|
| 90 | if ( !strcmp(argv[i], "--freq") ) { |
---|
| 91 | freq = atof(argv[++i]); |
---|
| 92 | } else if ( !strcmp(argv[i], "--time") ) { |
---|
| 93 | length = atof(argv[++i]); |
---|
| 94 | } else if ( !strcmp(argv[i], "--sin") ) { |
---|
| 95 | func = sin; |
---|
[888] | 96 | } else if ( !strcmp(argv[i], "--rect") ) { |
---|
| 97 | func = rect; |
---|
[887] | 98 | } else if ( !strcmp(argv[i], "--saw") ) { |
---|
| 99 | func = saw; |
---|
| 100 | } else if ( !strcmp(argv[i], "--tri") ) { |
---|
| 101 | func = tri; |
---|
[888] | 102 | } else if ( !strcmp(argv[i], "--trap") ) { |
---|
| 103 | func = trap; |
---|
[887] | 104 | } else { |
---|
| 105 | return 2; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | step = M_PI*2*freq/rate; |
---|
[154] | 110 | |
---|
| 111 | if ( (fh = roar_simple_play(rate, channels, bits, codec, NULL, "sine gen")) == -1 ) { |
---|
| 112 | fprintf(stderr, "Error: can not open playback!\n"); |
---|
| 113 | exit(1); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | while (t < 2*M_PI*freq*length) { |
---|
| 117 | for (i = 0; i < 1024; i++) { |
---|
[887] | 118 | out[i] = 32767*func(t); |
---|
[154] | 119 | t += step; |
---|
| 120 | } |
---|
[887] | 121 | write(fh, out, 2048); |
---|
[154] | 122 | } |
---|
| 123 | |
---|
| 124 | roar_simple_close(fh); |
---|
| 125 | |
---|
| 126 | return 0; |
---|
| 127 | } |
---|
| 128 | |
---|
[1103] | 129 | #else |
---|
| 130 | int main (void) { |
---|
| 131 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
| 132 | return 1; |
---|
| 133 | } |
---|
| 134 | #endif |
---|
| 135 | |
---|
[154] | 136 | //ll |
---|