Commit 8a202cc238431ec6f2c11e62407fa8e87e956e2b

Authored by eishchuk
0 parents

Initial commit

.main.cpp.swp 0 → 100644
No preview for this file type
COW.WAV 0 → 100644
No preview for this file type
COW_d.WAV 0 → 100644
No preview for this file type
DO.WAV 0 → 100644
No preview for this file type
Makefile 0 → 100644
  1 +++ a/Makefile
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +
  2 +CC = g++
  3 +FLAGS = -c
  4 +OBJECTS = $(SOURDES:.cpp=.o)
  5 +
  6 +TARGET = clavier
  7 +
  8 +$(TARGET) : $(OBJECTS)
  9 +
  10 +all: $(TARGET)
  11 +
  12 +clean:
  13 + rm -f core *.o $(TARGET)
  14 +
  15 +
  16 +
  17 +
fft.cpp 0 → 100644
  1 +++ a/fft.cpp
@@ -0,0 +1,104 @@ @@ -0,0 +1,104 @@
  1 +/*----------------------------------------------------------------------------
  2 + fft.c - fast Fourier transform and its inverse (both recursively)
  3 + Copyright (C) 2004, Jerome R. Breitenbach. All rights reserved.
  4 +
  5 + The author gives permission to anyone to freely copy, distribute, and use
  6 + this file, under the following conditions:
  7 + - No changes are made.
  8 + - No direct commercial advantage is obtained.
  9 + - No liability is attributed to the author for any damages incurred.
  10 + ----------------------------------------------------------------------------*/
  11 +
  12 +/******************************************************************************
  13 + * This file defines a C function fft that, by calling another function *
  14 + * fft_rec (also defined), calculates an FFT recursively. Usage: *
  15 + * fft(N, x, X); *
  16 + * Parameters: *
  17 + * N: number of points in FFT (must equal 2^n for some integer n >= 1) *
  18 + * x: pointer to N time-domain samples given in rectangular form (Re x, *
  19 + * Im x) *
  20 + * X: pointer to N frequency-domain samples calculated in rectangular form *
  21 + * (Re X, Im X) *
  22 + * Similarly, a function ifft with the same parameters is defined that *
  23 + * calculates an inverse FFT (IFFT) recursively. Usage: *
  24 + * ifft(N, x, X); *
  25 + * Here, N and X are given, and x is calculated. *
  26 + ******************************************************************************/
  27 +
  28 +#include <stdlib.h>
  29 +#include <math.h>
  30 +#include "fft.h"
  31 +/* macros */
  32 +#define TWO_PI (6.2831853071795864769252867665590057683943L)
  33 +
  34 +/* FFT */
  35 +void fft(int N, double (*x)[2], double (*X)[2])
  36 +{
  37 + /* Declare a pointer to scratch space. */
  38 + double (*XX)[2] = (double (*)[2]) malloc(2 * N * sizeof(double));
  39 +
  40 + /* Calculate FFT by a recursion. */
  41 + fft_rec(N, 0, 1, x, X, XX);
  42 +
  43 + /* Free memory. */
  44 + free(XX);
  45 +}
  46 +
  47 +/* FFT recursion */
  48 +void fft_rec(int N, int offset, int delta,
  49 + double (*x)[2], double (*X)[2], double (*XX)[2])
  50 +{
  51 + int N2 = N/2; /* half the number of points in FFT */
  52 + int k; /* generic index */
  53 + double cs, sn; /* cosine and sine */
  54 + int k00, k01, k10, k11; /* indices for butterflies */
  55 + double tmp0, tmp1; /* temporary storage */
  56 +
  57 + if(N != 2) /* Perform recursive step. */
  58 + {
  59 + /* Calculate two (N/2)-point DFT's. */
  60 + fft_rec(N2, offset, 2*delta, x, XX, X);
  61 + fft_rec(N2, offset+delta, 2*delta, x, XX, X);
  62 +
  63 + /* Combine the two (N/2)-point DFT's into one N-point DFT. */
  64 + for(k=0; k<N2; k++)
  65 + {
  66 + k00 = offset + k*delta; k01 = k00 + N2*delta;
  67 + k10 = offset + 2*k*delta; k11 = k10 + delta;
  68 + cs = cos(TWO_PI*k/(double)N); sn = sin(TWO_PI*k/(double)N);
  69 + tmp0 = cs * XX[k11][0] + sn * XX[k11][1];
  70 + tmp1 = cs * XX[k11][1] - sn * XX[k11][0];
  71 + X[k01][0] = XX[k10][0] - tmp0;
  72 + X[k01][1] = XX[k10][1] - tmp1;
  73 + X[k00][0] = XX[k10][0] + tmp0;
  74 + X[k00][1] = XX[k10][1] + tmp1;
  75 + }
  76 + }
  77 + else /* Perform 2-point DFT. */
  78 + {
  79 + k00 = offset; k01 = k00 + delta;
  80 + X[k01][0] = x[k00][0] - x[k01][0];
  81 + X[k01][1] = x[k00][1] - x[k01][1];
  82 + X[k00][0] = x[k00][0] + x[k01][0];
  83 + X[k00][1] = x[k00][1] + x[k01][1];
  84 + }
  85 +}
  86 +
  87 +/* IFFT */
  88 +void ifft(int N, double (*x)[2], double (*X)[2])
  89 +{
  90 + int N2 = N/2; /* half the number of points in IFFT */
  91 + int i; /* generic index */
  92 + double tmp0, tmp1; /* temporary storage */
  93 +
  94 + /* Calculate IFFT via reciprocity property of DFT. */
  95 + fft(N, X, x);
  96 + x[0][0] = x[0][0]/N; x[0][1] = x[0][1]/N;
  97 + x[N2][0] = x[N2][0]/N; x[N2][1] = x[N2][1]/N;
  98 + for(i=1; i<N2; i++)
  99 + {
  100 + tmp0 = x[i][0]/N; tmp1 = x[i][1]/N;
  101 + x[i][0] = x[N-i][0]/N; x[i][1] = x[N-i][1]/N;
  102 + x[N-i][0] = tmp0; x[N-i][1] = tmp1;
  103 + }
  104 +}
