forked from dapphp/PHPWavUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
171 lines (133 loc) · 4.65 KB
/
example.php
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
<?php
require_once 'WavFile.php';
require_once 'WavMaker.php';
siusecase();
//maryHad();
//noiseTest();
//sineTest();
//squareTest();
//sineWave();
mergeWavs();
function siusecase()
{
$audio_path = dirname(__FILE__) . '/audio';
$wav = new WavMaker(1, 11025, 16);
$letters = array('p', 'k', 'l', '2', '5', 'm');
$wavs = array();
foreach ($letters as $letter) {
$letter = strtoupper($letter);
if (!isset($wavs[$letter])) {
try {
$l = new WavFile($audio_path . '/' . $letter . '.wav');
$wavs[$letter] = $l;
} catch (Exception $ex) {
// failed to open file...handle
die("Error with character '$letter': " . $ex->getMessage());
}
}
$wav->appendWav($wavs[$letter]);
}
$wav->save(dirname(__FILE__) . '/wavs/siout.wav');
$sound = new WavFile(dirname(__FILE__) . '/wavs/mary.wav');
$wav->mergeWav($sound);
$wav->save(dirname(__FILE__) . '/wavs/siout-merged.wav');
die('Saved captcha');
}
function mergeWavs()
{
$wav1 = new WavFile(dirname(__FILE__) . '/wavs/spin.wav');
$wav2 = new WavFile(dirname(__FILE__) . '/wavs/sinetest-2-44100-8.wav');
$wav1->mergeWav($wav2);
$fp = fopen(dirname(__FILE__) . '/wavs/merged.wav', 'w+b');
fwrite($fp, $wav1->makeHeader());
fwrite($fp, $wav1->getDataSubchunk());
fclose($fp);
die('Merge completed');
}
function maryHad()
{
$wav = new WavMaker(1, 11025, 16);
$wav->generateSineWav(493.883, 0.4); // b
$wav->generateSineWav(440, 0.4); // a
$wav->generateSineWav(391.995, 0.4); // g
$wav->generateSineWav(440, 0.4); // a
$wav->generateSineWav(493.883, 0.4); // b
$wav->generateSineWav(493.883, 0.4); // b
$wav->generateSineWav(493.883, 0.8); // b
$wav->generateSineWav(440, 0.4); // a
$wav->generateSilence(0.05);
$wav->generateSineWav(440, 0.4); // a
$wav->generateSilence(0.05);
$wav->generateSineWav(440, 0.8); // a
$wav->generateSineWav(493.883, 0.4); // b
$wav->generateSineWav(587.330, 0.4); // d
$wav->generateSineWav(587.330); // d
$wav->generateSilence(2); // long pause
$wav->generateSineWav(493.883, 0.4); // b
$wav->generateSineWav(440, 0.4); // a
$wav->generateSineWav(391.995, 0.4); // g
$wav->generateSineWav(440, 0.4); // a
$wav->generateSineWav(493.883, 0.4); // b
$wav->generateSineWav(493.883, 0.4); // b
$wav->generateSineWav(493.883, 0.8); // b
$wav->generateSineWav(440, 0.4); // a
$wav->generateSilence(0.05);
$wav->generateSineWav(440, 0.4); // a
$wav->generateSilence(0.05);
$wav->generateSineWav(440, 0.8); // a
$wav->generateSineWav(493.883, 0.4); // b
$wav->generateSineWav(587.330, 0.4); // d
$wav->generateSineWav(587.330); // d
$wav->save(dirname(__FILE__) . '/wavs/mary.wav');
die('Mary had a little lamb');
}
function sineTest()
{
// generate 3 second sine waves in multiple bit and sample rates
$sps = array(8000, 11025, 22050, 44100);
$bps = array(8, 16, 24);
foreach($sps as $samplesPerSec) {
foreach($bps as $bitsPerSample) {
$wav = new WavMaker(2, $samplesPerSec, $bitsPerSample);
$wav->generateSineWav(329.628, 3);
$wav->save(dirname(__FILE__) . '/wavs/sinetest-2-' . $samplesPerSec . '-' . $bitsPerSample . '.wav');
}
}
die('Sine test completed');
}
function squareTest()
{
$sps = array(8000, 11025, 22050, 44100);
$bps = array(8, 16, 24);
foreach($sps as $samplesPerSec) {
foreach($bps as $bitsPerSample) {
$wav = new WavMaker(1, $samplesPerSec, $bitsPerSample);
$wav->generateSquareWave(130.813, 3);
$wav->save(dirname(__FILE__) . '/wavs/squaretest-1-' . $samplesPerSec . '-' . $bitsPerSample . '.wav');
}
}
die('Square test completed');
}
function noiseTest()
{
$sps = array(8000, 44100);
$bps = array(8, 16);
foreach($sps as $samplesPerSec) {
foreach($bps as $bitsPerSample) {
$wav = new WavMaker(1, $samplesPerSec, $bitsPerSample);
$wav->generateNoise(3);
$wav->save(dirname(__FILE__) . '/wavs/noise-1-' . $samplesPerSec . '-' . $bitsPerSample . '.wav');
}
}
die('Noise test completed');
}
function sineWave()
{
$wav = new WavMaker(1, 44100, 16); // 2 channel, 44100 samples/sec, 16 bits/sample
$wav->generateSineWav(659.255, 3); // E5 for 2 seconds
$fp = fopen(dirname(__FILE__) . '/wavs/sine.wav', 'w+b');
fwrite($fp, $wav->makeHeader());
fwrite($fp, $wav->getDataSubchunk());
fclose($fp);
die('Sine wav completed');
}