Mercurial > hg > audiostuff
comparison intercom/sinuston.cpp @ 2:13be24d74cd2
import intercom-0.4.1
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Fri, 25 Jun 2010 09:57:52 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:9cadc470e3da | 2:13be24d74cd2 |
|---|---|
| 1 /* sinuston.cpp | |
| 2 * erzeuget Sinustabelle für Frequenz | |
| 3 * Author: Christian Dorge, Andre Adrian | |
| 4 * | |
| 5 * Compile: | |
| 6 c++ -DMODULTEST -g -O0 -o sinuston sinuston.cpp | |
| 7 * | |
| 8 * debug: | |
| 9 ddd sinuston & | |
| 10 * | |
| 11 * Quelltext Formatierung: | |
| 12 indent -kr -i2 -nlp -ci2 -l72 -lc72 -nut sinuston.cpp | |
| 13 * | |
| 14 */ | |
| 15 | |
| 16 #include <stdio.h> | |
| 17 #include <math.h> | |
| 18 #include <string.h> | |
| 19 | |
| 20 /* fuer syslog */ | |
| 21 #include <syslog.h> | |
| 22 | |
| 23 #include "sinuston.h" | |
| 24 | |
| 25 float dB2faktor(float dB) | |
| 26 { | |
| 27 /* Dezibel nach Faktor */ | |
| 28 return powf(10.0f, dB / 20.0f); | |
| 29 } | |
| 30 | |
| 31 float faktor2dB(float q) | |
| 32 { | |
| 33 /* Faktor nach Dezibel */ | |
| 34 return 20.0f * log10f(q); | |
| 35 } | |
| 36 | |
| 37 int kgv(int a, int b) | |
| 38 { | |
| 39 | |
| 40 int r = a; | |
| 41 int q = b; | |
| 42 int rest; | |
| 43 | |
| 44 while ((rest = r % q)) { | |
| 45 r = q; | |
| 46 q = rest; | |
| 47 } | |
| 48 int kgv = a * b / q; | |
| 49 // fprintf(stderr, "%d \n", kgv); | |
| 50 | |
| 51 return kgv; | |
| 52 } | |
| 53 | |
| 54 SINUSTON::SINUSTON() | |
| 55 { | |
| 56 // init auf 0 | |
| 57 j = 0; | |
| 58 cnt = 0; | |
| 59 memset(sinustab, 0, sizeof(sinustab)); | |
| 60 } | |
| 61 | |
| 62 short SINUSTON::frequenz(int f1, int fs, float pegel) | |
| 63 { | |
| 64 if (f1 < 0) { | |
| 65 syslog(LOG_WARNING, "Tonfrequenz < 0 \n"); | |
| 66 return -1; | |
| 67 } | |
| 68 | |
| 69 if (fs < 0) { | |
| 70 syslog(LOG_WARNING, "Abtastfrequenz < 0 \n"); | |
| 71 return -1; | |
| 72 } | |
| 73 | |
| 74 if (pegel > 0.0f) { | |
| 75 syslog(LOG_WARNING, "Pegel > 0 \n"); | |
| 76 return -1; | |
| 77 } | |
| 78 | |
| 79 if (2 * f1 > fs) { | |
| 80 syslog(LOG_WARNING, "Tonfrequenz > 1/2 Abtastfrequenz"); | |
| 81 return -1; | |
| 82 } | |
| 83 | |
| 84 cnt = kgv(fs, f1) / f1; | |
| 85 | |
| 86 if (cnt > SINUSTONLEN) // Prüfung, ob Samplewert nicht Größer als Max.wert der Sinustabelle | |
| 87 { | |
| 88 cnt = SINUSTONLEN; | |
| 89 } | |
| 90 | |
| 91 float winkelschritt = (float)f1 / (float)fs * 2 * M_PI; // Berechnung Winkelschritt | |
| 92 | |
| 93 float pegelwert = 32767 * dB2faktor(pegel); //Umrechnung Dezibel in Faktor (16bit PCM) | |
| 94 | |
| 95 for (int i = 0; i < cnt; i++) { | |
| 96 | |
| 97 sinustab[i] = (short) (pegelwert * sinf(i * winkelschritt)); | |
| 98 | |
| 99 // printf("%f, %f, %f \n", f1, fs, sinustab[i]); | |
| 100 | |
| 101 } | |
| 102 | |
| 103 return cnt; | |
| 104 } | |
| 105 | |
| 106 short SINUSTON::mischen(short ein_sprache) | |
| 107 { | |
| 108 // Schleifenende | |
| 109 if (j >= cnt) { | |
| 110 j = 0; | |
| 111 } | |
| 112 | |
| 113 long sum = (long) ein_sprache + (long) sinustab[j]; | |
| 114 | |
| 115 // Werte limitieren auf 16bit Wertebereich | |
| 116 if (sum > 32767) { | |
| 117 sum = 32767; | |
| 118 } else if (sum < -32767) { | |
| 119 sum = -32767; | |
| 120 } | |
| 121 // Schleifeninkrement | |
| 122 j++; | |
| 123 | |
| 124 return (short) sum; | |
| 125 } | |
| 126 | |
| 127 #ifdef MODULTEST | |
| 128 | |
| 129 int main(int argc, char *argv[]) | |
| 130 { | |
| 131 int f1 = 2040; // Frequenz Ton | |
| 132 int fs = 8000; // Abtastfrequenz | |
| 133 float pegel = -24; // minus Dezibel | |
| 134 short ein_sprache[80000]; | |
| 135 short aus_sprache_signal[80000]; | |
| 136 SINUSTON s; | |
| 137 | |
| 138 /* init syslog (Fehler Logging) | |
| 139 * syslog Fehlermeldungen auch nach /var/log/warn */ | |
| 140 openlog(NULL, LOG_PERROR, LOG_WARNING); | |
| 141 | |
| 142 /* | |
| 143 printf("Bitte Frequenz eingeben: "); | |
| 144 scanf("%f", &f1); | |
| 145 printf("Bitte Samplerate eingeben: "); | |
| 146 scanf("%f", &fs); | |
| 147 printf("Bitte Pegel eingeben: "); | |
| 148 scanf("%f", &pegel); | |
| 149 */ | |
| 150 | |
| 151 // Eingabe | |
| 152 fread(ein_sprache, sizeof(short), 80000, stdin); | |
| 153 | |
| 154 // Verarbeitung | |
| 155 short cnt = s.frequenz(f1, fs, pegel); | |
| 156 // fprintf(stderr, "Die Anzahl der Elemente ist %f \n", cnt); | |
| 157 | |
| 158 int j; | |
| 159 for (int i = 0; i < 80000; i++) { | |
| 160 aus_sprache_signal[i] = s.mischen(ein_sprache[i]); | |
| 161 } | |
| 162 | |
| 163 // Ausgabe | |
| 164 /* (short) wg. Ausgabe in RAW-Datei | |
| 165 * (1 Sample = 16bit Signed Int Wert) | |
| 166 * fwrite(pointer auf Daten, Datengröße des Typs, Anzahl zu lesender Elemente, Ausgabe) | |
| 167 */ | |
| 168 fwrite(aus_sprache_signal, sizeof(short), 80000, stdout); | |
| 169 | |
| 170 } | |
| 171 | |
| 172 #endif |