fft.h 0 → 100644
  1 +++ a/fft.h
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +#ifndef FFT_H
  2 +#define FFT_H
  3 +
  4 +/* function prototypes */
  5 +void fft(int N, double (*x)[2], double (*X)[2]);
  6 +void fft_rec(int N, int offset, int delta,
  7 + double (*x)[2], double (*X)[2], double (*XX)[2]);
  8 +void ifft(int N, double (*x)[2], double (*X)[2]);
  9 +
  10 +#endif // FFT_H
fft.o 0 → 100644
No preview for this file type
main.cpp 0 → 100644
  1 +++ a/main.cpp
@@ -0,0 +1,70 @@ @@ -0,0 +1,70 @@
  1 +#include <iostream>
  2 +#include "wavdata.h"
  3 +#include "fft.h"
  4 +#include <math.h>
  5 +
  6 +#define DELAY 5000
  7 +#define TOUCH 15000
  8 +#define AMPLITUDE 0.5
  9 +
  10 +int main(int argc, char **argv)
  11 +{
  12 + /*WavData w;
  13 + char * name = "COW.WAV";
  14 + w.load(name);
  15 +
  16 + char *data = w.data();
  17 + char *data2 = new char[w.datasize()*2];
  18 +
  19 + int i;
  20 + for(i=0;i<w.datasize();i++)
  21 + data2[i]=data[i];
  22 + for(;i<w.datasize()*2;i++)
  23 + data2[i]=128;
  24 +
  25 + for(int i=DELAY;i<2*w.datasize();i++)
  26 + {
  27 + float value = (float)(unsigned char)data2[i-DELAY]-128.0;
  28 + value = value * AMPLITUDE;
  29 + int val = (unsigned int)value + (unsigned char)data2[i];
  30 + if(val>255)val=255;
  31 + if(val<0)val=0;
  32 +
  33 + data2[i]= (unsigned char)(unsigned int)val;
  34 + }
  35 +
  36 + w.clearData();
  37 + w.setDatasize(w.datasize()*2);
  38 + w.setData(data2);
  39 +
  40 + char * name_save = "COW_d.WAV";
  41 + w.save(name_save);*/
  42 +
  43 +
  44 + // recréer une note ) partir du son de la vache
  45 +
  46 + WavData note_do;
  47 + note_do.load("COW.WAV");
  48 + char * data_do = note_do.data();
  49 +
  50 + // TOUCH = durée d'une note
  51 + int size = note_do.datasize();
  52 + if (size > TOUCH) size = TOUCH;
  53 +
  54 + // changer le son pour avoir une note approprié
  55 +
  56 + int i;
  57 + for (i = 0; i < size ; i++) {
  58 + data_do[i] = i;
  59 + }
  60 +
  61 + // assign new data & save
  62 +
  63 + note_do.clearData();
  64 + note_do.setDatasize(size);
  65 + note_do.setData(data_do);
  66 +
  67 + note_do.save("DO.WAV");
  68 +
  69 +
  70 +}
main.o 0 → 100644
No preview for this file type
task.txt 0 → 100644
  1 +++ a/task.txt
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +Realiser un clavier de piano sur les touches pour avoir le son de vach pour chaque note musicale
test 0 → 100755
No preview for this file type
testWAV.pro 0 → 100644
  1 +++ a/testWAV.pro
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +######################################################################
  2 +# Automatically generated by qmake (2.01a) lun. mai 9 11:16:11 2011
  3 +######################################################################
  4 +
  5 +TEMPLATE = app
  6 +TARGET =
  7 +DEPENDPATH += .
  8 +INCLUDEPATH += .
  9 +
  10 +# Input
  11 +HEADERS += fft.h wavdata.h
  12 +SOURCES += fft.cpp main.cpp wavdata.cpp
wavdata.cpp 0 → 100644
  1 +++ a/wavdata.cpp
@@ -0,0 +1,269 @@ @@ -0,0 +1,269 @@
  1 +#include "wavdata.h"
  2 +#include <iostream>
  3 +
  4 +
  5 +using namespace std;
  6 +
  7 +WavData::WavData()
  8 +{
  9 + _data=NULL;
  10 +}
  11 +
  12 +int WavData::error(int i,int line)
  13 +{
  14 + std::cerr << "Erreur "<<__FILE__<<" L"<<line<< ": ";
  15 + switch(i)
  16 + {
  17 + case -1: std::cerr << "ouverture du fichier";
  18 + break;
  19 + case -2: std::cerr << "le fichier n'est pas au format WAVE";
  20 + break;
  21 + default: std::cerr << "inconnue";
  22 + break;
  23 + }
  24 +
  25 + std::cerr << "\n";
  26 + return i;
  27 +}
  28 +
  29 +unsigned int WavData::fromLittleEndian(char * content, int size)
  30 +{
  31 + unsigned int result = 0;
  32 + for(int i=size;i>=0;i--)
  33 + {
  34 + result *= 256;
  35 + result += (unsigned char)content[i];
  36 + }
  37 + return result;
  38 +}
  39 +
  40 +char * WavData::toLittleEndian(unsigned int content, int size)
  41 +{
  42 + char * result= new char [size*sizeof(char)];
  43 +
  44 + for(int i=0;i<size;i++)
  45 + {
  46 + result += (char)(content%256);
  47 + content >>= 8;
  48 + }
  49 +
  50 + return result;
  51 +}
  52 +
  53 +char * WavData::read(ifstream* file,unsigned int size)
  54 +{
  55 + char * c = new char[size+1];
  56 + for(int i=0;i<size;i++)
  57 + {
  58 + c[i] = file->get();
  59 + }
  60 + c[size]='\0';
  61 + return c;
  62 +}
  63 +
  64 +bool WavData::comp(char* c1, char *c2, int size)
  65 +{
  66 + for(int i=0;i<size;i++)
  67 + {
  68 + if(c1[i]!=c2[i])return false;
  69 + }
  70 + return true;
  71 +}
  72 +
  73 +int WavData::testString(ifstream* file,char*compstr,int size,char *mess)
  74 +{
  75 + char * content = read(file,size);
  76 + std::cout << "| "<< mess << std::endl<<"| ->"<< content << std::endl << "|"<<std::endl;
  77 + if(!comp(content,compstr,size))return error(-2,__LINE__);
  78 +
  79 + delete[] content;
  80 + return 1;
  81 +}
  82 +
  83 +int WavData::testInt(ifstream* file,int size,char *mess,unsigned int *val)
  84 +{
  85 + int test;
  86 + char * content = read(file,size);
  87 +
  88 + int value=fromLittleEndian(content, size);
  89 + std::cout << "| "<< mess << std::endl<<"| ->"<<value << std::endl<<"|" <<std::endl;
  90 + if(val!=NULL)*val=value;
  91 + delete[] content;
  92 + return 1;
  93 +}
  94 +
  95 +int WavData::openFormatBloc(ifstream* file)
  96 +{
  97 + std::cout << "[Bloc de declaration d'un fichier au format WAVE]"<<std::endl<<"|"<<std::endl;
  98 +
  99 + // FileTypeBlocId
  100 + int r;
  101 + r = testString(file,"RIFF",4, "FileTypeBlocID (4 octets) : Constante 'RIFF'");
  102 + if(r<=0)return r;
  103 +
  104 + // FileSize
  105 + r = testInt(file,4, "FileSize (4 octets) : Taille du fichier moins 8 octets");
  106 + if(r<=0)return r;
  107 +
  108 + // FileFormatID
  109 + r = testString(file,"WAVE",4, "FileFormatID (4 octets) : Format = 'WAVE'");
  110 + if(r<=0)return r;
  111 +
  112 + std::cout << std::endl;
  113 + return 1;
  114 +}
  115 +
  116 +int WavData::openDescriptionBloc(ifstream* file)
  117 +{
  118 + std::cout << "[Bloc decrivant le format audio]"<<std::endl<<"|"<<std::endl;
  119 +
  120 + int r;
  121 +
  122 + // FormatBlocID
  123 + r = testString(file,"fmt ",4, "FormatBlocID (4 octets) : Identifiant 'fmt'");
  124 + if(r<=0)return r;
  125 +
  126 + // BlocSize
  127 + r = testInt(file,4,"BlocSize (4 octets) : Nombre d'octets du bloc - 8 (attendue: 16)");
  128 + if(r<=0)return r;
  129 +
  130 + // AudioFormat
  131 + r = testInt(file,2,"AudioFormat (2 octets) : Format du stockage dans le fichier (1: PCM, ...)",&_audioFormat);
  132 + if(r<=0)return r;
  133 +
  134 + // NbrCanaux
  135 + r = testInt(file,2,"NbrCanaux (2 octets) : Nombre de canaux (de 1 a 6)",&_nbrChanel);
  136 + if(r<=0)return r;
  137 +
  138 + // Frequency
  139 + r = testInt(file,4,"Frequence (4 octets) : Frequence d'echantillonnage (en Hertz)",&_frequency);
  140 + if(r<=0)return r;
  141 +
  142 + // BytePerSec
  143 + r = testInt(file,4,"BytePerSec (4 octets) : Nombre d'octets a lire par seconde",&_bytePerSec);
  144 + if(r<=0)return r;
  145 +
  146 + // BytePerBloc
  147 + r = testInt(file,2,"BytePerBloc (2 octets) : Nombre d'octets par bloc d'echantillonnage",&_bytePerBloc);
  148 + if(r<=0)return r;
  149 +
  150 + // BitsPerSample
  151 + r = testInt(file,2,"BitsPerSample (2 octets) : Nombre de bits pour le codage d'un echantillon",&_bitsPerSample);
  152 + if(r<=0)return r;
  153 +
  154 + return 1;
  155 +}
  156 +
  157 +int WavData::openDataBloc(ifstream* file)
  158 +{
  159 + std::cout << "[Bloc des donnees]\n";
  160 +
  161 + int r,test;
  162 + // DataBlocID
  163 + r = testString(file,"data",4,"DataBlocID (4 octets) : Constante 'data'");
  164 + if(r<=0)return r;
  165 +
  166 + // DataSize
  167 + r = testInt(file,3,"DataSize (4 octets) : Nombre d'octets des donnees",&_datasize);
  168 + if(r<=0)return r;
  169 +
  170 + // Data[];
  171 + std::cout << "| Data[] : [Octets du Sample 1 du Canal 1] [Octets du Sample 1 du Canal 2] [Octets du Sample 2 du Canal 1] [Octets du Sample 2 du Canal 2]\n\n";
  172 + if(_data!=NULL)delete[] _data;
  173 + _data = read(file,_datasize);
  174 +
  175 + return 1;
  176 +}
  177 +
  178 +int WavData::load(char * s)
  179 +{
  180 + std::cout << "Load file: \"" << s << "\""<<std::endl<<std::endl;
  181 +
  182 + ifstream file(s, ios::in | ios::binary );
  183 + if (!file)return error(-1,__LINE__);
  184 +
  185 + if(openFormatBloc(&file) != 1) {file.close(); return 0;}
  186 + if(openDescriptionBloc(&file) != 1) {file.close(); return 0;}
  187 + if(openDataBloc(&file) != 1) {file.close(); return 0;}
  188 +
  189 + std::cout << "close file"<< std::endl;
  190 + file.close();
  191 + std::cout << "file closed"<<std::endl;
  192 +
  193 + return 1;
  194 +}
  195 +
  196 +void WavData::toLittleEndian(char* result,unsigned int content, int size)
  197 +{
  198 + for(int i=0;i<size;i++)
  199 + {
  200 + result[i] = (char)(content%256);
  201 + content >>= 8;
  202 + }
  203 +
  204 + return;
  205 +}
  206 +
  207 +void WavData::write(ofstream *file,char* mess,unsigned int size)
  208 +{
  209 + for(int i=0;i<size;i++)
  210 + {
  211 + file->put(mess[i]);
  212 + }
  213 +}
  214 +
  215 +int WavData::saveFormatBloc(ofstream* file)
  216 +{
  217 + char b[13] = "RIFF WAVE";
  218 +
  219 + toLittleEndian(b+4,_datasize+36,4);
  220 +
  221 + write(file,b,12);
  222 +
  223 + return 1;
  224 +}
  225 +
  226 +int WavData::saveDescriptionBloc(ofstream* file)
  227 +{
  228 + char b[25] = "fmt ";
  229 +
  230 + toLittleEndian(b+4,0x10,4);
  231 + std::cout<<_audioFormat<<std::endl;
  232 + toLittleEndian(b+8,_audioFormat,2);
  233 + toLittleEndian(b+10,_nbrChanel,2);
  234 + toLittleEndian(b+12,_frequency,4);
  235 + toLittleEndian(b+16,_bytePerSec,4);
  236 + toLittleEndian(b+20,_bytePerBloc,2);
  237 + toLittleEndian(b+22,_bitsPerSample,2);
  238 +
  239 + write(file,b,24);
  240 +
  241 + return 1;
  242 +}
  243 +int WavData::saveDataBloc(ofstream* file)
  244 +{
  245 + char b[9] = "data ";
  246 + toLittleEndian(b+4,_datasize,4);
  247 +
  248 + write(file,b,8);
  249 +
  250 + write(file,_data,_datasize);
  251 +
  252 + return 1;
  253 +}
  254 +
  255 +int WavData::save(char * s)
  256 +{
  257 + std::cout << "Save file: \"" << s << "\"\n\n";
  258 +
  259 + ofstream file(s, ios::out | ios::trunc | ios::binary);
  260 + if (!file)return error(-1,__LINE__);
  261 +
  262 + saveFormatBloc(&file);
  263 + saveDescriptionBloc(&file);
  264 + saveDataBloc(&file);
  265 +
  266 + file.close();
  267 +
  268 + return 1;
  269 +}
