annotate chiptune2.js @ 7:e22a954c2714

i fucked up
author Paper <mrpapersonic@gmail.com>
date Sun, 30 May 2021 18:10:36 -0400
parents 5507443a71c0
children 8f0b52a3cb69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 // constants
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 const OPENMPT_MODULE_RENDER_STEREOSEPARATION_PERCENT = 2
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 const OPENMPT_MODULE_RENDER_INTERPOLATIONFILTER_LENGTH = 1
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 // audio context
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 var ChiptuneAudioContext = window['AudioContext'] || window['webkitAudioContext'];
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 // config
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 var ChiptuneJsConfig = function (repeatCount, stereoSeparation, interpolationFilter, context)
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 this.repeatCount = repeatCount;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 this.stereoSeparation = stereoSeparation;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 this.interpolationFilter = interpolationFilter;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 this.context = context;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 ChiptuneJsConfig.prototype.constructor = ChiptuneJsConfig;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 // player
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 var ChiptuneJsPlayer = function (config) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 this.config = config;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 this.context = config.context || new ChiptuneAudioContext();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 this.currentPlayingNode = null;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 this.handlers = [];
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 this.touchLocked = true;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 ChiptuneJsPlayer.prototype.constructor = ChiptuneJsPlayer;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 // event handlers section
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 ChiptuneJsPlayer.prototype.fireEvent = function (eventName, response) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 var handlers = this.handlers;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 if (handlers.length) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 handlers.forEach(function (handler) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 if (handler.eventName === eventName) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 handler.handler(response);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 })
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 ChiptuneJsPlayer.prototype.addHandler = function (eventName, handler) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 this.handlers.push({eventName: eventName, handler: handler});
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 ChiptuneJsPlayer.prototype.onEnded = function (handler) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 this.addHandler('onEnded', handler);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 ChiptuneJsPlayer.prototype.onError = function (handler) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 this.addHandler('onError', handler);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 // metadata
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 ChiptuneJsPlayer.prototype.duration = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 return libopenmpt._openmpt_module_get_duration_seconds(this.currentPlayingNode.modulePtr);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 ChiptuneJsPlayer.prototype.getCurrentRow = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 return libopenmpt._openmpt_module_get_current_row(this.currentPlayingNode.modulePtr);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 ChiptuneJsPlayer.prototype.getCurrentPattern = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 return libopenmpt._openmpt_module_get_current_pattern(this.currentPlayingNode.modulePtr);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 ChiptuneJsPlayer.prototype.getCurrentOrder = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 return libopenmpt._openmpt_module_get_current_order(this.currentPlayingNode.modulePtr);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 ChiptuneJsPlayer.prototype.metadata = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 var data = {};
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 var keys = Pointer_stringify(libopenmpt._openmpt_module_get_metadata_keys(this.currentPlayingNode.modulePtr)).split(';');
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 var keyNameBuffer = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 for (var i = 0; i < keys.length; i++) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76 keyNameBuffer = libopenmpt._malloc(keys[i].length + 1);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 writeAsciiToMemory(keys[i], keyNameBuffer);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 data[keys[i]] = Pointer_stringify(libopenmpt._openmpt_module_get_metadata(this.currentPlayingNode.modulePtr, keyNameBuffer));
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 libopenmpt._free(keyNameBuffer);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81 return data;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 ChiptuneJsPlayer.prototype.module_ctl_set = function(ctl, value) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 return libopenmpt.ccall('openmpt_module_ctl_set', 'number', ['number', 'string', 'string'], [this.currentPlayingNode.modulePtr, ctl, value]) === 1;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 // playing, etc
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 ChiptuneJsPlayer.prototype.unlock = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 var context = this.context;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 var buffer = context.createBuffer(1, 1, 22050);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 var unlockSource = context.createBufferSource();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 unlockSource.buffer = buffer;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96 unlockSource.connect(context.destination);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97 unlockSource.start(0);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
98
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
99 this.touchLocked = false;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
100 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
101
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
102 ChiptuneJsPlayer.prototype.load = function(input, callback) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
103
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
104 if (this.touchLocked) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
105 this.unlock();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
106 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108 var player = this;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
110 if (input instanceof File) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
111 var reader = new FileReader();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112 reader.onload = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113 return callback(reader.result); // no error
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
114 }.bind(this);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
115 reader.readAsArrayBuffer(input);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
116 } else {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
117 var xhr = new XMLHttpRequest();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
118 xhr.open('GET', input, true);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
119 xhr.responseType = 'arraybuffer';
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
120 xhr.onload = function(e) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
121 if (xhr.status === 200) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
122 return callback(xhr.response); // no error
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
123 } else {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
124 player.fireEvent('onError', {type: 'onxhr'});
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
125 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
126 }.bind(this);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
127 xhr.onerror = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
128 player.fireEvent('onError', {type: 'onxhr'});
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
129 };
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
130 xhr.onabort = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
131 player.fireEvent('onError', {type: 'onxhr'});
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
132 };
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
133 xhr.send();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
134 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
135 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
136
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
137 ChiptuneJsPlayer.prototype.play = function(buffer) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
138 this.stop();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
139 var processNode = this.createLibopenmptNode(buffer, this.config);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
140 if (processNode == null) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
141 return;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
142 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
143
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
144 // set config options on module
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
145 libopenmpt._openmpt_module_set_repeat_count(processNode.modulePtr, this.config.repeatCount);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
146 libopenmpt._openmpt_module_set_render_param(processNode.modulePtr, OPENMPT_MODULE_RENDER_STEREOSEPARATION_PERCENT, this.config.stereoSeparation);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
147 libopenmpt._openmpt_module_set_render_param(processNode.modulePtr, OPENMPT_MODULE_RENDER_INTERPOLATIONFILTER_LENGTH, this.config.interpolationFilter);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
148
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
149 this.currentPlayingNode = processNode;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
150 processNode.connect(this.context.destination);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
151 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
152
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
153 ChiptuneJsPlayer.prototype.stop = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
154 if (this.currentPlayingNode != null) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
155 this.currentPlayingNode.disconnect();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
156 this.currentPlayingNode.cleanup();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
157 this.currentPlayingNode = null;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
158 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
159 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
160
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
161 ChiptuneJsPlayer.prototype.togglePause = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
162 if (this.currentPlayingNode != null) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
163 this.currentPlayingNode.togglePause();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
164 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
165 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
166
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
167 ChiptuneJsPlayer.prototype.createLibopenmptNode = function(buffer, config) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
168 // TODO error checking in this whole function
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
169
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
170 var maxFramesPerChunk = 4096;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
171 var processNode = this.context.createScriptProcessor(2048, 0, 2);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
172 processNode.config = config;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
173 processNode.player = this;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
174 var byteArray = new Int8Array(buffer);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
175 var ptrToFile = libopenmpt._malloc(byteArray.byteLength);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
176 libopenmpt.HEAPU8.set(byteArray, ptrToFile);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
177 processNode.modulePtr = libopenmpt._openmpt_module_create_from_memory(ptrToFile, byteArray.byteLength, 0, 0, 0);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
178 processNode.paused = false;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
179 processNode.leftBufferPtr = libopenmpt._malloc(4 * maxFramesPerChunk);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
180 processNode.rightBufferPtr = libopenmpt._malloc(4 * maxFramesPerChunk);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
181 processNode.cleanup = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
182 if (this.modulePtr != 0) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
183 libopenmpt._openmpt_module_destroy(this.modulePtr);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
184 this.modulePtr = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
185 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
186 if (this.leftBufferPtr != 0) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
187 libopenmpt._free(this.leftBufferPtr);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
188 this.leftBufferPtr = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
189 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
190 if (this.rightBufferPtr != 0) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
191 libopenmpt._free(this.rightBufferPtr);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
192 this.rightBufferPtr = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
193 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
194 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
195 processNode.stop = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
196 this.disconnect();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
197 this.cleanup();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
198 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
199 processNode.pause = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
200 this.paused = true;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
201 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
202 processNode.unpause = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
203 this.paused = false;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
204 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
205 processNode.togglePause = function() {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
206 this.paused = !this.paused;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
207 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
208 processNode.onaudioprocess = function(e) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
209 var outputL = e.outputBuffer.getChannelData(0);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
210 var outputR = e.outputBuffer.getChannelData(1);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
211 var framesToRender = outputL.length;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
212 if (this.ModulePtr == 0) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
213 for (var i = 0; i < framesToRender; ++i) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
214 outputL[i] = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
215 outputR[i] = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
216 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
217 this.disconnect();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
218 this.cleanup();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
219 return;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
220 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
221 if (this.paused) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
222 for (var i = 0; i < framesToRender; ++i) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
223 outputL[i] = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
224 outputR[i] = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
225 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
226 return;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
227 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
228 var framesRendered = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
229 var ended = false;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
230 var error = false;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
231 while (framesToRender > 0) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
232 var framesPerChunk = Math.min(framesToRender, maxFramesPerChunk);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
233 var actualFramesPerChunk = libopenmpt._openmpt_module_read_float_stereo(this.modulePtr, this.context.sampleRate, framesPerChunk, this.leftBufferPtr, this.rightBufferPtr);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
234 if (actualFramesPerChunk == 0) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
235 ended = true;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
236 // modulePtr will be 0 on openmpt: error: openmpt_module_read_float_stereo: ERROR: module * not valid or other openmpt error
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
237 error = !this.modulePtr;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
238 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
239 var rawAudioLeft = libopenmpt.HEAPF32.subarray(this.leftBufferPtr / 4, this.leftBufferPtr / 4 + actualFramesPerChunk);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
240 var rawAudioRight = libopenmpt.HEAPF32.subarray(this.rightBufferPtr / 4, this.rightBufferPtr / 4 + actualFramesPerChunk);
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
241 for (var i = 0; i < actualFramesPerChunk; ++i) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
242 outputL[framesRendered + i] = rawAudioLeft[i];
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
243 outputR[framesRendered + i] = rawAudioRight[i];
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
244 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
245 for (var i = actualFramesPerChunk; i < framesPerChunk; ++i) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
246 outputL[framesRendered + i] = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
247 outputR[framesRendered + i] = 0;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
248 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
249 framesToRender -= framesPerChunk;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
250 framesRendered += framesPerChunk;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
251 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
252 if (ended) {
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
253 this.disconnect();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
254 this.cleanup();
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
255 error ? processNode.player.fireEvent('onError', {type: 'openmpt'}) : processNode.player.fireEvent('onEnded');
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
256 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
257 }
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
258 return processNode;
5507443a71c0 make website awesomer
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
259 }