[154] | 1 | //roarsin.c: |
---|
| 2 | |
---|
[669] | 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
[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 | |
---|
[5533] | 75 | static void usage(const char * progname) { |
---|
| 76 | fprintf(stderr, "Usage: %s [OPTIONS] [FUNCTION]\n", progname); |
---|
| 77 | |
---|
| 78 | fprintf(stderr, "\nOptions:\n\n"); |
---|
| 79 | fprintf(stderr, |
---|
| 80 | " --help - Show this help\n" |
---|
| 81 | " --server SERVER - Set server address\n" |
---|
| 82 | " --rate -R RATE - Set sample rate to use\n" |
---|
| 83 | " --freq FREQ - Set frequency (in Hz)\n" |
---|
| 84 | " --time TIME - Set time (in sec)\n" |
---|
[5550] | 85 | " --volume VOL - Set volume\n" |
---|
[5533] | 86 | ); |
---|
| 87 | |
---|
| 88 | fprintf(stderr, "\nFunctions:\n\n"); |
---|
| 89 | fprintf(stderr, |
---|
| 90 | " --sin - Use Sinus\n" |
---|
| 91 | " --rect - Use Rectangle\n" |
---|
| 92 | " --saw - Use Saw\n" |
---|
| 93 | " --tri - Use Triangle\n" |
---|
| 94 | " --trap - Use Trap\n" |
---|
| 95 | ); |
---|
| 96 | } |
---|
| 97 | |
---|
[887] | 98 | int main (int argc, char * argv[]) { |
---|
[5533] | 99 | const char * server = NULL; |
---|
[154] | 100 | int rate = ROAR_RATE_DEFAULT; |
---|
| 101 | float freq = 523.2; /* middle C */ |
---|
| 102 | float t = 0; /* current time */ |
---|
[5186] | 103 | float tcalc = 0; /* current time for calculation */ |
---|
[154] | 104 | float length = 5; /* 5 sec */ |
---|
[887] | 105 | float step; /* how much time per sample we have to encode ... */ |
---|
[4885] | 106 | roar_vs_t * vss; |
---|
| 107 | int err; |
---|
[154] | 108 | int i; |
---|
[887] | 109 | int16_t out[1024]; |
---|
| 110 | double (*func)(double x) = sin; |
---|
[5550] | 111 | double volume = 1; |
---|
[887] | 112 | |
---|
| 113 | for (i = 1; i < argc; i++) { |
---|
| 114 | if ( !strcmp(argv[i], "--freq") ) { |
---|
| 115 | freq = atof(argv[++i]); |
---|
| 116 | } else if ( !strcmp(argv[i], "--time") ) { |
---|
| 117 | length = atof(argv[++i]); |
---|
[5533] | 118 | } else if ( !strcmp(argv[i], "--server") ) { |
---|
| 119 | server = argv[++i]; |
---|
| 120 | } else if ( !strcmp(argv[i], "--rate") || !strcmp(argv[i], "-R") ) { |
---|
| 121 | rate = roar_str2rate(argv[++i]); |
---|
[5550] | 122 | } else if ( !strcmp(argv[i], "--volume") ) { |
---|
| 123 | volume = atof(argv[++i]); |
---|
[887] | 124 | } else if ( !strcmp(argv[i], "--sin") ) { |
---|
| 125 | func = sin; |
---|
[888] | 126 | } else if ( !strcmp(argv[i], "--rect") ) { |
---|
| 127 | func = rect; |
---|
[887] | 128 | } else if ( !strcmp(argv[i], "--saw") ) { |
---|
| 129 | func = saw; |
---|
| 130 | } else if ( !strcmp(argv[i], "--tri") ) { |
---|
| 131 | func = tri; |
---|
[888] | 132 | } else if ( !strcmp(argv[i], "--trap") ) { |
---|
| 133 | func = trap; |
---|
[5533] | 134 | } else if ( !strcmp(argv[i], "--help") ) { |
---|
| 135 | usage(argv[0]); |
---|
| 136 | return 0; |
---|
[887] | 137 | } else { |
---|
[5533] | 138 | usage(argv[0]); |
---|
[887] | 139 | return 2; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | step = M_PI*2*freq/rate; |
---|
[154] | 144 | |
---|
[5549] | 145 | if ( (vss = roar_vs_new_playback(server, "sine gen", rate, 1, ROAR_CODEC_DEFAULT, 16, &err)) == NULL ) { |
---|
[4885] | 146 | fprintf(stderr, "Error: can not open playback: %s\n", roar_vs_strerr(err)); |
---|
[154] | 147 | exit(1); |
---|
| 148 | } |
---|
| 149 | |
---|
[5550] | 150 | if ( roar_vs_volume_mono(vss, volume, NULL) == 0 ) { |
---|
| 151 | // If setting server volume successed set local volume to one. |
---|
| 152 | volume = 1; |
---|
| 153 | } |
---|
| 154 | |
---|
[154] | 155 | while (t < 2*M_PI*freq*length) { |
---|
| 156 | for (i = 0; i < 1024; i++) { |
---|
[5550] | 157 | out[i] = 32767.*volume*func(tcalc); |
---|
[5186] | 158 | t += step; |
---|
| 159 | tcalc += step; |
---|
[154] | 160 | } |
---|
[5249] | 161 | if ( roar_vs_write(vss, out, 2048, &err) != (ssize_t)2048 ) { |
---|
| 162 | fprintf(stderr, "Error: can not write data: %s\n", roar_vs_strerr(err)); |
---|
| 163 | break; |
---|
| 164 | } |
---|
[5186] | 165 | |
---|
| 166 | // this code enables us to generate the same signal for a long periode of time |
---|
| 167 | // without loosing accuracy of the float type. |
---|
| 168 | while (tcalc > 2*M_PI) |
---|
| 169 | tcalc -= 2*M_PI; |
---|
[154] | 170 | } |
---|
| 171 | |
---|
[4885] | 172 | roar_vs_close(vss, ROAR_VS_FALSE, NULL); |
---|
[154] | 173 | |
---|
| 174 | return 0; |
---|
| 175 | } |
---|
| 176 | |
---|
[1103] | 177 | #else |
---|
| 178 | int main (void) { |
---|
| 179 | fprintf(stderr, "Error: No Math library support compiled in.\n"); |
---|
| 180 | return 1; |
---|
| 181 | } |
---|
| 182 | #endif |
---|
| 183 | |
---|
[154] | 184 | //ll |
---|