1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
| unit UnitMusique;
interface
uses MMSystem;
const
//Mesures
triple_croche =93;
double_croche = 187;
croche34 = 281; //partition en 3:4
croche = 375;
noire34 = 563; //Partition en 3:4
noire = 750;
blanche34 = 1125; //Partition en 3:4
blanche = 1500;
ronde = 3000;
//Tons, au nombre de douze, numérotés de 1 à 12
_SI_d=1 ; _DO_=1; //Do
_DO_d=2; _RE_b=2; //Do dièse et Ré bémol
_RE_=3; //Ré
_RE_d=4; _MI_b=4; //Ré dièse et Mi bémol
_MI_=5; _FA_b=5; //Mi et Fa bémol
_MI_d=6; _FA_=6; //Mi dièse et Fa
_FA_d=7; _SOL_b=7; //Fa dièse et SOL bémol, etc...
_SOL_=8;
_SOL_d=9; _LA_b=9;
_LA_=10;
_LA_d=11; _SI_b=11;
_SI_=12; _DO_b=12;
type
TVolumeLevel = 0..127;
procedure MusiqueMystere;
implementation
uses classes,dialogs,sysutils,windows,forms;
procedure MakeSound(Frequence {Hz}:double; Duree{mSec}: Integer; Volume: TVolumeLevel);
var
WaveFormatEx: TWaveFormatEx;
MS: TMemoryStream;
i, TempInt, DataCount, RiffCount: integer;
SoundValue: byte;
w,v: double; // omega=2*pi*fréquence
const
Mono: Word = $0001; //Son Mono
SampleRate: Integer = 11025; // autres taux d'échantillonnage possibles 8000, 11025, 22050, or 44100
//Champs de l'en-tête d'un fichier .WAV
RiffId: string = 'RIFF';
WaveId: string = 'WAVE';
FmtId: string = 'fmt ';
DataId: string = 'data';
begin
//si la fréquence de la note est trop élevée par rapport au taux d'échantillonnage
if Frequence > (0.6 * SampleRate) then
begin
//... on considère que la note sera injouable.
ShowMessage(Format('Un taux d''échantillonage de %d est trop bas pour jouer une tonalité de %gHz',
[SampleRate, Frequence]));
Exit;
end;
with WaveFormatEx do
begin
//Paramètres d'un en-tête de fichier WAV au format PCM
wFormatTag := WAVE_FORMAT_PCM;
nChannels := Mono;
nSamplesPerSec := SampleRate;
wBitsPerSample := $0008;
nBlockAlign := (nChannels * wBitsPerSample) div 8;
nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
cbSize := 0;
end;
MS := TMemoryStream.Create;
with MS do
begin
//Calcule la longueur des données du son à jouer et celle du fichier WAV qui en découle
DataCount := (Duree * SampleRate) div 1000; // sound data
RiffCount := Length(WaveId) + Length(FmtId) + SizeOf(DWORD) +
SizeOf(TWaveFormatEx) + Length(DataId) + SizeOf(DWORD) + DataCount; // données du fichier
//écriture de l'en-tête du fichier WAV dans le flux mémoire
Write(RiffId[1], 4); // 'RIFF'
Write(RiffCount, SizeOf(DWORD)); // taille du fichier
Write(WaveId[1], Length(WaveId)); // 'WAVE'
Write(FmtId[1], Length(FmtId)); // 'fmt '
TempInt := SizeOf(TWaveFormatEx);
Write(TempInt, SizeOf(DWORD)); // taille de TWaveFormat
Write(WaveFormatEx, SizeOf(TWaveFormatEx)); // taille d'un enregistrement WaveFormatEx
Write(DataId[1], Length(DataId)); // 'data'
Write(DataCount, SizeOf(DWORD)); // taille des données du son
//calcule et écrit le signal sonore
// calcul des valeurs qui composent le son,
// c'est une courbe sinusoïdale dont les valeurs sont comprises entre 0 et 255,
//donc l'axe médian de la sinusoïde est en y=127)
w := 2 * Pi * Frequence; // omega
v:=0.05;
for i := 0 to DataCount - 1 do
begin
//enveloppe d'attaque
if (i<19) then v:=v+0.05;
//enveloppe de descente
if (i>(DataCount-19)) then v:=v-0.05;
SoundValue := 127 + trunc(v*Volume * sin(i * w / SampleRate)); // explication : wt = w * i / SampleRate
//on écrit notre "point" de la courbe dans notre fichier WAV
Write(SoundValue, SizeOf(Byte));
end;
//Maintenant on joue le son en disant à Windows :
//-SND_MEMORY : c'est un son écrit dans un flux mémoire
//-SND_SYNC : de jouer le son de façon synchrone (tant que le son n'est pas terminé, le code ne se déroule plus.
// Ainsi, un deuxième appel consécutif à notre procédure ne "coupera pas le sifflet" du premier appel.
sndPlaySound(MS.Memory, SND_MEMORY or SND_SYNC);
MS.Free;
end;
end;
procedure Pause(const nDuree: Cardinal);
var
Tc: Cardinal;
begin
Tc := GetTickCount;
repeat
Application.ProcessMessages;
Sleep(1);
until Cardinal(GetTickCount - Tc) > nDuree;
end;
//calcule 2^n, n peut être un réel
function DeuxPuissance(n:double):double;
begin
result:=Exp(n*Ln(2));
end;
procedure Jouer(Ton,Octave,Duree:Integer);
const ref=440;
var F:double;
begin
//Calcul de la fréquence de la note, en fonction du ton et de l'octave
f:=ref*DeuxPuissance((Octave-3)+(Ton-10)/12);
MakeSound(f,Duree,127);
Pause(20);
end;
procedure MusiqueMystere;
begin
Jouer (_DO_,4,noire);
Jouer (_SOL_,4,noire);
Jouer (_FA_,4,triple_croche);
Jouer (_MI_,4,triple_croche);
Jouer (_RE_,4,triple_croche);
Jouer (_DO_,5,noire);
Jouer (_SOL_,4,noire);
Jouer (_FA_,4,triple_croche);
Jouer (_MI_,4,triple_croche);
Jouer (_RE_,4,triple_croche);
Jouer (_DO_,5,noire);
Jouer (_SOL_,4,noire);
Jouer (_FA_,4,triple_croche);
Jouer (_MI_,4,triple_croche);
Jouer (_FA_,4,triple_croche);
Jouer (_RE_,4,noire);
Jouer (_SOL_,3,triple_croche);
Jouer (_SOL_,3,triple_croche);
Jouer (_SOL_,3,triple_croche);
Jouer (_DO_,4,noire);
Jouer (_SOL_,4,noire);
Jouer (_FA_,4,triple_croche);
Jouer (_MI_,4,triple_croche);
Jouer (_RE_,4,triple_croche);
Jouer (_DO_,5,noire);
Jouer (_SOL_,4,noire);
Jouer (_FA_,4,triple_croche);
Jouer (_MI_,4,triple_croche);
Jouer (_RE_,4,triple_croche);
Jouer (_DO_,5,noire);
Jouer (_SOL_,4,noire);
Jouer (_LA_d,4,triple_croche);
Jouer (_LA_,4,triple_croche);
Jouer (_LA_d,4,triple_croche);
Jouer (_SOL_,4,noire);
Jouer (_SOL_,4,noire);
Jouer (_SOL_,3,noire);
Jouer (_SOL_,3,triple_croche);
Jouer (_LA_,3,noire34);
Jouer (_LA_,3,double_croche);
Jouer (_FA_,4,double_croche);
Jouer (_MI_,4,double_croche);
Jouer (_RE_,4,double_croche);
Jouer (_DO_,4,double_croche);
Jouer (_DO_,4,triple_croche);
Jouer (_RE_,4,triple_croche);
Jouer (_MI_,4,triple_croche);
Jouer (_RE_,4,triple_croche);
Jouer (_LA_,3,double_croche);
Jouer (_SI_,3,croche);
Jouer (_SOL_,3,croche34);
Jouer (_SOL_,3,triple_croche);
Jouer (_LA_,3,noire34);
Jouer (_LA_,3,double_croche);
Jouer (_FA_,4,double_croche);
Jouer (_MI_,4,double_croche);
Jouer (_RE_,4,double_croche);
Jouer (_DO_,4,double_croche);
Jouer (_SOL_,4,double_croche);
Jouer (_RE_,4,noire34);
Jouer (_SOL_,3,croche34);
Jouer (_SOL_,3,triple_croche);
Jouer (_LA_,3,noire34);
Jouer (_LA_,3,double_croche);
Jouer (_FA_,4,double_croche);
Jouer (_MI_,4,double_croche);
Jouer (_RE_,4,double_croche);
Jouer (_DO_,4,double_croche);
Jouer (_DO_,4,triple_croche);
Jouer (_RE_,4,triple_croche);
Jouer (_MI_,4,triple_croche);
Jouer (_RE_,4,triple_croche);
Jouer (_LA_,3,double_croche);
Jouer (_SI_,3,croche);
Jouer (_SOL_,4,croche34);
Jouer (_SOL_,4,triple_croche);
Jouer (_DO_,5,croche34);
Jouer (_LA_d,4,triple_croche);
Jouer (_SOL_d,4,croche34);
Jouer (_SOL_,4,triple_croche);
Jouer (_FA_,4,croche34);
Jouer (_RE_d,4,triple_croche);
Jouer (_RE_,4,croche34);
Jouer (_DO_,4,croche34);
Jouer (_SOL_,4,noire);
Jouer (_SOL_,4,croche34);
Jouer (_SOL_,4,triple_croche);
Jouer (_SOL_,4,triple_croche);
Jouer (_SOL_,4,triple_croche);
Jouer (_SOL_,4,triple_croche);
Jouer (_DO_,5,double_croche);
Jouer (_DO_,4,triple_croche);
Jouer (_DO_,4,triple_croche);
Jouer (_DO_,4,triple_croche);
Jouer (_DO_,4,noire34);
end;
end. |
Partager