Ce code sert à fabriquer un fichier wav pour etre écouté ou gravé sur CD.
On peut régler la fréquence d'énchantillonnage, les amplitudes en % ou en dB, les fréquences séparément pour la voie droite ou gauche
Source / Exemple :
#include <windows.h>
#include "stdafx.h"
#include "resource.h"
#include <stdio.h>
#include <commdlg.h>
#include <math.h>
#include <mmsystem.h>
//****************************************************************************
BOOL CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static char txt[64];
static char pathname[256];
static int status;
static int i,j;
static int FE[6];
static int duree[6];
static int frequenceG[19981], frequenceD[19981], frequence[19981];
static int gainG[121], gainD[121], gain[121];
static int canaux[2];
static int octets[2];
FILE *fichier;
static double pi=3.1415926535897932;
static short val;
static unsigned long pointeur=0;
static unsigned long longueur=0;
static unsigned char bloc[4];
static char en_tete[44]={0x52,0x49,0x46,0x46, // "RIFF"
0x00,0x00,0x00,0x00, // Taille du fichier moins 8 octets
0x57,0x41,0x56,0x45, // "WAVE"
0x66,0x6d,0x74,0x20, // "fmt "
0x10,0x00,0x00,0x00,
0x01,0x00, // 1 pour PCM
0x02,0x00, // 1 pour mono, 2 pour stereo
0x44,0xAC,0x00,0x00, // frequence d'echantillonnage AC44h = 44100
0x10,0xb1,0x02,0x00, // Nombre d'octets par seconde de musique
0x04,0x00, // Nombre d'octets par échantillon
0x10,0x00, // Nombre de bits par donnée
0x64,0x61,0x74,0x61, // Constante "data"
0x00,0x00,0x00,0x00}; // Taille du file_handle moins 116 octets
if (msg == WM_INITDIALOG)
{
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_frequence),SW_HIDE);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_gain),SW_HIDE);
SetDlgItemText(hWnd,IDC_STATIC1," ");
SendDlgItemMessage(hWnd, IDC_EDIT_CHEMIN, EM_LIMITTEXT, 200, 0);
sprintf(txt,"48000");
FE[0] = 48000;
SendDlgItemMessage(hWnd, IDC_COMBO_FE , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"44100");
FE[1] = 44100;
SendDlgItemMessage(hWnd, IDC_COMBO_FE , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"32000");
FE[2] = 32000;
SendDlgItemMessage(hWnd, IDC_COMBO_FE , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"22050");
FE[3] = 22050;
SendDlgItemMessage(hWnd, IDC_COMBO_FE , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"11025");
FE[4] = 11025;
SendDlgItemMessage(hWnd, IDC_COMBO_FE , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"8000");
FE[5] = 8000;
SendDlgItemMessage(hWnd, IDC_COMBO_FE , CB_ADDSTRING , 0 , (LPARAM)txt);
SendDlgItemMessage(hWnd, IDC_COMBO_FE , CB_SETCURSEL , 1 , 0);
sprintf(txt,"100 ms");
duree[0] = 100;
SendDlgItemMessage(hWnd, IDC_COMBO_duree , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"200 ms");
duree[1] = 200;
SendDlgItemMessage(hWnd, IDC_COMBO_duree , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"500 ms");
duree[2] = 500;
SendDlgItemMessage(hWnd, IDC_COMBO_duree , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"1000 ms");
duree[3] = 1000;
SendDlgItemMessage(hWnd, IDC_COMBO_duree , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"2000 ms");
duree[4] = 2000;
SendDlgItemMessage(hWnd, IDC_COMBO_duree , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"5000 ms");
duree[5] = 5000;
SendDlgItemMessage(hWnd, IDC_COMBO_duree , CB_ADDSTRING , 0 , (LPARAM)txt);
SendDlgItemMessage(hWnd, IDC_COMBO_duree , CB_SETCURSEL , 3 , 0);
sprintf(txt,"mono");
canaux[0] = 1;
SendDlgItemMessage(hWnd, IDC_COMBO_canaux , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"stereo");
canaux[1] = 2;
SendDlgItemMessage(hWnd, IDC_COMBO_canaux , CB_ADDSTRING , 0 , (LPARAM)txt);
SendDlgItemMessage(hWnd, IDC_COMBO_canaux , CB_SETCURSEL , 1 , 0);
sprintf(txt,"8 bit");
octets[0] = 1;
SendDlgItemMessage(hWnd, IDC_COMBO_octets , CB_ADDSTRING , 0 , (LPARAM)txt);
sprintf(txt,"16 bit");
octets[1] = 2;
SendDlgItemMessage(hWnd, IDC_COMBO_octets , CB_ADDSTRING , 0 , (LPARAM)txt);
SendDlgItemMessage(hWnd, IDC_COMBO_octets , CB_SETCURSEL , 1 , 0);
for(i=0; i<19981; i++)
{
sprintf(txt,"%d Hz",i+20);
frequenceG[i] = frequenceD[i] = frequence[i] = i;
SendDlgItemMessage(hWnd, IDC_COMBO_frequenceG , CB_ADDSTRING , 0 , (LPARAM)txt);
SendDlgItemMessage(hWnd, IDC_COMBO_frequenceD , CB_ADDSTRING , 0 , (LPARAM)txt);
SendDlgItemMessage(hWnd, IDC_COMBO_frequence , CB_ADDSTRING , 0 , (LPARAM)txt);
}
SendDlgItemMessage(hWnd, IDC_COMBO_frequenceG , CB_SETCURSEL , 420 , 0);
SendDlgItemMessage(hWnd, IDC_COMBO_frequenceD , CB_SETCURSEL , 420 , 0);
SendDlgItemMessage(hWnd, IDC_COMBO_frequence , CB_SETCURSEL , 420 , 0);
j = 0;
for(i=0; i<121; i++)
{
sprintf(txt,"%d dB",j);
gainG[i] = gainD[i] = gain[i] = j;
SendDlgItemMessage(hWnd, IDC_COMBO_gainG , CB_ADDSTRING , 0 , (LPARAM)txt);
SendDlgItemMessage(hWnd, IDC_COMBO_gainD , CB_ADDSTRING , 0 , (LPARAM)txt);
SendDlgItemMessage(hWnd, IDC_COMBO_gain , CB_ADDSTRING , 0 , (LPARAM)txt);
j--;
}
SendDlgItemMessage(hWnd, IDC_COMBO_gainG , CB_SETCURSEL , 0 , 0);
SendDlgItemMessage(hWnd, IDC_COMBO_gainD , CB_SETCURSEL , 0 , 0);
SendDlgItemMessage(hWnd, IDC_COMBO_gain , CB_SETCURSEL , 0 , 0);
return 0;
}
if (msg == WM_COMMAND)
{
if ((LOWORD(wParam)) == IDC_COMBO_canaux)
{
if (SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0) == 0) // mono
{
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_frequence),SW_SHOW);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_gain),SW_SHOW);
ShowWindow(GetDlgItem(hWnd,IDC_STATIC_G),SW_HIDE);
ShowWindow(GetDlgItem(hWnd,IDC_STATIC_D),SW_HIDE);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_frequenceG),SW_HIDE);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_frequenceD),SW_HIDE);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_gainG),SW_HIDE);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_gainD),SW_HIDE);
return 0;
}
if (SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0) == 1) // stereo
{
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_frequence),SW_HIDE);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_gain),SW_HIDE);
ShowWindow(GetDlgItem(hWnd,IDC_STATIC_G),SW_SHOW);
ShowWindow(GetDlgItem(hWnd,IDC_STATIC_D),SW_SHOW);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_frequenceG),SW_SHOW);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_frequenceD),SW_SHOW);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_gainG),SW_SHOW);
ShowWindow(GetDlgItem(hWnd,IDC_COMBO_gainD),SW_SHOW);
return 0;
}
return 0;
}
if (wParam == IDOK)
{
return 0;
}
if (wParam == IDCANCEL)
{
EndDialog(hWnd, 0);
return 0;
}
if (wParam == IDC_PLAY)
{
GetDlgItemText(hWnd, IDC_EDIT_CHEMIN,pathname,128);
if(strlen(pathname) == 0)
{
MessageBox(hWnd, "nom fichier vide !", "Makewav", MB_OK);
pathname[0]=0;
OPENFILENAME ofn;
memset( &ofn, 0, sizeof(OPENFILENAME) );
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = "Fichiers wave";
ofn.lpstrDefExt = "wav";
ofn.lpstrFile = pathname;
ofn.nMaxFile = 200;
ofn.lpstrTitle = "Sélectionnez un fichier";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_READONLY | OFN_EXTENSIONDIFFERENT | OFN_EXPLORER;
if(GetOpenFileName(&ofn))
{
SetDlgItemText(hWnd,IDC_EDIT_CHEMIN,pathname);
}
return 0;
}
PlaySound (pathname, NULL, SND_FILENAME | SND_ASYNC);
return 0;
}
if (wParam == IDC_FICHIER)
{
pathname[0]=0;
OPENFILENAME ofn;
memset( &ofn, 0, sizeof(OPENFILENAME) );
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = "Fichiers wave";
ofn.lpstrDefExt = "wav";
ofn.lpstrFile = pathname;
ofn.nMaxFile = 200;
ofn.lpstrTitle = "Sélectionnez un fichier";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_READONLY | OFN_EXTENSIONDIFFERENT | OFN_EXPLORER;
if(GetOpenFileName(&ofn))
{
SetDlgItemText(hWnd,IDC_EDIT_CHEMIN,pathname);
}
return 0;
}
if (wParam == IDC_WRITE)
{
GetDlgItemText(hWnd, IDC_EDIT_CHEMIN,pathname,128);
if(strlen(pathname) == 0)
{
MessageBox(hWnd, "nom fichier vide !", "Makewav", MB_OK);
pathname[0]=0;
OPENFILENAME ofn;
memset( &ofn, 0, sizeof(OPENFILENAME) );
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = "Fichiers wave";
ofn.lpstrDefExt = "wav";
ofn.lpstrFile = pathname;
ofn.nMaxFile = 200;
ofn.lpstrTitle = "Enregistrer sous";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_READONLY | OFN_EXTENSIONDIFFERENT | OFN_EXPLORER;
if(GetSaveFileName(&ofn))
{
SetDlgItemText(hWnd,IDC_EDIT_CHEMIN,pathname);
}
return 0;
}
fichier = fopen(pathname,"wb");
if (fichier == 0)
{
MessageBox(hWnd, "ouverture fichier impossible !", "Makewav", MB_OK);
return 0;
}
EnableWindow(GetDlgItem(hWnd,IDC_WRITE),FALSE);
SetDlgItemText(hWnd,IDC_STATIC1,"écriture...");
pointeur = 44;
fseek(fichier, pointeur, SEEK_SET);
for (i=0;i<(FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]*duree[SendDlgItemMessage(hWnd, IDC_COMBO_duree, CB_GETCURSEL , 0 , 0)]*0.001);i++)
{
fseek(fichier, pointeur, SEEK_SET);
if ((SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0) == 1) && (SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0) == 1))
// stereo 16 bit
{
val = pow(10, gainG[SendDlgItemMessage(hWnd, IDC_COMBO_gainG, CB_GETCURSEL , 0 , 0)]/20.0) * 32767 * sin(2*pi*frequenceG[SendDlgItemMessage(hWnd, IDC_COMBO_frequenceG, CB_GETCURSEL , 0 , 0)]*i/FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]);
bloc[0] = (char)(val & 255);
bloc[1] = (char)(val >> 8);
val = pow(10, gainD[SendDlgItemMessage(hWnd, IDC_COMBO_gainD, CB_GETCURSEL , 0 , 0)]/20.0) * 32767 * sin(2*pi*frequenceD[SendDlgItemMessage(hWnd, IDC_COMBO_frequenceD, CB_GETCURSEL , 0 , 0)]*i/FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]);
bloc[2] = (char)(val & 255);
bloc[3] = (char)(val >> 8);
}
if ((SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0) == 0) && (SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0) == 1))
// mono 16 bit
{
val = pow(10, gain[SendDlgItemMessage(hWnd, IDC_COMBO_gain, CB_GETCURSEL , 0 , 0)]/20.0) * 32767 * sin(2*pi*frequence[SendDlgItemMessage(hWnd, IDC_COMBO_frequence, CB_GETCURSEL , 0 , 0)]*i/FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]);
bloc[0] = (char)(val & 255);
bloc[1] = (char)(val >> 8);
}
if ((SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0) == 0) && (SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0) == 0))
// mono 8 bit
{
val = pow(10, gain[SendDlgItemMessage(hWnd, IDC_COMBO_gain, CB_GETCURSEL , 0 , 0)]/20.0) * 127 * sin(2*pi*frequence[SendDlgItemMessage(hWnd, IDC_COMBO_frequence, CB_GETCURSEL , 0 , 0)]*i/FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]);
bloc[0] = 127+(char)(val & 255);
}
if ((SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0) == 1) && (SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0) == 0))
// stereo 8 bit
{
val = pow(10, gainG[SendDlgItemMessage(hWnd, IDC_COMBO_gainG, CB_GETCURSEL , 0 , 0)]/20.0) * 127 * sin(2*pi*frequenceG[SendDlgItemMessage(hWnd, IDC_COMBO_frequenceG, CB_GETCURSEL , 0 , 0)]*i/FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]);
bloc[0] = 127+(char)(val & 255);
val = pow(10, gainD[SendDlgItemMessage(hWnd, IDC_COMBO_gainD, CB_GETCURSEL , 0 , 0)]/20.0) * 127 * sin(2*pi*frequenceD[SendDlgItemMessage(hWnd, IDC_COMBO_frequenceD, CB_GETCURSEL , 0 , 0)]*i/FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]);
bloc[1] = 127+(char)(val & 255);
}
fwrite(bloc,(canaux[SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0)]*octets[SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0)]),1,fichier);
pointeur=pointeur+(canaux[SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0)]*octets[SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0)]);
}
fseek(fichier, 0L, SEEK_END);
longueur = ftell(fichier);
en_tete[0x04] = (char)((longueur-8) >> 0) & 255;
en_tete[0x05] = (char)((longueur-8) >> 8) & 255;
en_tete[0x06] = (char)((longueur-8) >> 16) & 255;
en_tete[0x07] = (char)((longueur-8) >> 24) & 255;
en_tete[0x16] = (char)(canaux[SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0)] & 255); //nombre de canaux
en_tete[0x18] = (char)(((long)FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)] >> 0) & 255); //fe low
en_tete[0x19] = (char)(((long)FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)] >> 8) & 255); //fe high
en_tete[0x1c] = (char)(((long)FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]*canaux[SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0)]*octets[SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0)]) >> 0) & 255;
en_tete[0x1d] = (char)(((long)FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]*canaux[SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0)]*octets[SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0)]) >> 8) & 255;
en_tete[0x1e] = (char)(((long)FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]*canaux[SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0)]*octets[SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0)]) >> 16) & 255;
en_tete[0x1f] = (char)(((long)FE[SendDlgItemMessage(hWnd, IDC_COMBO_FE, CB_GETCURSEL , 0 , 0)]*canaux[SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0)]*octets[SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0)]) >> 24) & 255;
en_tete[0x20] = (char)(canaux[SendDlgItemMessage(hWnd, IDC_COMBO_canaux, CB_GETCURSEL , 0 , 0)]*octets[SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0)]); // 1 ou 2 ou 4 octet par echantillon
en_tete[0x22] = (char)(octets[SendDlgItemMessage(hWnd, IDC_COMBO_octets, CB_GETCURSEL , 0 , 0)]*8); // format 8 ou 16 bit
en_tete[0x28] = (char)((longueur-44) >> 0) & 255;
en_tete[0x29] = (char)((longueur-44) >> 8) & 255;
en_tete[0x2a] = (char)((longueur-44) >> 16) & 255;
en_tete[0x2b] = (char)((longueur-44) >> 24) & 255;
pointeur=0;
fseek(fichier, pointeur, SEEK_SET);
fwrite(en_tete,44,1,fichier);
fclose(fichier);
EnableWindow(GetDlgItem(hWnd,IDC_WRITE),TRUE);
SetDlgItemText(hWnd,IDC_STATIC1,"terminé");
return 0;
}
}
return 0;
}
//****************************************************************************
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),HWND_DESKTOP,DlgProc);
return 0;
}
//****************************************************************************
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.