annotate js/chiptune2.js @ 66:ba20211d6b00

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