|  | @@ -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()
 | 
	
		
			
				|  |  | +        })
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 |