wavdata.h 0 → 100644
  1 +++ a/wavdata.h
@@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
  1 +#ifndef WAVDATA_H
  2 +#define WAVDATA_H
  3 +
  4 +#include <iostream>
  5 +#include <fstream>
  6 +
  7 +class WavData
  8 +{
  9 + unsigned int _audioFormat;
  10 + unsigned int _nbrChanel;
  11 + unsigned int _frequency;
  12 + unsigned int _bytePerBloc;
  13 + unsigned int _bytePerSec;
  14 + unsigned int _bitsPerSample;
  15 +
  16 + char * _data;
  17 + unsigned int _datasize;
  18 +public:
  19 + WavData();
  20 +
  21 + int load(char* s);
  22 + int save(char* s);
  23 +
  24 + unsigned int fromLittleEndian(char * content, int size);
  25 + char * toLittleEndian(unsigned int content, int size);
  26 + void toLittleEndian(char* result,unsigned int content, int size);
  27 +
  28 + inline unsigned int audioFormat(){return _audioFormat;}
  29 + inline unsigned int nbrChanel(){return _nbrChanel;}
  30 + inline unsigned int frequency(){return _frequency;}
  31 + inline unsigned int bytePerBloc(){return _bytePerBloc;}
  32 + inline unsigned int bytePerSec(){return _bytePerSec;}
  33 + inline unsigned int bitsPerSample(){return _bitsPerSample;}
  34 + inline unsigned int datasize(){return _datasize;}
  35 + inline char * data(){return _data;}
  36 +
  37 + inline void setAudioFormat(unsigned int a){_audioFormat=a;}
  38 + inline void setNbrChanel(unsigned int a){_nbrChanel=a;}
  39 + inline void setFrequency(unsigned int a){_frequency=a;}
  40 + inline void setBytePerBloc(unsigned int a){_bytePerBloc=a;}
  41 + inline void setBytePerSec(unsigned int a){_bytePerSec=a;}
  42 + inline void setBitsPerSample(unsigned int a){_bitsPerSample=a;}
  43 + inline void setData(char * a){_data=a;}
  44 + inline void clearData(){delete[] _data;_data=NULL;}
  45 + inline void setDatasize(unsigned int i){_datasize=i;}
  46 +
  47 +private:
  48 + int error(int,int);
  49 + int openFormatBloc(std::ifstream* file);
  50 + int openDescriptionBloc(std::ifstream *file);
  51 + int openDataBloc(std::ifstream *file);
  52 +
  53 + int saveFormatBloc(std::ofstream *file);
  54 + int saveDescriptionBloc(std::ofstream *file);
  55 + int saveDataBloc(std::ofstream *file);
  56 +
  57 + void write(std::ofstream *file,char* mess,unsigned int size);
  58 + char * read(std::ifstream* file,unsigned int size);
  59 +
  60 + bool comp(char* c1, char *c2, int size);
  61 + int testString(std::ifstream* file,char*compstr,int size,char *mess);
  62 + int testInt(std::ifstream* file,int size,char *mess,unsigned int *val=NULL);
  63 +
  64 +};
  65 +
  66 +#endif // WAVDATA_H
wavdata.o 0 → 100644
No preview for this file type