Pisząc program który miał mi pomóc w przygotowaniu się do zajęć na studiach, postanowiłem umieścić w nim regulator głośności, a że moje studia dotyczą muzyki/dźwięku, postanowiłem wyskalować regulator głośności w decybelach.
Dla niewtajemniczonych skala decybelowa, jest skalą logarytmiczną. Aby określić w decybelach wzmocnienie posługujemy się formułą:
20*log(A1/A2)=x dB
Nasz regulator jest tłumikiem, który obniża poziom wyjściowy, a wartościami, których szukamy jest stosunek A1/A2 dla zadanego x. Po przekształceniu otrzymujemy, że
A1/A2=10^(x/20)
Reszta, to już łopatologiczna praca polegająca na wyznaczeniu całkiem sporej ilości A1/A2 i pomnożenie ich przez 65535, zaokrąglenie i konwersja na format szesnastkowy. każdą taką liczbę trzeba umieścić w odpowiednim parametrze funkcji WaveOutSetvolume
64: WaveOutSetvolume(0, $FFFFFFFF);
trzeba taką liczbę wpisać dwukrotnie, ponieważ każda stanowi głośność jednego kanału.
Regulator głośności zaprezentowany poniżej powinien wystarczyć na potrzeby większości użytkowników, którzy potrzebują decybeli. Zawsze można usunąć część wartości i zostawić np. tylko całkowite.
Rozwiązanie testowałem pod Windows 7 RC i mogę powiedzieć, że parametr głośności, zmienia się tylko w programie i nie narusza w żaden sposób tego, co się dzieje poza nim.
Zaznaczam, że lista parametrów nie wyczerpuje całego zakresu głośności, jako, że 16bitowa regulacja głośności (z taką mamy do czynienia w funkcji WaveOutSetVolume) zapewnia dynamikę około 96 dB
Regulator głośności stanowi TrackBar, a regulacja jest realizowana przez WaveOutSetvolume (pamiętamy, aby dodać biblotekę MMSystem do sekcji uses).
Poniżej przedstawiam kodu:
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
case trackBar1.Position of
0: WaveOutSetvolume(0, $00000000); // całkowite wyciszenie
1: WaveOutSetvolume(0, $06CF06CF); // -31,5 dB
2: WaveOutSetvolume(0, $07370737); // -31,0 dB
3: WaveOutSetvolume(0, $07A407A4); // -30,5dB
4: WaveOutSetvolume(0, $08180818); // -30 dB dB
5: WaveOutSetvolume(0, $08930893); //
6: WaveOutSetvolume(0, $09150915); // -29 dB
7: WaveOutSetvolume(0, $099F099F); //
8: WaveOutSetvolume(0, $0A300A30); // -28 dB
9: WaveOutSetvolume(0, $0ACB0ACB); //
10: WaveOutSetvolume(0, $0B6F0B6F); // -27 dB
11: WaveOutSetvolume(0, $0C1C0C1C); //
12: WaveOutSetvolume(0, $0CD40CD4); // -26 dB
13: WaveOutSetvolume(0, $0D970D97); //
14: WaveOutSetvolume(0, $0E650E65); // -25 dB
15: WaveOutSetvolume(0, $0F3F0F3F); //
16: WaveOutSetvolume(0, $10261026); // -24 dB
17: WaveOutSetvolume(0, $111B111B); //
18: WaveOutSetvolume(0, $121F121F); // -23 dB
19: WaveOutSetvolume(0, $13321332); //
20: WaveOutSetvolume(0, $14551455); // -22 dB
21: WaveOutSetvolume(0, $158A158A); //
22: WaveOutSetvolume(0, $16D016D0); // -21 dB
23: WaveOutSetvolume(0, $182A182A); //
24: WaveOutSetvolume(0, $19991999); // -20 dB
25: WaveOutSetvolume(0, $1B1D1B1D); //
26: WaveOutSetvolume(0, $1CB91CB9); // -19 dB
27: WaveOutSetvolume(0, $1E6C1E6C); //
28: WaveOutSetvolume(0, $203A203A); // -18 dB
29: WaveOutSetvolume(0, $22232223); //
30: WaveOutSetvolume(0, $24292429); // -17 dB
31: WaveOutSetvolume(0, $264D264D); //
32: WaveOutSetvolume(0, $28922892); // -16 dB
33: WaveOutSetvolume(0, $2AFA2AFA); //
34: WaveOutSetvolume(0, $2D852D85); // -15 dB
35: WaveOutSetvolume(0, $30383038); //
36: WaveOutSetvolume(0, $33133313); // -14 dB
37: WaveOutSetvolume(0, $361A361A); //
38: WaveOutSetvolume(0, $394F394F); // -13 dB
39: WaveOutSetvolume(0, $3CB43CB4); //
40: WaveOutSetvolume(0, $404D404D); // -12 dB
41: WaveOutSetvolume(0, $441D441D); //
42: WaveOutSetvolume(0, $48264826); // -11 dB
43: WaveOutSetvolume(0, $4C6C4C6C); //
44: WaveOutSetvolume(0, $50F350F3); // -10 dB
45: WaveOutSetvolume(0, $55BF55BF); //
46: WaveOutSetvolume(0, $5AD45AD4); // -9 dB
47: WaveOutSetvolume(0, $60366036); //
48: WaveOutSetvolume(0, $65E965E9); // -8 dB
49: WaveOutSetvolume(0, $6BF36BF3); //
50: WaveOutSetvolume(0, $72597259); // -7 dB
51: WaveOutSetvolume(0, $791F791F); //
52: WaveOutSetvolume(0, $804D804D); // -6 dB
53: WaveOutSetvolume(0, $87E787E7); //
54: WaveOutSetvolume(0, $8FF58FF5); // -5 dB
55: WaveOutSetvolume(0, $987C987C); //
56: WaveOutSetvolume(0, $A185A185); // -4 dB
57: WaveOutSetvolume(0, $AB17AB17); //
58: WaveOutSetvolume(0, $B53BB53B); // -3 dB
59: WaveOutSetvolume(0, $BFF8BFF8); //
60: WaveOutSetvolume(0, $CB58CB58); // -2 dB
61: WaveOutSetvolume(0, $D764D764); //
62: WaveOutSetvolume(0, $E428E428); // -1 dB
63: WaveOutSetvolume(0, $F1ADF1AD); // -0,5 dB
64: WaveOutSetvolume(0, $FFFFFFFF); // 0 dB
end;
end;
</u>