Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $fundamentalTone = readline("Fundamental tone (in Hz) : ");
- $octaves = readline("Number of octaves : ");
- $tonesPerOctave = readline("Tones per octave : ");
- function tone_generator($freqOfTone = 440, $i) {
- $sampleRate = 44100;
- $samplesCount = 80000;
- $amplitude = 0.25 * 32768;
- $w = 2 * pi() * $freqOfTone / $sampleRate;
- $samples = array();
- for ($n = 0; $n < $samplesCount; $n++) {
- $samples[] = (int)($amplitude * sin($n * $w));
- }
- $srate = 44100; //sample rate
- $bps = 16; //bits per sample
- $Bps = $bps/8; //bytes per sample
- $str = call_user_func_array("pack",
- array_merge(array("VVVVVvvVVvvVVv*"),
- array(//header
- 0x46464952, //RIFF
- 1600038, //File size
- 0x45564157, //WAVE
- 0x20746d66, //"fmt " (chunk)
- 16, //chunk size
- 1, //compression
- 1, //nchannels
- $srate, //sample rate
- $Bps*$srate, //bytes/second
- $Bps, //block align
- $bps, //bits/sample
- 0x61746164, //"data"
- 160000 //chunk size
- ),
- $samples //data
- )
- );
- $myfile = fopen("sine$i.wav", "wb") or die("Unable to open file!");
- fwrite($myfile, $str);
- fclose($myfile);
- }
- $content = "<title>SINE, that's the wrong NUMBAH !</title>
- <style>
- table {
- height : 100%;
- width : 100%;
- }
- td {
- border : solid 1px black;
- width : " . (100/$tonesPerOctave) . "%;
- }
- </style>
- <table cellspacing='0'>
- ";
- for ($t = 0; $t < $tonesPerOctave*$octaves; $t++) {
- tone_generator($fundamentalTone*pow(2,$t/$tonesPerOctave), $t);
- if ($t % $tonesPerOctave == 0) {
- $content .= "\n<tr>";
- }
- $content .= "<script>var sine$t = new Audio('sine$t.wav')</script>\n<td onclick='sine$t.play()'>≈ " . round($fundamentalTone*pow(2,$t/$tonesPerOctave), 2) . " Hz";
- }
- file_put_contents("sine.html", $content);
- ?>
Add Comment
Please, Sign In to add comment