简体   繁体   中英

Convert a BLOB file (a music file) to .WAV in NODE.js (server-side)

I'm struggling with a seemingly simple concept.

I have a Node server it properly receives a POST request. I am posting a Blob to it (converting the blob to a .wav file).

What do I need to do to save this Blob to disk as a .wav file? I am using a music player to play the user's uploaded music file and then it is supposed to play the song.

You didn't give too much information but are you using Express? and which database would you like to store that on.

You may want to

    var express = require('express');
    var bodyParser = require('body-parser');
    var sqlite3 = require("sqlite3").verbose();
    var cors = require('cors');

    var db = new sqlite3.Database("db/database.db");
    var app = express();

    app.use(cors());
    app.use(bodyParser.json({ extended: false }));

    app.use(express.static('public'));
function toArrayBuffer(buffer) {
    var ab = new ArrayBuffer(buffer.length);
    var view = new Uint8Array(ab);
    for (var i = 0; i < buffer.length; ++i) {
        view[i] = buffer[i];
    }
    return ab;
}


  app.post("/blobs" function(req, res){
    console.log(req.body);
//You should hopefully have your blob show up...
    toArray(blob);
    db.run("INSERT INTO pics (name, blob) VALUES (?, ?)",___nameOfBlob(hopefully from req.body as well if you have a form___, __bufferBase64__, function(err){
    if(err) console.log(err);
    });
});

Give us more details if that doesn't work in general... If you want to convert to a .wav Check this out:

function encodeWAV(samples) {
var buffer = new ArrayBuffer(44 + samples.length * 2);
var view = new DataView(buffer);

writeString(view, 0, 'RIFF');
view.setUint32(4, 32 + samples.length * 2, true);
writeString(view, 8, 'WAVE');
writeString(view, 12, 'fmt ');
view.setUint32(16, 16, true);
view.setUint16(20, 1, true);
view.setUint16(22, 2, true);
view.setUint32(24, sampleRate, true);
view.setUint32(28, sampleRate * 4, true);
view.setUint16(32, 4, true);
view.setUint16(34, 16, true);
writeString(view, 36, 'data');
view.setUint32(40, samples.length * 2, true);

floatTo16BitPCM(view, 44, samples);

return view;
}
function exportWAV(type) {
var bufferL = mergeBuffers(recBuffersL, recLength);
var bufferR = mergeBuffers(recBuffersR, recLength);
var interleaved = interleave(bufferL, bufferR);
var dataview = encodeWAV(interleaved);
var audioBlob = new Blob([dataview], {
    type: type
});

this.postMessage(audioBlob);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM