Elkyr94 Principiante
Messaggi : 42
| Titolo: Generatore toni DTMF (quelli del tastierino telefonico) [C] 1/2/2011, 7:36 pm | |
| Sto imparando a conoscere i files audio, così per esercizio/esperimento ho creato un programmino a riga di comando in C che genera un file wave contenente dei toni DTMF. In sostanza, voi eseguite il programma, digitate un numero di telefono, e nella stessa cartella del programma vi si crea un file "dtmf.wav". Se lo riproducete avvicinando alle casse del pc la cornetta del telefono fisso, questo chiamerà automaticamente il numero che avevate digitato. Lo so che è perfettamente inutile, ma era solo un esercizio. I files sono due: wave.c si occupa della scrittura del file wave, contenente l'audio. - Codice:
-
/******************************************************************* * MICROSOFT WAVE (PCM 32 BITS) GENERATOR * * * * Created by Luca Robbiano, 1st Feb 2011 * * * * Creative Commons * * Attribution-NonCommercial-ShareAlike 2.5 Italy (CC BY-NC-SA 2.5) * * * * You are free: * * - to Share: to copy, distribute and transmit the work * * - to Remix: to adapt the work * * * * Under the following conditions: * * - Attribution: You must attribute the work in the manner * * specified by the author or licensor (but not in any way * * that suggests that they endorse you or your use of * * the work). * * - Noncommercial: You may not use this work for commercial * * purposes. * * - Share Alike: If you alter, transform, or build upon this * * work, you may distribute the resulting work only under * * the same or similar license to this one. * * * * With the understanding that: * * - Waiver: Any of the above conditions can be waived if * * you get permission from the copyright holder. * * - Public Domain: Where the work or any of its elements * * is in the public domain under applicable law, that * * status is in no way affected by the license. * * - Other Rights: In no way are any of the following rights * * affected by the license: * * - Your fair dealing or fair use rights, or other * * applicable copyright exceptions and limitations; * * - The author's moral rights; * * - Rights other persons may have either in the work * * itself or in how the work is used, such as publicity * * or privacy rights. * * * * Notice: For any reuse or distribution, you must make clear to * * others the license terms of this work. * *******************************************************************/
#include <stdio.h> #include <math.h> #include <string.h>
#define BITS_PER_SAMPLE 32 #define SAMPLE_RATE 44100 #define CHANNELS 1
char* createWave(unsigned int length) { char *mat=malloc(length*SAMPLE_RATE*BITS_PER_SAMPLE/8*sizeof(char)); return mat; }
//Supports only 1 channel void setSample(char *wave,unsigned int sample_id,double value) { double v=value;
value=1+value; unsigned int offset=sample_id*BITS_PER_SAMPLE/8; unsigned int val=(pow(2,BITS_PER_SAMPLE-1)*(1+value)); putint(wave,offset,val); }
void saveWave(char *wave,short length,char *filename) { FILE *fp; fp=fopen(filename,"wb"); unsigned int bytes=CHANNELS*BITS_PER_SAMPLE/8*SAMPLE_RATE*length; unsigned char header[44]; //Fill header of 0x0 int i; for(i=0;i<44;i++) { header[i]=0; } header[0]=0x52; //R header[1]=0x49; //I header[2]=0x46; //F header[3]=0x46; //F putint(header,5,bytes+36); //Size header[8]=0x57; //W header[9]=0x41; //A header[10]=0x56; //V header[11]=0x45; //E header[12]=0x66; //f header[13]=0x6d; //m header[14]=0x74; //t header[15]=0x20; // header[16]=0x10; // 16 header[20]=0x01; // Audio format = PCM header[22]=CHANNELS; // 1 Channel putint(header,24,SAMPLE_RATE); //Sample rate putint(header,28,SAMPLE_RATE*BITS_PER_SAMPLE/8); //Byte rate //Block Aling short block_aling=CHANNELS*BITS_PER_SAMPLE/8; header[32]=(block_aling<<8)>>8; header[33]=block_aling>>8; //Bits per sample header[34]=(BITS_PER_SAMPLE<<8)>>8; header[35]=BITS_PER_SAMPLE>>8; header[36]=0x64; //d header[37]=0x61; //a header[38]=0x74; //t header[39]=0x61; //a putint(header,40,bytes); //Size fwrite(header,1,44,fp); //START AUDIO PCM DATA for(i=0;i<bytes;i++) { fputc(wave[i],fp); } fclose(fp); //Free memory free(wave); }
int putint(unsigned char header[54],int offset,int value) { char i; for(i=0;i<4;i++) { header[offset+i]=(value<<(24-i*8))>>24; } }
dtmf.c è il programma vero e proprio, che genera i toni e per mezzo di wave.c li salva in un file .wav - Codice:
-
/******************************************************************* * DTMF TONES GENERATOR * * * * Created by Luca Robbiano, 1st Feb 2011 * * * * Creative Commons * * Attribution-NonCommercial-ShareAlike 2.5 Italy (CC BY-NC-SA 2.5) * * * * You are free: * * - to Share: to copy, distribute and transmit the work * * - to Remix: to adapt the work * * * * Under the following conditions: * * - Attribution: You must attribute the work in the manner * * specified by the author or licensor (but not in any way * * that suggests that they endorse you or your use of * * the work). * * - Noncommercial: You may not use this work for commercial * * purposes. * * - Share Alike: If you alter, transform, or build upon this * * work, you may distribute the resulting work only under * * the same or similar license to this one. * * * * With the understanding that: * * - Waiver: Any of the above conditions can be waived if * * you get permission from the copyright holder. * * - Public Domain: Where the work or any of its elements * * is in the public domain under applicable law, that * * status is in no way affected by the license. * * - Other Rights: In no way are any of the following rights * * affected by the license: * * - Your fair dealing or fair use rights, or other * * applicable copyright exceptions and limitations; * * - The author's moral rights; * * - Rights other persons may have either in the work * * itself or in how the work is used, such as publicity * * or privacy rights. * * * * Notice: For any reuse or distribution, you must make clear to * * others the license terms of this work. * *******************************************************************/
/* How to compile: With GCC on Unix or MinGW on Windows: gcc dtmf.c wave.c -o dtmf */
#include <stdio.h> #include <math.h>
#define BITS_PER_SAMPLE 32 #define SAMPLE_RATE 44100 #define CHANNELS 1 #define PI 3.141592653
int main(); double vol(double t,double ln);
//Tabella delle frequenze DTMF int freq_a[]={697,697,697,770,770,770,852,852,852,941,941,941}; //Colonne int freq_b[]={1209,1336,1477,1209,1336,1477,1209,1336,1477,1209,1336,1477}; //Righe
int main() { unsigned int i;
//chiedo il numero char s_num[30]; printf("Numero di telefono: "); scanf("%s",&s_num); //converto il numero char *num=malloc(strlen(s_num)*sizeof(short)); for(i=0;i<strlen(s_num);i++) { char c=s_num[i]; //controllo validità if(!((c>=0x30&&c<=0x39)||(c==0x2a||c==0x23))) { printf("Il carattere "%c" non e' valido!\n",c); return 1; } char n=c; switch(n) { case 0x30: n=10; break; case 0x23: n=11; break; case 0x2a: n=9; break; default: n-=49; break; } num[i]=n; }
//Calcolo la lunghezza del file short len=(short)(0.3*(double)strlen(s_num))+1; //Creo un file wave char *wave=createWave(len); unsigned int k=0; //Scorro tutti i numeri for(i=0;i<strlen(s_num);i++) { int j; //Inserisco il tono char n=num[i]; for(j=0;j<((short)(SAMPLE_RATE*0.2));j++) { //Calcolo il valore del campione da inserire double time=((double)j)/SAMPLE_RATE; double value=0.5*vol(time,0.2)*(sin(time*2*PI*freq_a[n])+sin(time*2*PI*freq_b[n])); //Aggiungo il campione setSample(wave,k,value); k++; } //Inserisco un decimo di secondo di silenzio for(j=0;j<((short)(SAMPLE_RATE*0.1));j++) { setSample(wave,k,0); k++; } } //Informo sulla lunghezza printf("\nLunghezza: %d secondi.\n",len-1); //-1 ? //Salvo il file wave saveWave(wave,len,"dtmf.wav"); }
//Questa funzione serve per fare in modo che il volume si alzi gradualmente all'inizio ed alla fine di ogni tono //Onde evitare fastidiosi "POC" double vol(double t,double ln) { double k=ln/100; if(t<k) { return (pow(t/k,2)); } if(t>(ln-k)) { return (1-pow((t-ln+k)/k,2)); } return 1; } I file generati sono audio mono da 1411 kbit/s (44100 campioni da 32 bit al secondo). L'ho compilato solo su Windows 7 a 64 bit, usando MinGW, ma dovrebbe funzionare su qualsiasi piattaforma dato che il programma usa solo la libreria standard del C. Più tardi comunque lo provo su un qualche sistema Linux. | |
|
HyperTesto Intermedio
Messaggi : 53 Occupazione/Hobby : La FI*A
| Titolo: Re: Generatore toni DTMF (quelli del tastierino telefonico) [C] 2/2/2011, 8:41 pm | |
| Curioso!! lo proverei se avessi ancora il telefono Puoi dirmi dove hai trovato il materiale su cui ti sei basato? | |
|
Elkyr94 Principiante
Messaggi : 42
| Titolo: Re: Generatore toni DTMF (quelli del tastierino telefonico) [C] 2/2/2011, 9:33 pm | |
| - HyperTesto ha scritto:
- Curioso!! lo proverei se avessi ancora il telefono
Puoi dirmi dove hai trovato il materiale su cui ti sei basato? Qui ci sono le informazioni sui file wav: Struttura del file: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ PCM: http://it.wikipedia.org/wiki/Pulse-Code_Modulation Qui le informazioni sui toni DTMF: http://it.wikipedia.org/wiki/DTMF (I files wav ovviamente non sono compressi, se lo fossero suppongo sarebbe tutto estremamente più complesso ) | |
|
HyperTesto Intermedio
Messaggi : 53 Occupazione/Hobby : La FI*A
| Titolo: Re: Generatore toni DTMF (quelli del tastierino telefonico) [C] 3/2/2011, 8:21 pm | |
| | |
|
Contenuto sponsorizzato
| Titolo: Re: Generatore toni DTMF (quelli del tastierino telefonico) [C] | |
| |
|