diff --git a/public/index.html b/public/index.html index 44a11a4..f88b462 100644 --- a/public/index.html +++ b/public/index.html @@ -12,8 +12,9 @@
-
");
+ var img = document.createElement("img")
+ promises.push(promiseLoad(img))
+ img.id = "music-bg"
+ img.src = "/songs/" + id + "/bg.png"
+ document.getElementById("assets").appendChild(img)
- var audio = new Audio();
- audio.muted = true;
- audio.src = '/songs/'+_selectedSong.folder+'/main.mp3';
- audio.load();
-
- $("#music-bg").load(function(){
- _bgLoaded=true;
- _this.checkIfEverythingLoaded();
- });
-
- audio.playAsset = function(){
- try{
- audio.muted = false;
- audio.play()
- }catch(e){
- console.warn(e)
+ promises.push(new Promise((resolve, reject) => {
+ var songObj
+ assets.songs.forEach(song => {
+ if(song.id == id){
+ songObj = song
+ }
+ })
+ if(songObj.sound){
+ songObj.sound.gain = snd.musicGain
+ resolve()
+ }else{
+ snd.musicGain.load("/songs/" + id + "/main.mp3").then(sound => {
+ songObj.sound = sound
+ resolve()
+ }, reject)
}
- }
+ }))
- audio.onloadeddata = function(){
- _musicLoaded=true;
- assets.sounds["main-music"]=audio;
- _this.checkIfEverythingLoaded();
- };
-
- $.ajax({
- url : _songFilePath,
- dataType: "text",
- success : function (data) {
- _songData = data.split("\n");
- _songDataLoaded=true;
- _this.checkIfEverythingLoaded();
- }
- });
+ promises.push(ajax(this.songFilePath).then(data => {
+ this.songData = data.replace(/\0/g, "").split("\n")
+ }))
+ Promise.all(promises).then(() => {
+ $("#screen").load("/src/views/game.html", () => {
+ var taikoGame = new Controller(this.selectedSong, this.songData, this.autoPlayEnabled)
+ taikoGame.run()
+ })
+ }, () => {
+ alert("An error occurred, please refresh")
+ })
}
-
- this.checkIfEverythingLoaded = function(){
- if(_musicLoaded && _songDataLoaded && _bgLoaded){
- $("#screen").load("/src/views/game.html", function(){
- var taikoGame = new Controller(_selectedSong, _songData, autoPlayEnabled);
- taikoGame.run();
- });
- }
- }
-
- $("#screen").load("/src/views/loadsong.html", _this.run);
}
\ No newline at end of file
diff --git a/public/src/js/main.js b/public/src/js/main.js
deleted file mode 100644
index c305166..0000000
--- a/public/src/js/main.js
+++ /dev/null
@@ -1,7 +0,0 @@
-$(document).ready(function(){
-
- new Loader();
-
-});
-
-
diff --git a/public/src/js/scoresheet.js b/public/src/js/scoresheet.js
index d5a9eb7..7280ada 100644
--- a/public/src/js/scoresheet.js
+++ b/public/src/js/scoresheet.js
@@ -1,18 +1,18 @@
function Scoresheet(controller, score){
-
- var _this = this;
- var _score = score;
- var _mark;
+
+ var _this = this;
+ var _score = score;
+ var _mark;
this.setResults = function(){
if (_score.fail == 0) {
- _mark = 'gold';
- } else if (_score.hp >= 50) {
- _mark = 'silver';
- };
-
+ _mark = 'gold';
+ } else if (_score.hp >= 50) {
+ _mark = 'silver';
+ };
+
var imgW = (_score.hp*$("#score-hp-bar-colour").width())/100;
$("#score-hp-bar-colour img").css("clip", "rect(0, "+imgW+"px, "+$("#score-hp-bar-colour").height()+"px, 0)");
@@ -23,13 +23,13 @@ function Scoresheet(controller, score){
} else {
$("#score-mark").remove();
};
-
+
$("#score-points").html(_score.points+"点");
$("#nb-great").html(_score.great);
$("#nb-good").html(_score.good);
$("#nb-fail").html(_score.fail);
$("#max-combo").html(_score.maxCombo);
-
+
$('.result-song').attr('alt', _score.song).html(_score.song);
}
@@ -76,36 +76,29 @@ function Scoresheet(controller, score){
}
- this.run = function(){
+ this.run = function(){
_this.positionning();
_this.setResults();
- $("#song-select").click(function(){
- assets.sounds["don"].playAsset();
- bgm.pause();
- controller.songSelection();
- });
-
- $("#replay").click(function(){
- assets.sounds["don"].playAsset();
- bgm.pause();
- controller.restartSong();
- });
+ $("#song-select").click(function(){
+ assets.sounds["don"].play();
+ assets.sounds["bgm_result"].stop();
+ controller.songSelection();
+ });
+
+ $("#replay").click(function(){
+ assets.sounds["don"].play();
+ assets.sounds["bgm_result"].stop();
+ controller.restartSong();
+ });
$(window).resize(_this.positionning);
- }
-
- assets.sounds["results"].playAsset();
+ }
+
+ assets.sounds["results"].play()
+ assets.sounds["bgm_result"].playLoop(0, false, 0, 0.846625)
- bgm = new BufferedLoop(
- {url: '/assets/audio/bgm_result.ogg', duration: 0.847},
- {url: '/assets/audio/bgm_result_loop.ogg', duration: 16.842}
- );
- bgm.play();
-
-
-
$("#screen").load("/src/views/scoresheet.html", _this.run);
}
\ No newline at end of file
diff --git a/public/src/js/songselect.js b/public/src/js/songselect.js
index b741f2b..7ab7bdd 100644
--- a/public/src/js/songselect.js
+++ b/public/src/js/songselect.js
@@ -1,82 +1,88 @@
function SongSelect(){
-
- var _this=this;
- var _songs;
+ var _this=this;
+ var _songs;
var _selectedSong = {title:'', folder:'', difficulty:''};
var _preview;
- var _preview_to;
+ var _preview_ended
+ var _preview_startLoad
var _diffNames={
easy:"かんたん",
normal:"ふつう",
hard:"むずかしい",
oni:"おに"
}
-
+
this.startPreview = function(id, prvtime, first_open=true) {
- var start = Date.now();
- setTimeout(function(){
- bgm.pause();
- }, 400);
-
- _preview = new Audio('/songs/' + id + '/main.mp3');
- _preview.onloadeddata = function() {
- var end = Date.now();
- var delay = end - start;
- var no_delay = first_open ? 0 : 300;
-
- _preview.currentTime = prvtime/1000;
- _preview.volume = 0.5;
-
- _preview.addEventListener('ended', function(){
- this.currentTime = prvtime/1000;
- this.play();
- }, false);
-
- _preview_to = setTimeout(function(){
- _preview.play();
- }, delay <= 1000 && first_open ? 1000 : no_delay);
+ _this.endPreview();
+ _preview_startLoad = +new Date
+ _preview_ended = false
+ if(first_open){
+ snd.musicGain.fadeOut(0.4)
+ }
+ var songObj
+ assets.songs.forEach(song => {
+ if(song.id == id){
+ songObj = song
+ }
+ })
+ if(songObj.sound){
+ _preview = songObj.sound
+ _preview.gain = snd.previewGain
+ this.previewLoaded(prvtime)
+ }else{
+ snd.previewGain.load("/songs/" + id + "/main.mp3").then(sound => {
+ if(!_preview_ended){
+ songObj.sound = sound
+ _preview = sound
+ this.previewLoaded(prvtime)
+ }
+ })
}
};
-
+
+ this.previewLoaded = function(prvtime){
+ var endLoad = +new Date
+ var delay = Math.max(1000 - Math.min(1000, endLoad - _preview_startLoad), 300)
+ _preview.playLoop(delay / 1000, false, prvtime / 1000)
+ }
+
this.endPreview = function() {
- clearTimeout(_preview_to);
+ _preview_ended = true
if (_preview) {
- _preview.pause();
+ _preview.stop();
};
};
-
- this.run = function(){
-
+
+ this.run = function(){
_this.createCode();
- _this.display();
- $(window).resize(_this.display);
- var menuLoop = setInterval(_this.refresh, 20);
$("#song-container").show();
-
+
$('#songsel-help').click(function(){
- bgm.pause();
+ assets.sounds["bgm_songsel"].stop()
+ assets.sounds["song-select"].stop()
+ assets.sounds["diffsel"].stop()
+ assets.sounds["don"].play()
+ snd.musicGain.fadeIn()
_this.endPreview();
- assets.sounds['don'].playAsset();
-
+
new Tutorial();
});
$(".difficulty").click(function(e){
_this.endPreview();
- assets.sounds["diffsel"].pause();
- assets.sounds["diffsel"].currentTime = 0;
- assets.sounds["don"].playAsset();
-
- clearInterval(menuLoop);
+ assets.sounds["bgm_songsel"].stop()
+ assets.sounds["diffsel"].stop()
+ assets.sounds["don"].play()
+
var difficultyElement = (e.target.className=="stars" || e.target.className=="diffname") ? e.target.parentElement : e.target;
_selectedSong.difficulty = difficultyElement.classList[1]+'.osu';
var parentID = $(this).parent().closest(".song").attr("id");
var songID = parseInt(parentID.substr(5, parentID.length-1));
_selectedSong.title = $(this).parent().closest('.song').data('title');
_selectedSong.folder = songID;
-
- bgm.pause();
+
+ snd.musicGain.fadeIn()
new loadSong(_selectedSong, e.shiftKey);
});
@@ -93,46 +99,38 @@ function SongSelect(){
if (!$(e.target).parents('.difficulties').length) {
if ($(".opened").length && $(".opened").attr('id') == $(this).attr('id')) {
_this.endPreview();
- bgm.play();
- assets.sounds["cancel"].playAsset();
+ snd.musicGain.fadeIn(0.4)
+ assets.sounds["diffsel"].stop()
+ assets.sounds["cancel"].play()
+ assets.sounds["song-select"].play(0.3)
+
$(".difficulty").hide();
$(".opened").removeClass("opened", 300);
-
- assets.sounds["diffsel"].pause();
- assets.sounds["diffsel"].currentTime = 0;
- setTimeout(function(){
- assets.sounds["song-select"].playAsset();
- }, 300);
-
+
$('.songsel-title').fadeOut(200, function(){
$('.songsel-title').attr('alt', '曲をえらぶ').html('曲をえらぶ').css('left', -300);
$('.songsel-title').animate({left:0, opacity:"show"}, 400);
});
-
+
return;
}
-
-
+
if(!$('.opened').length) {
_this.startPreview($(this).data('song-id'), $(this).data('preview'));
- assets.sounds["don"].playAsset();
- assets.sounds["song-select"].pause();
- assets.sounds["song-select"].currentTime = 0;
- setTimeout(function(){
- assets.sounds["diffsel"].playAsset();
- }, 300);
-
+ assets.sounds["don"].play()
+ assets.sounds["song-select"].stop()
+ assets.sounds["diffsel"].play(0.3)
+
$('.songsel-title').fadeOut(200, function(){
$('.songsel-title').attr('alt', 'むずかしさをえらぶ').html('むずかしさをえらぶ').css('left', -300);
$('.songsel-title').animate({left:0, opacity:"show"}, 400);
});
} else {
- _preview.pause();
_this.startPreview($(this).data('song-id'), $(this).data('preview'), false);
- assets.sounds["ka"].playAsset();
+ assets.sounds["ka"].play();
}
};
-
+
$(".difficulty").hide();
$(".opened").removeClass("opened", 300);
$(this).addClass("opened", 300, "linear", function(){
@@ -140,19 +138,12 @@ function SongSelect(){
$(this).css("background", "rgba(255, 220, 47, 0.90)");
});
});
-
+
}
-
+
this.createCode = function(){
- bgm = new BufferedLoop(
- {url: '/assets/audio/bgm_songsel.ogg', duration: 1.442},
- {url: '/assets/audio/bgm_songsel_loop.ogg', duration: 2.064}
- );
- bgm.play();
-
- setTimeout(function(){
- assets.sounds["song-select"].playAsset();
- }, 200);
+ assets.sounds["bgm_songsel"].playLoop(0, false, 0, 1.4423958333333333)
+ assets.sounds["song-select"].play(0.2);
var songElements = [0]
@@ -243,14 +234,7 @@ function SongSelect(){
)
$('.difficulty').hide();
}
-
- this.display = function(){
- }
-
- this.refresh = function(){
-
- }
$("#screen").load("/src/views/songselect.html", _this.run);
-
+
}
\ No newline at end of file
diff --git a/public/src/js/soundbuffer.js b/public/src/js/soundbuffer.js
new file mode 100644
index 0000000..cd4279c
--- /dev/null
+++ b/public/src/js/soundbuffer.js
@@ -0,0 +1,193 @@
+class SoundBuffer{
+ constructor(){
+ this.context = new AudioContext()
+ var resume = () => {
+ if(this.context.state == "suspended"){
+ this.context.resume()
+ }
+ removeEventListener("click", resume)
+ }
+ addEventListener("click", resume)
+ }
+ load(url, gain){
+ return new Promise((resolve, reject) => {
+ var request = new XMLHttpRequest()
+ request.open("GET", url)
+ request.responseType = "arraybuffer"
+ request.addEventListener("load", () => {
+ this.context.decodeAudioData(request.response, buffer => {
+ resolve(new Sound(gain || {soundBuffer: this}, buffer))
+ }, reject)
+ })
+ request.addEventListener("error", reject)
+ request.addEventListener("abort", reject)
+ request.send()
+ })
+ }
+ createGain(){
+ return new SoundGain(this)
+ }
+ setCrossfade(gain1, gain2, median){
+ gain1.setCrossfade(1 - median)
+ gain2.setCrossfade(median)
+ }
+ getTime(){
+ return this.context.currentTime
+ }
+ convertTime(time, absolute){
+ time = (time || 0)
+ if(time < 0){
+ time = 0
+ }
+ return time + (absolute ? 0 : this.getTime())
+ }
+ createSource(sound){
+ var source = this.context.createBufferSource()
+ source.buffer = sound.buffer
+ source.connect(sound.gain.gainNode || this.context.destination)
+ return source
+ }
+}
+class SoundGain{
+ constructor(soundBuffer){
+ this.soundBuffer = soundBuffer
+ this.gainNode = soundBuffer.context.createGain()
+ this.gainNode.connect(soundBuffer.context.destination)
+ this.setVolume(1)
+ }
+ load(url){
+ return this.soundBuffer.load(url, this)
+ }
+ convertTime(time, absolute){
+ return this.soundBuffer.convertTime(time, absolute)
+ }
+ setVolume(amount){
+ this.gainNode.gain.value = amount * amount
+ this.volume = amount
+ }
+ setCrossfade(amount){
+ this.setVolume(Math.pow(Math.sin(Math.PI / 2 * amount), 1 / 4))
+ }
+ fadeIn(duration, time, absolute){
+ this.fadeVolume(0, this.volume * this.volume, duration, time, absolute)
+ }
+ fadeOut(duration, time, absolute){
+ this.fadeVolume(this.volume * this.volume, 0, duration, time, absolute)
+ }
+ fadeVolume(vol1, vol2, duration, time, absolute){
+ time = this.convertTime(time, absolute)
+ this.gainNode.gain.linearRampToValueAtTime(vol1, time)
+ this.gainNode.gain.linearRampToValueAtTime(vol2, time + (duration || 0))
+ }
+ mute(){
+ this.gainNode.gain.value = 0
+ }
+ unmute(){
+ this.setVolume(this.volume)
+ }
+}
+class Sound{
+ constructor(gain, buffer){
+ this.gain = gain
+ this.buffer = buffer
+ this.soundBuffer = gain.soundBuffer
+ this.duration = buffer.duration
+ this.timeouts = new Set()
+ this.sources = new Set()
+ }
+ getTime(){
+ return this.soundBuffer.getTime()
+ }
+ convertTime(time, absolute){
+ return this.soundBuffer.convertTime(time, absolute)
+ }
+ setTimeouts(time){
+ return new Promise(resolve => {
+ var relTime = time - this.getTime()
+ if(relTime > 0){
+ var timeout = setTimeout(() => {
+ this.timeouts.delete(timeout)
+ resolve()
+ }, relTime * 1000)
+ this.timeouts.add(timeout)
+ }else{
+ resolve()
+ }
+ })
+ }
+ clearTimeouts(){
+ this.timeouts.forEach(timeout => {
+ clearTimeout(timeout)
+ this.timeouts.delete(timeout)
+ })
+ }
+ playLoop(time, absolute, seek1, seek2, until){
+ time = this.convertTime(time, absolute)
+ seek1 = seek1 || 0
+ seek2 = seek2 || seek1
+ until = until || this.duration
+ this.loop = {
+ started: time + until - seek1,
+ seek: seek2,
+ until: until
+ }
+ this.play(time, true, seek1, until)
+ this.addLoop()
+ this.loop.interval = setInterval(() => {
+ this.addLoop()
+ }, 100)
+ }
+ addLoop(){
+ if(this.getTime() > this.loop.started - 1){
+ this.play(this.loop.started, true, this.loop.seek, this.loop.until)
+ this.loop.started += this.loop.until - this.loop.seek
+ }
+ }
+ play(time, absolute, seek, until){
+ time = this.convertTime(time, absolute)
+ var source = this.soundBuffer.createSource(this)
+ seek = seek || 0
+ until = until || this.duration
+ this.setTimeouts(time).then(() => {
+ this.cfg = {
+ started: time,
+ seek: seek,
+ until: until
+ }
+ })
+ source.start(time, Math.max(0, seek || 0), Math.max(0, until - seek))
+ source.startTime = time
+ this.sources.add(source)
+ source.onended = () => {
+ this.sources.delete(source)
+ }
+ }
+ stop(time, absolute){
+ time = this.convertTime(time, absolute)
+ this.sources.forEach(source => {
+ source.stop(Math.max(source.startTime, time))
+ })
+ this.setTimeouts(time).then(() => {
+ if(this.loop){
+ clearInterval(this.loop.interval)
+ }
+ this.clearTimeouts()
+ })
+ }
+ pause(time, absolute){
+ if(this.cfg){
+ time = this.convertTime(time, absolute)
+ this.stop(time, true)
+ this.cfg.pauseSeek = time - this.cfg.started + this.cfg.seek
+ }
+ }
+ resume(time, absolute){
+ if(this.cfg){
+ if(this.loop){
+ this.playLoop(time, absolute, this.cfg.pauseSeek, this.loop.seek, this.loop.until)
+ }else{
+ this.play(time, absolute, this.cfg.pauseSeek, this.cfg.until)
+ }
+ }
+ }
+}
diff --git a/public/src/js/soundsystem.js b/public/src/js/soundsystem.js
deleted file mode 100644
index bf7e097..0000000
--- a/public/src/js/soundsystem.js
+++ /dev/null
@@ -1,48 +0,0 @@
-function soundSystem(controller){
-
- var _this = this;
- var _speed=0;
- var _circles = [];
- var _circleID = -1;
- var _measures = [];
- var _sounds = assets.sounds;
- var _channels=[];
- var _channelMAX=20;
-
- for (var i=0;i<_channelMAX;i++) {// prepare the channels
- _channels[i] = {};
- _channels[i]["end"] = -1;
- _channels[i]["audio"] = new Audio();
- }
-
- this.playSound = function(soundID){
-
- for(var i=0;i<_channelMAX;i++){ //play in different sounds in different channels
- var now = new Date();
- if (_channels[i]["end"] < now.getTime()) {// is this channel finished?
- _channels[i]["end"] = now.getTime() + _sounds[soundID].duration*1000;
- _channels[i]["audio"].src = _sounds[soundID].src;
- _channels[i]["audio"].load();
- _channels[i]["audio"].play();
- break;
- }
- }
-
- }
-
- this.pauseSound = function(){
- _sounds["main-music"].pause();
- }
-
- this.fadeOutMusic = function(){
-
- if(_sounds["main-music"].volume.toFixed(1)!=0.0){
- _sounds["main-music"].volume-=0.1;
- }
- else{
- _sounds["main-music"].pause();
- controller.fadeOutOver();
- }
- }
-
-}
\ No newline at end of file
diff --git a/public/src/js/titlescreen.js b/public/src/js/titlescreen.js
index 64b3163..c1eb80e 100644
--- a/public/src/js/titlescreen.js
+++ b/public/src/js/titlescreen.js
@@ -32,7 +32,7 @@ function Titlescreen(){
$("#screen").find("#title-screen").show();
$(window).resize(_this.positionning);
- assets.sounds["title"].playAsset();
+ assets.sounds["title"].play();
}
@@ -40,7 +40,7 @@ function Titlescreen(){
assets.sounds["title"].pause();
assets.sounds["title"].currentTime = 0;
- assets.sounds["don"].playAsset();
+ assets.sounds["don"].play();
if (localStorage.getItem('tutorial') !== 'true') {
new Tutorial();
} else {
diff --git a/public/src/js/tutorial.js b/public/src/js/tutorial.js
index 7c8cce2..9b9ee2e 100644
--- a/public/src/js/tutorial.js
+++ b/public/src/js/tutorial.js
@@ -2,15 +2,11 @@ function Tutorial() {
var _this = this;
this.run = function() {
- bgm = new BufferedLoop(
- {url: '/assets/audio/bgm_setsume.ogg', duration: 1.054},
- {url: '/assets/audio/bgm_setsume_loop.ogg', duration: 15}
- );
- bgm.play();
+ assets.sounds["bgm_setsume"].playLoop(0, false, 0, 1.0540416666666668)
$('#tutorial-end-button').click(function(){
- bgm.pause();
- assets.sounds['don'].playAsset();
+ assets.sounds["bgm_setsume"].stop();
+ assets.sounds["don"].play();
localStorage.setItem('tutorial', 'true');
new SongSelect();
diff --git a/public/src/js/view.js b/public/src/js/view.js
index 5067744..e603d35 100644
--- a/public/src/js/view.js
+++ b/public/src/js/view.js
@@ -4,10 +4,9 @@ class View{
this.bg = bg
this.diff = diff
- this.winW = $(window).width()
- this.winH = $(window).height()
-
- this.canvas = new ScalableCanvas("canvas", this.winW, this.winH)
+ this.canvas = new ScalableCanvas("canvas", $(window).width(), $(window).height())
+ this.winW = this.canvas.scaledWidth
+ this.winH = this.canvas.scaledHeight
this.ctx = this.canvas.ctx
this.taikoSquareW = this.winW / 4
");
+ var img = document.createElement("img")
+ promises.push(promiseLoad(img))
+ img.id = "music-bg"
+ img.src = "/songs/" + id + "/bg.png"
+ document.getElementById("assets").appendChild(img)
- var audio = new Audio();
- audio.muted = true;
- audio.src = '/songs/'+_selectedSong.folder+'/main.mp3';
- audio.load();
-
- $("#music-bg").load(function(){
- _bgLoaded=true;
- _this.checkIfEverythingLoaded();
- });
-
- audio.playAsset = function(){
- try{
- audio.muted = false;
- audio.play()
- }catch(e){
- console.warn(e)
+ promises.push(new Promise((resolve, reject) => {
+ var songObj
+ assets.songs.forEach(song => {
+ if(song.id == id){
+ songObj = song
+ }
+ })
+ if(songObj.sound){
+ songObj.sound.gain = snd.musicGain
+ resolve()
+ }else{
+ snd.musicGain.load("/songs/" + id + "/main.mp3").then(sound => {
+ songObj.sound = sound
+ resolve()
+ }, reject)
}
- }
+ }))
- audio.onloadeddata = function(){
- _musicLoaded=true;
- assets.sounds["main-music"]=audio;
- _this.checkIfEverythingLoaded();
- };
-
- $.ajax({
- url : _songFilePath,
- dataType: "text",
- success : function (data) {
- _songData = data.split("\n");
- _songDataLoaded=true;
- _this.checkIfEverythingLoaded();
- }
- });
+ promises.push(ajax(this.songFilePath).then(data => {
+ this.songData = data.replace(/\0/g, "").split("\n")
+ }))
+ Promise.all(promises).then(() => {
+ $("#screen").load("/src/views/game.html", () => {
+ var taikoGame = new Controller(this.selectedSong, this.songData, this.autoPlayEnabled)
+ taikoGame.run()
+ })
+ }, () => {
+ alert("An error occurred, please refresh")
+ })
}
-
- this.checkIfEverythingLoaded = function(){
- if(_musicLoaded && _songDataLoaded && _bgLoaded){
- $("#screen").load("/src/views/game.html", function(){
- var taikoGame = new Controller(_selectedSong, _songData, autoPlayEnabled);
- taikoGame.run();
- });
- }
- }
-
- $("#screen").load("/src/views/loadsong.html", _this.run);
}
\ No newline at end of file
diff --git a/public/src/js/main.js b/public/src/js/main.js
deleted file mode 100644
index c305166..0000000
--- a/public/src/js/main.js
+++ /dev/null
@@ -1,7 +0,0 @@
-$(document).ready(function(){
-
- new Loader();
-
-});
-
-
diff --git a/public/src/js/scoresheet.js b/public/src/js/scoresheet.js
index d5a9eb7..7280ada 100644
--- a/public/src/js/scoresheet.js
+++ b/public/src/js/scoresheet.js
@@ -1,18 +1,18 @@
function Scoresheet(controller, score){
-
- var _this = this;
- var _score = score;
- var _mark;
+
+ var _this = this;
+ var _score = score;
+ var _mark;
this.setResults = function(){
if (_score.fail == 0) {
- _mark = 'gold';
- } else if (_score.hp >= 50) {
- _mark = 'silver';
- };
-
+ _mark = 'gold';
+ } else if (_score.hp >= 50) {
+ _mark = 'silver';
+ };
+
var imgW = (_score.hp*$("#score-hp-bar-colour").width())/100;
$("#score-hp-bar-colour img").css("clip", "rect(0, "+imgW+"px, "+$("#score-hp-bar-colour").height()+"px, 0)");
@@ -23,13 +23,13 @@ function Scoresheet(controller, score){
} else {
$("#score-mark").remove();
};
-
+
$("#score-points").html(_score.points+"点");
$("#nb-great").html(_score.great);
$("#nb-good").html(_score.good);
$("#nb-fail").html(_score.fail);
$("#max-combo").html(_score.maxCombo);
-
+
$('.result-song').attr('alt', _score.song).html(_score.song);
}
@@ -76,36 +76,29 @@ function Scoresheet(controller, score){
}
- this.run = function(){
+ this.run = function(){
_this.positionning();
_this.setResults();
- $("#song-select").click(function(){
- assets.sounds["don"].playAsset();
- bgm.pause();
- controller.songSelection();
- });
-
- $("#replay").click(function(){
- assets.sounds["don"].playAsset();
- bgm.pause();
- controller.restartSong();
- });
+ $("#song-select").click(function(){
+ assets.sounds["don"].play();
+ assets.sounds["bgm_result"].stop();
+ controller.songSelection();
+ });
+
+ $("#replay").click(function(){
+ assets.sounds["don"].play();
+ assets.sounds["bgm_result"].stop();
+ controller.restartSong();
+ });
$(window).resize(_this.positionning);
- }
-
- assets.sounds["results"].playAsset();
+ }
+
+ assets.sounds["results"].play()
+ assets.sounds["bgm_result"].playLoop(0, false, 0, 0.846625)
- bgm = new BufferedLoop(
- {url: '/assets/audio/bgm_result.ogg', duration: 0.847},
- {url: '/assets/audio/bgm_result_loop.ogg', duration: 16.842}
- );
- bgm.play();
-
-
-
$("#screen").load("/src/views/scoresheet.html", _this.run);
}
\ No newline at end of file
diff --git a/public/src/js/songselect.js b/public/src/js/songselect.js
index b741f2b..7ab7bdd 100644
--- a/public/src/js/songselect.js
+++ b/public/src/js/songselect.js
@@ -1,82 +1,88 @@
function SongSelect(){
-
- var _this=this;
- var _songs;
+ var _this=this;
+ var _songs;
var _selectedSong = {title:'', folder:'', difficulty:''};
var _preview;
- var _preview_to;
+ var _preview_ended
+ var _preview_startLoad
var _diffNames={
easy:"かんたん",
normal:"ふつう",
hard:"むずかしい",
oni:"おに"
}
-
+
this.startPreview = function(id, prvtime, first_open=true) {
- var start = Date.now();
- setTimeout(function(){
- bgm.pause();
- }, 400);
-
- _preview = new Audio('/songs/' + id + '/main.mp3');
- _preview.onloadeddata = function() {
- var end = Date.now();
- var delay = end - start;
- var no_delay = first_open ? 0 : 300;
-
- _preview.currentTime = prvtime/1000;
- _preview.volume = 0.5;
-
- _preview.addEventListener('ended', function(){
- this.currentTime = prvtime/1000;
- this.play();
- }, false);
-
- _preview_to = setTimeout(function(){
- _preview.play();
- }, delay <= 1000 && first_open ? 1000 : no_delay);
+ _this.endPreview();
+ _preview_startLoad = +new Date
+ _preview_ended = false
+ if(first_open){
+ snd.musicGain.fadeOut(0.4)
+ }
+ var songObj
+ assets.songs.forEach(song => {
+ if(song.id == id){
+ songObj = song
+ }
+ })
+ if(songObj.sound){
+ _preview = songObj.sound
+ _preview.gain = snd.previewGain
+ this.previewLoaded(prvtime)
+ }else{
+ snd.previewGain.load("/songs/" + id + "/main.mp3").then(sound => {
+ if(!_preview_ended){
+ songObj.sound = sound
+ _preview = sound
+ this.previewLoaded(prvtime)
+ }
+ })
}
};
-
+
+ this.previewLoaded = function(prvtime){
+ var endLoad = +new Date
+ var delay = Math.max(1000 - Math.min(1000, endLoad - _preview_startLoad), 300)
+ _preview.playLoop(delay / 1000, false, prvtime / 1000)
+ }
+
this.endPreview = function() {
- clearTimeout(_preview_to);
+ _preview_ended = true
if (_preview) {
- _preview.pause();
+ _preview.stop();
};
};
-
- this.run = function(){
-
+
+ this.run = function(){
_this.createCode();
- _this.display();
- $(window).resize(_this.display);
- var menuLoop = setInterval(_this.refresh, 20);
$("#song-container").show();
-
+
$('#songsel-help').click(function(){
- bgm.pause();
+ assets.sounds["bgm_songsel"].stop()
+ assets.sounds["song-select"].stop()
+ assets.sounds["diffsel"].stop()
+ assets.sounds["don"].play()
+ snd.musicGain.fadeIn()
_this.endPreview();
- assets.sounds['don'].playAsset();
-
+
new Tutorial();
});
$(".difficulty").click(function(e){
_this.endPreview();
- assets.sounds["diffsel"].pause();
- assets.sounds["diffsel"].currentTime = 0;
- assets.sounds["don"].playAsset();
-
- clearInterval(menuLoop);
+ assets.sounds["bgm_songsel"].stop()
+ assets.sounds["diffsel"].stop()
+ assets.sounds["don"].play()
+
var difficultyElement = (e.target.className=="stars" || e.target.className=="diffname") ? e.target.parentElement : e.target;
_selectedSong.difficulty = difficultyElement.classList[1]+'.osu';
var parentID = $(this).parent().closest(".song").attr("id");
var songID = parseInt(parentID.substr(5, parentID.length-1));
_selectedSong.title = $(this).parent().closest('.song').data('title');
_selectedSong.folder = songID;
-
- bgm.pause();
+
+ snd.musicGain.fadeIn()
new loadSong(_selectedSong, e.shiftKey);
});
@@ -93,46 +99,38 @@ function SongSelect(){
if (!$(e.target).parents('.difficulties').length) {
if ($(".opened").length && $(".opened").attr('id') == $(this).attr('id')) {
_this.endPreview();
- bgm.play();
- assets.sounds["cancel"].playAsset();
+ snd.musicGain.fadeIn(0.4)
+ assets.sounds["diffsel"].stop()
+ assets.sounds["cancel"].play()
+ assets.sounds["song-select"].play(0.3)
+
$(".difficulty").hide();
$(".opened").removeClass("opened", 300);
-
- assets.sounds["diffsel"].pause();
- assets.sounds["diffsel"].currentTime = 0;
- setTimeout(function(){
- assets.sounds["song-select"].playAsset();
- }, 300);
-
+
$('.songsel-title').fadeOut(200, function(){
$('.songsel-title').attr('alt', '曲をえらぶ').html('曲をえらぶ').css('left', -300);
$('.songsel-title').animate({left:0, opacity:"show"}, 400);
});
-
+
return;
}
-
-
+
if(!$('.opened').length) {
_this.startPreview($(this).data('song-id'), $(this).data('preview'));
- assets.sounds["don"].playAsset();
- assets.sounds["song-select"].pause();
- assets.sounds["song-select"].currentTime = 0;
- setTimeout(function(){
- assets.sounds["diffsel"].playAsset();
- }, 300);
-
+ assets.sounds["don"].play()
+ assets.sounds["song-select"].stop()
+ assets.sounds["diffsel"].play(0.3)
+
$('.songsel-title').fadeOut(200, function(){
$('.songsel-title').attr('alt', 'むずかしさをえらぶ').html('むずかしさをえらぶ').css('left', -300);
$('.songsel-title').animate({left:0, opacity:"show"}, 400);
});
} else {
- _preview.pause();
_this.startPreview($(this).data('song-id'), $(this).data('preview'), false);
- assets.sounds["ka"].playAsset();
+ assets.sounds["ka"].play();
}
};
-
+
$(".difficulty").hide();
$(".opened").removeClass("opened", 300);
$(this).addClass("opened", 300, "linear", function(){
@@ -140,19 +138,12 @@ function SongSelect(){
$(this).css("background", "rgba(255, 220, 47, 0.90)");
});
});
-
+
}
-
+
this.createCode = function(){
- bgm = new BufferedLoop(
- {url: '/assets/audio/bgm_songsel.ogg', duration: 1.442},
- {url: '/assets/audio/bgm_songsel_loop.ogg', duration: 2.064}
- );
- bgm.play();
-
- setTimeout(function(){
- assets.sounds["song-select"].playAsset();
- }, 200);
+ assets.sounds["bgm_songsel"].playLoop(0, false, 0, 1.4423958333333333)
+ assets.sounds["song-select"].play(0.2);
var songElements = [0]
@@ -243,14 +234,7 @@ function SongSelect(){
)
$('.difficulty').hide();
}
-
- this.display = function(){
- }
-
- this.refresh = function(){
-
- }
$("#screen").load("/src/views/songselect.html", _this.run);
-
+
}
\ No newline at end of file
diff --git a/public/src/js/soundbuffer.js b/public/src/js/soundbuffer.js
new file mode 100644
index 0000000..cd4279c
--- /dev/null
+++ b/public/src/js/soundbuffer.js
@@ -0,0 +1,193 @@
+class SoundBuffer{
+ constructor(){
+ this.context = new AudioContext()
+ var resume = () => {
+ if(this.context.state == "suspended"){
+ this.context.resume()
+ }
+ removeEventListener("click", resume)
+ }
+ addEventListener("click", resume)
+ }
+ load(url, gain){
+ return new Promise((resolve, reject) => {
+ var request = new XMLHttpRequest()
+ request.open("GET", url)
+ request.responseType = "arraybuffer"
+ request.addEventListener("load", () => {
+ this.context.decodeAudioData(request.response, buffer => {
+ resolve(new Sound(gain || {soundBuffer: this}, buffer))
+ }, reject)
+ })
+ request.addEventListener("error", reject)
+ request.addEventListener("abort", reject)
+ request.send()
+ })
+ }
+ createGain(){
+ return new SoundGain(this)
+ }
+ setCrossfade(gain1, gain2, median){
+ gain1.setCrossfade(1 - median)
+ gain2.setCrossfade(median)
+ }
+ getTime(){
+ return this.context.currentTime
+ }
+ convertTime(time, absolute){
+ time = (time || 0)
+ if(time < 0){
+ time = 0
+ }
+ return time + (absolute ? 0 : this.getTime())
+ }
+ createSource(sound){
+ var source = this.context.createBufferSource()
+ source.buffer = sound.buffer
+ source.connect(sound.gain.gainNode || this.context.destination)
+ return source
+ }
+}
+class SoundGain{
+ constructor(soundBuffer){
+ this.soundBuffer = soundBuffer
+ this.gainNode = soundBuffer.context.createGain()
+ this.gainNode.connect(soundBuffer.context.destination)
+ this.setVolume(1)
+ }
+ load(url){
+ return this.soundBuffer.load(url, this)
+ }
+ convertTime(time, absolute){
+ return this.soundBuffer.convertTime(time, absolute)
+ }
+ setVolume(amount){
+ this.gainNode.gain.value = amount * amount
+ this.volume = amount
+ }
+ setCrossfade(amount){
+ this.setVolume(Math.pow(Math.sin(Math.PI / 2 * amount), 1 / 4))
+ }
+ fadeIn(duration, time, absolute){
+ this.fadeVolume(0, this.volume * this.volume, duration, time, absolute)
+ }
+ fadeOut(duration, time, absolute){
+ this.fadeVolume(this.volume * this.volume, 0, duration, time, absolute)
+ }
+ fadeVolume(vol1, vol2, duration, time, absolute){
+ time = this.convertTime(time, absolute)
+ this.gainNode.gain.linearRampToValueAtTime(vol1, time)
+ this.gainNode.gain.linearRampToValueAtTime(vol2, time + (duration || 0))
+ }
+ mute(){
+ this.gainNode.gain.value = 0
+ }
+ unmute(){
+ this.setVolume(this.volume)
+ }
+}
+class Sound{
+ constructor(gain, buffer){
+ this.gain = gain
+ this.buffer = buffer
+ this.soundBuffer = gain.soundBuffer
+ this.duration = buffer.duration
+ this.timeouts = new Set()
+ this.sources = new Set()
+ }
+ getTime(){
+ return this.soundBuffer.getTime()
+ }
+ convertTime(time, absolute){
+ return this.soundBuffer.convertTime(time, absolute)
+ }
+ setTimeouts(time){
+ return new Promise(resolve => {
+ var relTime = time - this.getTime()
+ if(relTime > 0){
+ var timeout = setTimeout(() => {
+ this.timeouts.delete(timeout)
+ resolve()
+ }, relTime * 1000)
+ this.timeouts.add(timeout)
+ }else{
+ resolve()
+ }
+ })
+ }
+ clearTimeouts(){
+ this.timeouts.forEach(timeout => {
+ clearTimeout(timeout)
+ this.timeouts.delete(timeout)
+ })
+ }
+ playLoop(time, absolute, seek1, seek2, until){
+ time = this.convertTime(time, absolute)
+ seek1 = seek1 || 0
+ seek2 = seek2 || seek1
+ until = until || this.duration
+ this.loop = {
+ started: time + until - seek1,
+ seek: seek2,
+ until: until
+ }
+ this.play(time, true, seek1, until)
+ this.addLoop()
+ this.loop.interval = setInterval(() => {
+ this.addLoop()
+ }, 100)
+ }
+ addLoop(){
+ if(this.getTime() > this.loop.started - 1){
+ this.play(this.loop.started, true, this.loop.seek, this.loop.until)
+ this.loop.started += this.loop.until - this.loop.seek
+ }
+ }
+ play(time, absolute, seek, until){
+ time = this.convertTime(time, absolute)
+ var source = this.soundBuffer.createSource(this)
+ seek = seek || 0
+ until = until || this.duration
+ this.setTimeouts(time).then(() => {
+ this.cfg = {
+ started: time,
+ seek: seek,
+ until: until
+ }
+ })
+ source.start(time, Math.max(0, seek || 0), Math.max(0, until - seek))
+ source.startTime = time
+ this.sources.add(source)
+ source.onended = () => {
+ this.sources.delete(source)
+ }
+ }
+ stop(time, absolute){
+ time = this.convertTime(time, absolute)
+ this.sources.forEach(source => {
+ source.stop(Math.max(source.startTime, time))
+ })
+ this.setTimeouts(time).then(() => {
+ if(this.loop){
+ clearInterval(this.loop.interval)
+ }
+ this.clearTimeouts()
+ })
+ }
+ pause(time, absolute){
+ if(this.cfg){
+ time = this.convertTime(time, absolute)
+ this.stop(time, true)
+ this.cfg.pauseSeek = time - this.cfg.started + this.cfg.seek
+ }
+ }
+ resume(time, absolute){
+ if(this.cfg){
+ if(this.loop){
+ this.playLoop(time, absolute, this.cfg.pauseSeek, this.loop.seek, this.loop.until)
+ }else{
+ this.play(time, absolute, this.cfg.pauseSeek, this.cfg.until)
+ }
+ }
+ }
+}
diff --git a/public/src/js/soundsystem.js b/public/src/js/soundsystem.js
deleted file mode 100644
index bf7e097..0000000
--- a/public/src/js/soundsystem.js
+++ /dev/null
@@ -1,48 +0,0 @@
-function soundSystem(controller){
-
- var _this = this;
- var _speed=0;
- var _circles = [];
- var _circleID = -1;
- var _measures = [];
- var _sounds = assets.sounds;
- var _channels=[];
- var _channelMAX=20;
-
- for (var i=0;i<_channelMAX;i++) {// prepare the channels
- _channels[i] = {};
- _channels[i]["end"] = -1;
- _channels[i]["audio"] = new Audio();
- }
-
- this.playSound = function(soundID){
-
- for(var i=0;i<_channelMAX;i++){ //play in different sounds in different channels
- var now = new Date();
- if (_channels[i]["end"] < now.getTime()) {// is this channel finished?
- _channels[i]["end"] = now.getTime() + _sounds[soundID].duration*1000;
- _channels[i]["audio"].src = _sounds[soundID].src;
- _channels[i]["audio"].load();
- _channels[i]["audio"].play();
- break;
- }
- }
-
- }
-
- this.pauseSound = function(){
- _sounds["main-music"].pause();
- }
-
- this.fadeOutMusic = function(){
-
- if(_sounds["main-music"].volume.toFixed(1)!=0.0){
- _sounds["main-music"].volume-=0.1;
- }
- else{
- _sounds["main-music"].pause();
- controller.fadeOutOver();
- }
- }
-
-}
\ No newline at end of file
diff --git a/public/src/js/titlescreen.js b/public/src/js/titlescreen.js
index 64b3163..c1eb80e 100644
--- a/public/src/js/titlescreen.js
+++ b/public/src/js/titlescreen.js
@@ -32,7 +32,7 @@ function Titlescreen(){
$("#screen").find("#title-screen").show();
$(window).resize(_this.positionning);
- assets.sounds["title"].playAsset();
+ assets.sounds["title"].play();
}
@@ -40,7 +40,7 @@ function Titlescreen(){
assets.sounds["title"].pause();
assets.sounds["title"].currentTime = 0;
- assets.sounds["don"].playAsset();
+ assets.sounds["don"].play();
if (localStorage.getItem('tutorial') !== 'true') {
new Tutorial();
} else {
diff --git a/public/src/js/tutorial.js b/public/src/js/tutorial.js
index 7c8cce2..9b9ee2e 100644
--- a/public/src/js/tutorial.js
+++ b/public/src/js/tutorial.js
@@ -2,15 +2,11 @@ function Tutorial() {
var _this = this;
this.run = function() {
- bgm = new BufferedLoop(
- {url: '/assets/audio/bgm_setsume.ogg', duration: 1.054},
- {url: '/assets/audio/bgm_setsume_loop.ogg', duration: 15}
- );
- bgm.play();
+ assets.sounds["bgm_setsume"].playLoop(0, false, 0, 1.0540416666666668)
$('#tutorial-end-button').click(function(){
- bgm.pause();
- assets.sounds['don'].playAsset();
+ assets.sounds["bgm_setsume"].stop();
+ assets.sounds["don"].play();
localStorage.setItem('tutorial', 'true');
new SongSelect();
diff --git a/public/src/js/view.js b/public/src/js/view.js
index 5067744..e603d35 100644
--- a/public/src/js/view.js
+++ b/public/src/js/view.js
@@ -4,10 +4,9 @@ class View{
this.bg = bg
this.diff = diff
- this.winW = $(window).width()
- this.winH = $(window).height()
-
- this.canvas = new ScalableCanvas("canvas", this.winW, this.winH)
+ this.canvas = new ScalableCanvas("canvas", $(window).width(), $(window).height())
+ this.winW = this.canvas.scaledWidth
+ this.winH = this.canvas.scaledHeight
this.ctx = this.canvas.ctx
this.taikoSquareW = this.winW / 4