-
Notifications
You must be signed in to change notification settings - Fork 0
/
nWebAudioWave.js
89 lines (67 loc) · 2.58 KB
/
nWebAudioWave.js
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
nWebAudio['wave'] = [];
nWebAudio['wave']['canvas'] = 'nWebAudioCanvasWave';
nWebAudio['wave']['config'] = new Array();
nWebAudio['wave']['config']['width'] = 550;
nWebAudio['wave']['config']['height'] = 50;
document.addEventListener("click", nWebAudio_waveJump, true);
function nWebAudio_waveJump(myEvent)
{
if( myEvent.target.id == nWebAudio['wave']['canvas'] )
{
if( nWebAudio['routing']['audioElement'].paused == false )
{
nWebAudio['routing']['audioElement'].currentTime = nWebAudio['routing']['audioElement'].duration * ( myEvent.offsetX / document.getElementById(nWebAudio['wave']['canvas']).offsetWidth );
}
}
}
function nWebAudio_waveDraw(waveFormBuffer)
{
nWaveLength = waveFormBuffer.length;
nWaveDuration = waveFormBuffer.duration;
nWaveSampleRate = waveFormBuffer.sampleRate;
nWaveChannelNr = waveFormBuffer.numberOfChannels;
nWaveChannel = waveFormBuffer.getChannelData(0);
var nCVwvRendLen = nWaveChannel.length / 1000;
var nCVwv = document.getElementById(nWebAudio['wave']['canvas']);
nCVwv = nCVwv.getContext('2d');
nCVwv.strokeStyle = '#00FFFF';
nCVwv.fillStyle = '#000000';
nCVwv.fillRect(0, 0, nWebAudio['wave']['config']['width'], nWebAudio['wave']['config']['height']);
nCVwv.translate(0,nWebAudio['wave']['config']['height'] / 2);
for( i = 0; i < nCVwvRendLen; i++ )
{
var x = Math.floor( nWebAudio['wave']['config']['width'] * i / nCVwvRendLen );
var y = nWaveChannel[(i*1000)] * nWebAudio['wave']['config']['height'] / 2;
nCVwv.beginPath();
nCVwv.moveTo(x , 0);
nCVwv.lineTo(x+1, y);
nCVwv.stroke();
nCVwv.closePath();
}
nCVwv.translate(0,-(nWebAudio['wave']['config']['height'] / 2));
numberOfSF = Math.round( nWaveLength / 100000 ) / 10;
}
function nWebAudio_waveLoad()
{
try
{
audioCtxFile = new AudioContext();
var request = new XMLHttpRequest();
request.open('GET', nWebAudio['routing']['audioElement'].src, true);
request.responseType = 'arraybuffer';
request.onload = function()
{
audioCtxFile.decodeAudioData(request.response, function(buffer)
{
nWaveFormBuffer = buffer;
nWebAudio_waveDraw(nWaveFormBuffer);
}, onDecodeError);
}
request.send();
} catch(e)
{
console.log(e);
}
}
function onDecodeError() { alert('Kann Datei nicht lesen.'); }
document.onload = window.setTimeout("nWebAudio_waveLoad()", 250);