birbsoundboard/src/views/Home.vue

157 lines
3.9 KiB
Vue
Raw Normal View History

2021-06-13 22:09:12 +02:00
<template>
2021-06-14 01:27:47 +02:00
<b-container fluid style="height: 100vh">
2021-06-14 01:55:43 +02:00
<b-row v-if="errorText || soundsLoaded < totalSounds">
<b>{{ errorText }}</b>
<br>
Loaded {{ soundsLoaded }} of {{ totalSounds }} sound files...
2021-06-14 01:27:47 +02:00
</b-row>
<b-row v-else class="mb-2 h-100">
<b-col v-for="(snd,id) in availableSounds" :key="id" class="my-2" :cols="columnWidth" align-v="stretch">
<b-button @click="play(id)" squared size="lg" class="bigBoyButton"
:variant="nowPlaying === id? 'success' : 'secondary'">
{{ snd.sampleName }}
</b-button>
</b-col>
</b-row>
</b-container>
2021-06-13 22:09:12 +02:00
</template>
<script>
2021-06-14 01:27:47 +02:00
import {Howl} from 'howler'
import axios from 'axios'
2021-06-14 23:58:40 +02:00
import _ from 'lodash'
2021-06-13 22:09:12 +02:00
export default {
name: 'Home',
2021-06-14 01:27:47 +02:00
data() {
return {
nowPlaying: -1,
availableSounds: [],
2021-06-14 01:55:43 +02:00
bgSound: null,
soundsLoaded: 0,
errorText: null,
2021-06-14 23:28:57 +02:00
columnWidth: 6,
2021-06-14 23:58:40 +02:00
sequence: [],
sequenceCleanerTimeout: null
2021-06-14 01:55:43 +02:00
}
},
computed: {
totalSounds() {
return this.availableSounds.length + 1 // because bg
2021-06-14 01:27:47 +02:00
}
},
methods: {
2021-06-14 23:58:40 +02:00
patternMagic(sampleId) {
this.sequence.push(sampleId)
if (this.sequence.length > 4) {
this.sequence.shift() // removes the first element from an array
}
2021-06-14 01:27:47 +02:00
2021-06-14 23:58:40 +02:00
if (_.isEqual(this.sequence, [0, 1, 3, 2])) {
2021-06-14 23:28:57 +02:00
const elem = document.documentElement
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.webkitRequestFullscreen) { /* Safari */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE11 */
elem.msRequestFullscreen();
}
2021-06-14 23:58:40 +02:00
this.sequence = []
}
if (_.isEqual(this.sequence, [0, 3, 3, 0])) {
location.reload()
this.sequence = []
}
if (_.isEqual(this.sequence, [0, 3, 1, 1])) {
this.bgSound.stop()
this.availableSounds[this.nowPlaying].howl.stop()
this.nowPlaying = -1
this.sequence = []
return true;
}
// Set timer to clear sequence
if (this.sequenceCleanerTimeout !== null) {
clearTimeout(this.sequenceCleanerTimeout)
}
this.sequenceCleanerTimeout = setTimeout(() => {
this.sequence = []
this.sequenceCleanerTimeout = null
2021-06-15 00:11:42 +02:00
},750)
2021-06-14 23:58:40 +02:00
return false;
},
play(sampleId) {
const prevent = this.patternMagic(sampleId)
if (prevent) {
return;
2021-06-14 23:28:57 +02:00
}
2021-06-14 01:27:47 +02:00
const sample = this.availableSounds[sampleId].howl
if (sample.playing()) {
sample.stop()
this.nowPlaying = -1
this.bgSound.play() // Continue background sound
} else {
this.bgSound.pause() // the background sound must be paused
2021-06-14 01:55:43 +02:00
if (this.nowPlaying !== -1) {
this.availableSounds[this.nowPlaying].howl.stop()
}
2021-06-14 01:27:47 +02:00
sample.play()
this.nowPlaying = sampleId
}
}
},
mounted() {
axios.get(process.env.VUE_APP_INDEX_URL).then((response) => {
this.columnWidth = response.data['columnWidth'] || 6
this.bgSound = new Howl({
src: response.data['bg'],
2021-06-14 01:55:43 +02:00
loop: true,
onload: () => this.soundsLoaded++,
preload: true,
onloaderror: (id, err) => this.errorText = id + err
2021-06-14 01:27:47 +02:00
})
response.data['availableSounds'].forEach((sndData) => {
this.availableSounds.push({
sampleName: sndData.sampleName,
howl: new Howl({
src: [sndData.url],
loop: false,
onend: () => {
this.nowPlaying = -1
this.bgSound.play() // Continue background sound
2021-06-14 01:55:43 +02:00
},
onload: () => this.soundsLoaded++,
preload: true,
onloaderror: (id, err) => this.errorText = `${id} ${err}`
2021-06-14 01:27:47 +02:00
})
})
})
2021-06-14 01:55:43 +02:00
}).catch((err) => this.errorText = err)
2021-06-13 22:09:12 +02:00
}
}
</script>
2021-06-14 01:27:47 +02:00
<style scoped>
.bigBoyButton {
width: 100%;
height: 100%;
2021-06-15 00:11:42 +02:00
font-size: ;
2021-06-14 01:27:47 +02:00
}
</style>