Преглед на файлове

Final. Oh My Goodness. Do not ever forget this trip of fucking with LAME audio and stuff. Waveform FTW.

AXCWG преди 10 месеца
родител
ревизия
83f5cded9e
променени са 3 файла, в които са добавени 36 реда и са изтрити 64 реда
  1. 28 63
      main.js
  2. 7 0
      package-lock.json
  3. 1 1
      package.json

+ 28 - 63
main.js

@@ -41,6 +41,8 @@ const https = require("node:https");
 const {OpenCC} = require("opencc");
 const {HttpsProxyAgent} = require("https-proxy-agent");
 const sortArray = require("sort-array");
+const Kali = require("@descript/kali");
+const WavEncoder = require("wav-encoder");
 const converters2t = new OpenCC('s2t.json')
 const convertert2s = new OpenCC('t2s.json')
 app.use(bodyParser.json({"limit": "200mb"}));
@@ -522,20 +524,21 @@ let availCache = {};
 // }, 1800000)
 
 // Fetch
-app.get('/:uuid', function (req, res) {
+app.get('/:uuid', async function (req, res) {
 
     let uuid = req.params.uuid;
     let pitch = req.query.pitch;
 
-    async function Provider(err, rows) {
+    async function Provider() {
+
         try {
-            availCache[uuid] = rows[0]
-            res.contentType("audio/mp3");
-            res.header("Access-Control-Allow-Origin", "*");
-            res.header("Access-Control-Allow-Headers", "Content-Type");
-            res.header('Content-Disposition', `attachment; filename="${encodeURI(availCache[uuid].song_name)}.mp3"`);
+
             /** @type {ArrayBuffer}*/
             if(!pitch){
+                res.contentType("audio/mp3");
+                res.header("Access-Control-Allow-Origin", "*");
+                res.header("Access-Control-Allow-Headers", "Content-Type");
+                res.header('Content-Disposition', `attachment; filename="${encodeURI(availCache[uuid].song_name)}.mp3"`);
                 res.sendSeekable(availCache[uuid].databinary)
             }else{
                 let decodeAudio = await import('audio-decode')
@@ -550,42 +553,28 @@ app.get('/:uuid', function (req, res) {
                 kali.input(decoded.getChannelData(0))
 
                 kali.process()
-                let length = decoded.getChannelData(0).length
                 let output = new Float32Array(samplerate * decoded.duration)
-                let samplesRead = kali.output(output);
-
-                fs.writeFileSync('./test', output)
-
-                let lame = require('lamejs')
-                let mp3Encoder = new lame.Mp3Encoder(1, samplerate, 320);
-                let mp3Tmp = mp3Encoder.encodeBuffer(output);
-                let mp3TmpTail = mp3Encoder.flush()
-                let sampleBlockSize = 1152; //can be anything but make it a multiple of 576 to make encoders life easier
-
-                let mp3Data = [];
-                // Copied From: https://github.com/zhuker/lamejs
-                for (let i = 0; i < output.length; i += sampleBlockSize) {
-                    let sampleChunk = output.subarray(i, i + sampleBlockSize);
-                    let  mp3buf = mp3Encoder.encodeBuffer(sampleChunk);
-                    if (mp3buf.length > 0) {
-                        mp3Data.push(mp3buf);
-                    }
-                }
-                let mp3buf = mp3Encoder.flush();   //finish writing mp3
-                if (mp3buf.length > 0) {
-                    mp3Data.push(new Int8Array(mp3buf));
+                kali.output(output);
+
+
+                const WavEncoder = require('wav-encoder')
+                const prep = {
+                    sampleRate: samplerate,
+                    channelData: [
+                        output
+                    ]
                 }
-                fs.writeFileSync("./test.mp3", new Uint8Array(await (new Blob(mp3Data, {type: 'audio/mp3'})).arrayBuffer()) );
+                let wavBuffer = await WavEncoder.encode(prep)
 
 
 
 
-                res.contentType("audio/mp3");
+                res.contentType("audio/wav");
                 res.header("Access-Control-Allow-Origin", "*");
                 res.header("Access-Control-Allow-Headers", "Content-Type");
                 res.header("Access-Control-Allow-Origin", "*");
-                res.header('Content-Disposition', `attachment; filename="${encodeURI(availCache[uuid].song_name)}.mp3"`);
-                res.sendSeekable()
+                res.header('Content-Disposition', `attachment; filename="${encodeURI(availCache[uuid].song_name)}.wav"`);
+                res.sendSeekable(Buffer.from(wavBuffer))
             }
 
 
@@ -601,37 +590,13 @@ app.get('/:uuid', function (req, res) {
     }
 
     if (availCache[uuid]) {
-        res.contentType("audio/mp3");
-        res.header("Access-Control-Allow-Origin", "*");
-        res.header("Access-Control-Allow-Headers", "Content-Type");
-        res.header("Access-Control-Allow-Origin", "*");
-        res.header('Content-Disposition', `attachment; filename="${encodeURI(availCache[uuid].song_name)}.mp3"`);
-        if(!pitch){
-            res.sendSeekable(availCache[uuid].databinary)
-        }else{
-            let output = new Float32Array(availCache[uuid].databinary.length)
-            let shifter  =require('pitch-shift')(
-                function onData(frame){
-                    output.push(frame);
-                },
-                function onTune(ert,pitch){
-                    return 2;
-                }
-
-            )
-            shifter(new Float32Array(availCache[uuid].databinary));
-            res.contentType("audio/mp3");
-            res.header("Access-Control-Allow-Origin", "*");
-            res.header("Access-Control-Allow-Headers", "Content-Type");
-            res.header("Access-Control-Allow-Origin", "*");
-            res.header('Content-Disposition', `attachment; filename="${encodeURI(availCache[uuid].song_name)}.mp3"`);
-            res.sendSeekable(availCache[uuid].output)
-
-        }
-
+        await Provider();
 
     } else {
-        db.execute("SELECT song_name, databinary FROM instrunet_entry WHERE uuid = ?", [uuid], Provider)
+        db.execute("SELECT song_name, databinary FROM instrunet_entry WHERE uuid = ?", [uuid], (err, rows)=>{
+            availCache[uuid] = rows[0]
+            Provider()
+        })
 
     }
 

+ 7 - 0
package-lock.json

@@ -30,6 +30,7 @@
         "send-seekable": "^1.0.4",
         "sort-array": "^5.0.0",
         "sqlite3": "^5.1.7",
+        "wav-encoder": "^1.3.0",
         "webp-converter": "^2.3.3",
         "webp-converter-browser": "^1.0.4",
         "ws": "^8.18.0"
@@ -3027,6 +3028,12 @@
         "node": ">= 0.8"
       }
     },
+    "node_modules/wav-encoder": {
+      "version": "1.3.0",
+      "resolved": "https://registry.npmjs.org/wav-encoder/-/wav-encoder-1.3.0.tgz",
+      "integrity": "sha512-FXJdEu2qDOI+wbVYZpu21CS1vPEg5NaxNskBr4SaULpOJMrLE6xkH8dECa7PiS+ZoeyvP7GllWUAxPN3AvFSEw==",
+      "license": "MIT"
+    },
     "node_modules/webidl-conversions": {
       "version": "3.0.1",
       "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",

+ 1 - 1
package.json

@@ -23,7 +23,6 @@
     "https-proxy-agent": "^7.0.6",
     "js-queue": "^2.0.2",
     "lamejs": "zhuker/lamejs",
-
     "minimist": "^1.2.8",
     "mysql2": "^3.11.5",
     "node-fetch": "^2.7.0",
@@ -34,6 +33,7 @@
     "send-seekable": "^1.0.4",
     "sort-array": "^5.0.0",
     "sqlite3": "^5.1.7",
+    "wav-encoder": "^1.3.0",
     "webp-converter": "^2.3.3",
     "webp-converter-browser": "^1.0.4",
     "ws": "^8.18.0"