Update modtracker

This commit is contained in:
Your New SJW Waifu 2020-04-27 19:22:32 -05:00
parent db9cc4a4e6
commit 113dfa0862
2 changed files with 70 additions and 48 deletions

View File

@ -1,51 +1,60 @@
function AudioPlayer( callback, effect ) {
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext();
var scriptProcessor = audioContext.createScriptProcessor( 0, 0, 2 );
var audioSource = new SineSource( audioContext.sampleRate );
var onaudioprocess = function( event ) {
var count = event.outputBuffer.length;
if( callback ) {
callback( count );
}
var leftBuf = event.outputBuffer.getChannelData( 0 );
var rightBuf = event.outputBuffer.getChannelData( 1 );
audioSource.getAudio( leftBuf, rightBuf, count );
if( effect ) {
effect( leftBuf, rightBuf, count );
}
}
this.getSamplingRate = function() {
return audioContext.sampleRate;
}
this.setAudioSource = function( audioSrc ) {
audioSource = audioSrc;
}
this.play = function() {
scriptProcessor.onaudioprocess = onaudioprocess;
scriptProcessor.connect( audioContext.destination );
}
this.stop = function() {
if( scriptProcessor.onaudioprocess ) {
scriptProcessor.disconnect( audioContext.destination );
scriptProcessor.onaudioprocess = null;
}
}
var audioContext = new AudioContext();
var scriptProcessor = audioContext.createScriptProcessor( 0, 0, 2 );
var audioSource = new SineSource( audioContext.sampleRate );
var gainNode = audioContext.createGain();
gainNode.gain.value = localStorage.getItem('volume') === null ? 0.25 : localStorage.getItem('volume');
gainNode.connect( audioContext.destination );
var onaudioprocess = function( event ) {
var count = event.outputBuffer.length;
if( callback ) {
callback( count );
}
var leftBuf = event.outputBuffer.getChannelData( 0 );
var rightBuf = event.outputBuffer.getChannelData( 1 );
audioSource.getAudio( leftBuf, rightBuf, count );
if( effect ) {
effect( leftBuf, rightBuf, count );
}
}
this.getSamplingRate = function() {
return audioContext.sampleRate;
}
this.setAudioSource = function( audioSrc ) {
audioSource = audioSrc;
}
this.play = function() {
audioContext.resume().then(() => {
scriptProcessor.onaudioprocess = onaudioprocess;
scriptProcessor.connect( gainNode );
});
}
this.stop = function() {
if( scriptProcessor.onaudioprocess ) {
scriptProcessor.disconnect( gainNode );
scriptProcessor.onaudioprocess = null;
}
}
this.setVolume = function( volume ) {
gainNode.gain.value = volume;
}
}
function SineSource( samplingRate ) {
// Simple AudioSource for testing.
var rate = samplingRate;
var freq = 2 * Math.PI * 440 / rate;
var phase = 0;
this.getSamplingRate = function() {
return rate;
}
this.getAudio = function( leftBuffer, rightBuffer, count ) {
for( var idx = 0; idx < count; idx++, phase++ ) {
leftBuffer[ idx ] = Math.sin( phase * freq );
rightBuffer[ idx ] = Math.sin( phase * freq * 0.5 );
}
}
// Simple AudioSource for testing.
var rate = samplingRate;
var freq = 2 * Math.PI * 440 / rate;
var phase = 0;
this.getSamplingRate = function() {
return rate;
}
this.getAudio = function( leftBuffer, rightBuffer, count ) {
for( var idx = 0; idx < count; idx++, phase++ ) {
leftBuffer[ idx ] = Math.sin( phase * freq );
rightBuffer[ idx ] = Math.sin( phase * freq * 0.5 );
}
}
}

View File

@ -6,6 +6,7 @@ PleromaModPlayer = function (attachment) {
this.initComponents();
this.module = null;
this.replay = null;
this.duration = 0;
this.patternDisplay = null;
this.samplePosition = 0;
this.channel = 0;
@ -46,7 +47,19 @@ PleromaModPlayer = function (attachment) {
this.seekBar.setAttribute("type", "range");
this.seekBar.onmousedown = () => { this.stop() };
this.seekBar.onmouseup = () => { this.play() };
this.seekBar.onkeydown = () => { this.stop() };
this.seekBar.onkeyup = () => { this.play() };
controls.appendChild(this.seekBar);
this.volumeBar = document.createElement("input");
this.volumeBar.setAttribute("class", "mod-volumebar");
this.volumeBar.setAttribute("type", "range");
this.volumeBar.value = localStorage.getItem('volume') === null ? 25 : localStorage.getItem('volume') * 100;
this.volumeBar.max = 100;
this.volumeBar.onmouseup = () => { localStorage.setItem('volume', this.volumeBar.value / 100) };
this.volumeBar.onkeyup = () => { localStorage.setItem('volume', this.volumeBar.value / 100) };
this.volumeBar.onmousemove = () => { PleromaModTracker.player.setVolume(this.volumeBar.value / 100) };
controls.appendChild(this.volumeBar);
this.attachment.appendChild(controls);
@ -92,10 +105,10 @@ PleromaModPlayer = function (attachment) {
this.module = new IBXMModule(moduleData);
const rate = PleromaModTracker.player.getSamplingRate();
this.replay = new IBXMReplay(this.module, rate);
const duration = Math.round(this.replay.calculateSongDuration() / rate);
this.duration = Math.round(this.replay.calculateSongDuration() / rate);
this.samplePosition = 0;
this.seekBar.value = 0;
this.seekBar.max = duration;
this.seekBar.max = this.duration;
this.songName.innerText = this.module.songName;
let instruments = "";
for (let idx = 1; idx < this.module.instruments.length; idx++) {
@ -115,7 +128,7 @@ PleromaModPlayer = function (attachment) {
if (count) {
this.samplePosition += count;
}
this.seekBar.value = this.samplePosition / PleromaModTracker.player.getSamplingRate();
this.seekBar.value = this.samplePosition / PleromaModTracker.player.getSamplingRate() % this.duration;
this.patternDisplay.display(this.module, this.replay, this.channel, this.patternCanvas);
},
@ -255,4 +268,4 @@ PleromaModTracker.currentDisplay = null;
}
].forEach((fn) => { PleromaModTracker[fn.name] = fn; });
PleromaModLoader.registerMod(PleromaModTracker);
PleromaModLoader.registerMod(PleromaModTracker);