Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  142] [ 2]  / answers: 1 / hits: 26980  / 8 Years ago, thu, september 8, 2016, 12:00:00

I'm trying to write a .wav file with fs.writeFile. The file is created successfully, however it's only 8-13bytes long, so obviously I'm not doing something right.



If the blob is already audio/wav can I write to disk or do I need to convert it to Base 64?



I'm pretty much at a loss here, I found another similar thread with no answer - Here



Any input would be appreciated.



routerApp.controller('audiotest', function($scope) {
$scope.saveToDisk = function(){
var nw = require('nw.gui');
var fs = require('fs');
var path = require('path');
fs.writeFileSync('test.wav', $scope.recordedInput)
};
}


console.log($scope.recordedInput) returns Blob {size: 294956, type: audio/wav}



It's not really relevant, but here's my HTML



<div class=row ng-controller=audiotest>
<div class=row>
<button type=button ng-click=saveToDisk()> Write this sucker to disk </button>
</div>

<ng-audio-recorder id='audioInput' audio-model='recordedInput'>
<!-- Start controls, exposed via recorder-->
<div ng-if=recorder.isAvailable>
<button ng-click=recorder.startRecord() type=button ng-disabled=recorder.status.isRecording>
Start Record
</button>
<button ng-click=recorder.stopRecord() type=button ng-disabled=recorder.status.isRecording === false>
Stop Record
</button>

</ng-audio-recorder>
</div>

More From » node.js

 Answers
12

You can convert the Blob to a Typed Array and then to a Buffer for passing directly to fs.writeFileSync():



var fileReader = new FileReader();
fileReader.onload = function() {
fs.writeFileSync('test.wav', Buffer.from(new Uint8Array(this.result)));
};
fileReader.readAsArrayBuffer($scope.recordedInput);

[#60774] Monday, September 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kileyr

Total Points: 112
Total Questions: 105
Total Answers: 114

Location: United States Minor Outlying Island
Member since Sat, May 28, 2022
2 Years ago
;