diff options
Diffstat (limited to 'native-app')
-rwxr-xr-x | native-app/index.js | 122 | ||||
-rw-r--r-- | native-app/manifest.json | 7 |
2 files changed, 129 insertions, 0 deletions
diff --git a/native-app/index.js b/native-app/index.js new file mode 100755 index 0000000..2f174ea --- /dev/null +++ b/native-app/index.js @@ -0,0 +1,122 @@ +#!/usr/bin/env node + +const basePath = '/home/florrie/Documents/interactive-bgm'; + +const logFile = basePath + '/native-app/log'; + +const log = msg => { + require('fs').appendFileSync(logFile, msg + '\n'); +}; + + +log('Started ' + Date()); + +const { spawn } = require('child_process'); +const EventEmitter = require('events'); +const FIFO = require('fifo-js'); +const http = require('http'); + +log('Loaded modules'); + +class TrackPlayer { + constructor(file) { + this.file = file; + this.volume = 0; + this.storedVolume = this.volume; + } + + loadProcess() { + this.fifo = new FIFO(); + this.process = spawn('mpv', [ + '--no-video', + '--loop', + '--volume=' + this.volume, + '--input-file=' + this.fifo.path, + this.file, + ]); + + const stream = new EventEmitter(); + stream.write = () => {}; + this.process.stderr.pipe(stream); + } + + sendCommand(command) { + if (this.fifo) { + this.fifo.write(command); + } + } + + seekToStart() { + this.sendCommand('seek 0 absolute'); + } + + pause() { + this.sendCommand('set pause yes'); + } + + play() { + this.sendCommand('set pause no'); + } + + setVolume(volume) { + if (Math.floor(volume) !== this.storedVolume) { + this.storedVolume = Math.floor(volume); + this.sendCommand(`set volume ${volume}`); + } + this.volume = volume; + } +} + +const tracks = { + mantis: new TrackPlayer(basePath + '/track1.wav'), + bass: new TrackPlayer(basePath + '/track2.wav'), + main: new TrackPlayer(basePath + '/track3.wav') +}; + +for (const track of Object.values(tracks)) { + track.loadProcess(); + track.pause(); +} + +setTimeout(() => { + for (const track of Object.values(tracks)) { + track.seekToStart(); + track.play(); + } + + let targetMode = [ + {track: 'main', volume: 100} + ]; + + setInterval(() => { + if (!Array.isArray(targetMode)) { + targetMode = []; + } + + for (const [key, track] of Object.entries(tracks)) { + const mode = targetMode.find(m => m.track === key) || {volume: 0}; + track.setVolume(track.volume + 0.1 * (mode.volume - track.volume)); + } + }, 100); + + process.stdin.on('data', data => { + log('STDIN: ' + data); + + const probablyJSON = data.toString().slice(data.indexOf('[')).trim(); + + try { + targetMode = JSON.parse(probablyJSON); + } catch (error) { + } + }); +}, 250); + +log('Go!'); + +process.on('SIGTERM', () => { + log('Exiting [sigterm]'); + for (const track of Object.values(tracks)) { + track.process.kill(); + } + log('Cleaned up'); +}); diff --git a/native-app/manifest.json b/native-app/manifest.json new file mode 100644 index 0000000..73f90dd --- /dev/null +++ b/native-app/manifest.json @@ -0,0 +1,7 @@ +{ + "name": "interactive_bgm", + "description": "Adds background music to your browsing.", + "path": "/home/florrie/Documents/interactive-bgm/native-app/index.js", + "type": "stdio", + "allowed_extensions": ["interactive_bgm@florrie.ed1.club"] +} |