Skip to content

Commit

Permalink
Bugfixes, minify, more meta
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSnuggles committed Feb 5, 2024
1 parent 685757e commit 5355afa
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 17 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ Why is the Windows version about 4kB smaller?

If you know the answer please let me know.

## ToDo
- build/rollup to make it a single .js request

## History
- 2024-02-04: Metadata contains song, bugfixes and minify
- 2024-01-24: Added config object, Modland player
- 2024-01-23: Drag'n'Drop files. Build library using Docker.
- 2024-01-22: Libopenmpt 0.7.3 compiled with Emscripten 3.1.51
Expand Down
8 changes: 6 additions & 2 deletions chiptune3.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ChiptuneJsPlayer {
this.handlers = []

// worklet
this.context.audioWorklet.addModule('/chiptune/chiptune3.worklet.js')
this.context.audioWorklet.addModule( new URL('./chiptune3.worklet.min.js', import.meta.url) )
.then(()=>{
this.processNode = new AudioWorkletNode(this.context, 'libopenmpt-processor', {
numberOfInputs: 0,
Expand Down Expand Up @@ -64,7 +64,10 @@ export class ChiptuneJsPlayer {
case 'pos':
//this.meta.pos = msg.data.pos
this.currentTime = msg.data.pos
this.fireEvent('onProgress', this.currentTime)
this.order = msg.data.order
this.pattern = msg.data.pattern
this.row = msg.data.row
this.fireEvent('onProgress', msg.data)
break
case 'end':
this.fireEvent('onEnded')
Expand Down Expand Up @@ -115,6 +118,7 @@ export class ChiptuneJsPlayer {
setPitch(val) { this.postMsg('setPitch', val) }
setTempo(val) { this.postMsg('setTempo', val) }
setPos(val) { this.postMsg('setPos', val) }
setOrderRow(o,r) { this.postMsg('setOrderRow', {o:o,r:r}) }
setVol(val) { this.gain.gain.value = val }
selectSubsong(val) { this.postMsg('selectSubsong', val) }
// compatibility
Expand Down
1 change: 1 addition & 0 deletions chiptune3.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 107 additions & 3 deletions chiptune3.worklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MPT extends AudioWorkletProcessor {
stereoSeparation: 100, // percents
interpolationFilter: 0, // https://lib.openmpt.org/doc/group__openmpt__module__render__param.html
}
this.channels = 0
}

process(inputList, outputList, parameters) {
Expand All @@ -74,7 +75,28 @@ class MPT extends AudioWorkletProcessor {
right.set(libopenmpt.HEAPF32.subarray(this.rightPtr / 4, this.rightPtr / 4 + actualFramesPerChunk))

// post progress
this.port.postMessage({cmd:'pos',pos:libopenmpt._openmpt_module_get_position_seconds(this.modulePtr)})
// openmpt_module_get_current_order

let msg = {
cmd: 'pos',
pos: libopenmpt._openmpt_module_get_position_seconds(this.modulePtr),
// pos in song
order: libopenmpt._openmpt_module_get_current_order(this.modulePtr),
pattern: libopenmpt._openmpt_module_get_current_pattern(this.modulePtr),
row: libopenmpt._openmpt_module_get_current_row(this.modulePtr),
// channel volumes
//chVol: [], // ch0Left, ch0Right, ch1Left, ...
}
/*
for (let i = 0; i < this.channels; i++) {
msg.chVol.push( {
left: libopenmpt._openmpt_module_get_current_channel_vu_left(this.modulePtr, i),
right: libopenmpt._openmpt_module_get_current_channel_vu_right(this.modulePtr, i),
})
}
*/

this.port.postMessage( msg )

return true // def. needed for Chrome
}
Expand Down Expand Up @@ -126,6 +148,20 @@ class MPT extends AudioWorkletProcessor {
if (!this.modulePtr) return
libopenmpt._openmpt_module_set_position_seconds(this.modulePtr, v)
break
case 'setOrderRow':
if (!this.modulePtr) return
libopenmpt._openmpt_module_set_position_order_row(this.modulePtr, v.o, v.r)
break
/*
case 'toggleMute'
// openmpt_module_ext_get_interface(mod_ext, interface_id, interface, interface_size)
// openmpt_module_ext_interface_interactive
// set_channel_mute_status
// https://lib.openmpt.org/doc/group__libopenmpt__ext__c.html#ga0275a35da407cd092232a20d3535c9e4
if (!this.modulePtr) return
//const extPtr = libopenmpt.openmpt_module_ext_get_interface(mod_ext, interface_id, interface, interface_size)
break
*/
default:
console.log('Received unknown message',msg.data)
}
Expand Down Expand Up @@ -181,10 +217,77 @@ class MPT extends AudioWorkletProcessor {
libopenmpt._free(this.rightBufferPtr)
this.rightBufferPtr = 0
}
this.channels = 0
}
meta() {
this.port.postMessage({cmd: 'meta', meta: this.getMeta()})
}
getSong() {
if (!libopenmpt.UTF8ToString || !this.modulePtr) return false

// https://lib.openmpt.org/doc/
let song = {
channels: [],
instruments: [],
samples: [],
orders: [],
numSubsongs: libopenmpt._openmpt_module_get_num_subsongs(this.modulePtr),
patterns: [],
}
// channels
const chNum = libopenmpt._openmpt_module_get_num_channels(this.modulePtr)
this.channel = chNum
for (let i = 0; i < chNum; i++) {
song.channels.push( libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_channel_name(this.modulePtr, i)) )
}
// instruments
for (let i = 0, e = libopenmpt._openmpt_module_get_num_instruments(this.modulePtr); i < e; i++) {
song.instruments.push( libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_instrument_name(this.modulePtr, i)) )
}
// samples
for (let i = 0, e = libopenmpt._openmpt_module_get_num_samples(this.modulePtr); i < e; i++) {
song.samples.push( libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_sample_name(this.modulePtr, i)) )
}
// orders
for (let i = 0, e = libopenmpt._openmpt_module_get_num_orders(this.modulePtr); i < e; i++) {
song.orders.push( {
name: libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_order_name(this.modulePtr, i)),
pat: libopenmpt._openmpt_module_get_order_pattern(this.modulePtr, i),
})
}
// patterns
for (let patIdx = 0, patNum = libopenmpt._openmpt_module_get_num_patterns(this.modulePtr); patIdx < patNum; patIdx++) {
const pattern = {
name: libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_pattern_name(this.modulePtr, patIdx)),
rows: [],
}
// rows
for(let rowIdx = 0, rowNum = libopenmpt._openmpt_module_get_pattern_num_rows(this.modulePtr, patIdx); rowIdx < rowNum; rowIdx++) {
const row = []
// channels
for (let chIdx = 0; chIdx < chNum; chIdx++) {
const channel = []
for (let comIdx = 0; comIdx < 6; comIdx++) {
/* commands
OPENMPT_MODULE_COMMAND_NOTE = 0
OPENMPT_MODULE_COMMAND_INSTRUMENT = 1
OPENMPT_MODULE_COMMAND_VOLUMEEFFECT = 2
OPENMPT_MODULE_COMMAND_EFFECT = 3
OPENMPT_MODULE_COMMAND_VOLUME = 4
OPENMPT_MODULE_COMMAND_PARAMETER = 5
*/
channel.push( libopenmpt._openmpt_module_get_pattern_row_channel_command(this.modulePtr, patIdx, rowIdx, chIdx, comIdx) )
}
row.push( channel )
}
pattern.rows.push( row )
}
song.patterns.push( pattern )
}


return song
}
getMeta() {
if (!libopenmpt.UTF8ToString || !this.modulePtr) return false

Expand All @@ -201,8 +304,9 @@ class MPT extends AudioWorkletProcessor {
data[keys[i]] = libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_metadata(this.modulePtr, keyNameBuffer))
libopenmpt._free(keyNameBuffer)
}
data.totalOrders = libopenmpt._openmpt_module_get_num_orders(this.modulePtr)
data.totalPatterns = libopenmpt._openmpt_module_get_num_patterns(this.modulePtr)
data.song = this.getSong()
data.totalOrders = data.song.orders.length // libopenmpt._openmpt_module_get_num_orders(this.modulePtr)
data.totalPatterns = data.song.patterns.length// libopenmpt._openmpt_module_get_num_patterns(this.modulePtr)
data.songs = this.getSongs()
data.libopenmptVersion = libopenmpt.version
data.libopenmptBuild = libopenmpt.build
Expand Down
1 change: 1 addition & 0 deletions chiptune3.worklet.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {LDR} from 'https://DrSnuggles.github.io/LDR/ldr-zip.min.js'
import {kkRows} from 'https://DrSnuggles.github.io/kkRows/js/kk-rows.min.js'
import {Visualizer} from 'https://DrSnuggles.github.io/visualizer/visualizer.min.js'
import {dnd} from './dnd.js'
import {ChiptuneJsPlayer} from './chiptune3.js'
import {ChiptuneJsPlayer} from './chiptune3.min.js'

let isLoading = false

Expand Down Expand Up @@ -63,8 +63,8 @@ function initPlayer() {
player.meta = meta
setMetadata(document.getElementById('modfilename').innerHTML)
})
player.onProgress((pos) => {
document.getElementById('seekbar').value = pos
player.onProgress((dat) => {
document.getElementById('seekbar').value = dat.pos
})
player.onError((err) => {
nextSong()
Expand Down Expand Up @@ -110,7 +110,7 @@ window.nextSong = (url) => {
function userInteracted() {
removeEventListener('keydown', userInteracted)
removeEventListener('click', userInteracted)
removeEventListener('touchstart', userInteracted)
removeEventListener('touchend', userInteracted)
removeEventListener('contextmenu', userInteracted)

audioModal.classList.add('fadeOut')
Expand All @@ -120,13 +120,14 @@ function userInteracted() {
}
addEventListener('keydown', userInteracted)
addEventListener('click', userInteracted)
addEventListener('touchstart', userInteracted)
addEventListener('touchend', userInteracted)
addEventListener('contextmenu', userInteracted)


init()
function init() {
const allowedExt = 'mptm mod s3m xm it 669 amf ams c67 dbm digi dmf dsm dsym dtm far fmt imf ice j2b m15 mdl med mms mt2 mtm mus nst okt plm psm pt36 ptm sfx sfx2 st26 stk stm stx stp symmod ult wow gdm mo3 oxm umx xpk ppm mmcmp'.split(' ')
const allowedExt = 'mptm mod s3m xm it 669 amf ams c67 dbm digi dmf dsm dsym dtm far fmt ice j2b m15 mdl med mms mt2 mtm mus nst okt plm psm pt36 ptm sfx sfx2 st26 stk stm stx stp symmod ult wow gdm mo3 oxm umx xpk ppm mmcmp'.split(' ')
// removed: imf
let data = []

let url = 'https://modland.com/allmods.zip'
Expand Down
4 changes: 3 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ RUN sed -i "s/'_malloc','_free'/'_malloc','_free','stackAlloc','stackSave','stac
RUN make CONFIG=emscripten EMSCRIPTEN_TARGET=audioworkletprocessor

# not sure if you have sed by hand on windows, so i add the IMPORT for polyfills here
RUN sed -i "1 i\import {atob, crypto, performance} from './polyfills.js';" bin/libopenmpt.js
# RUN sed -i "1 i\import {atob, crypto, performance} from './polyfills.js';" bin/libopenmpt.js
# instead of another import this is the tersered polyfills.js
RUN sed -i '1 i\function atob(r){const t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";let e,o,n,a,c,h,d,f="",i=0;r=r.replace(/[^A-Za-z0-9\+\/\=]/g,"");do{a=t.indexOf(r.charAt(i++)),c=t.indexOf(r.charAt(i++)),h=t.indexOf(r.charAt(i++)),d=t.indexOf(r.charAt(i++)),e=a<<2|c>>4,o=(15&c)<<4|h>>2,n=(3&h)<<6|d,f+=String.fromCharCode(e),64!==h&&(f+=String.fromCharCode(o)),64!==d&&(f+=String.fromCharCode(n))}while(i<r.length);return f}const performance={now:()=>Date.now()};const crypto={getRandomValues(r){for(let t=0;t<r.length;t++)r[t]=256*Math.random()|0}};' bin/libopenmpt.js
Loading

0 comments on commit 5355afa

Please sign in to comment.