From 7af998f0bc5df8df8230d89de83b6dec969e2135 Mon Sep 17 00:00:00 2001 From: 0x01FE Date: Thu, 24 Oct 2024 10:44:57 -0500 Subject: [PATCH] got the audio streams kind of working --- src/main.js | 54 ++++++++++++++++++++++++++++++++++++++++++------- src/renderer.js | 4 ++-- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/main.js b/src/main.js index 5f3028b..063c3a9 100644 --- a/src/main.js +++ b/src/main.js @@ -16,6 +16,7 @@ import NodeID3 from 'node-id3'; import fs from 'node:fs'; import ffmpeg from 'fluent-ffmpeg'; import Speaker from 'speaker'; +import Stream from 'node:stream'; let win; @@ -23,13 +24,12 @@ class MusicPlayer { constructor(path) { this.songPath = path; this.playing = false; - this.progress = 0; + this.progress = 0; // in seconds + this.duration = null; + this.audioStream = null; - this.speaker = new Speaker({ - channels: 2, - bitDepth: 16, - sampleRate: 44100 - }); + this.speaker = new Speaker(); + this.timer = null; } play(path) { @@ -37,7 +37,44 @@ class MusicPlayer { this.songPath = path; } - let stream = fs.create + if (this.audioStream != null) { + if (!this.audioStream.ended) { + this.audioStream.resume(); + return; + } + } + + this.audioStream = this.transcode(this.songPath); + + this.audioStream.pipe(this.speaker); + + this.timer = setInterval(() => { + this.progress += 0.5; + + if (this.progress >= this.duration) { + clearInterval(this.timer); + } + }, 500); + } + + // To pause close and don't? flush + pause() { + // this.speaker.close(false); + this.audioStream.pause(); + } + + transcode(input) { + const s = new Stream.PassThrough(); + + ffmpeg(input) + .on('codecData', (data) => { + this.duration = data.duration; + }) + .audioCodec('pcm_s16le') + .format('s16le') + .pipe(s); + + return s; } } @@ -55,6 +92,9 @@ const createWindow = () => { win.loadFile('src/index.html'); // enable(win.webContents); + + let player = new MusicPlayer('/home/jackson/Documents/code/SSPM/test-library/test.mp3'); + player.play(); }; app.whenReady().then(() => { diff --git a/src/renderer.js b/src/renderer.js index 720c3ec..06b6623 100644 --- a/src/renderer.js +++ b/src/renderer.js @@ -7,8 +7,8 @@ console.log('test'); // const url = 'file://C:\\Users\\JISAUAY\\Documents\\misc\\sspm\\test.ogg'; // const path = 'C:\\Users\\JISAUAY\\Documents\\misc\\sspm\\ChangelingForest.mp3'; -const libPath = 'C:/Users/JISAUAY/Documents/misc/sspm/test-library/'; -// const libPath = '/home/jackson/Documents/code/SSPM/test-library/' +// const libPath = 'C:/Users/JISAUAY/Documents/misc/sspm/test-library/'; +const libPath = '/home/jackson/Documents/code/SSPM/test-library/'; // Play Raw data to speaker // https://github.com/TooTallNate/node-speaker