0

I'm trying to use an audio buffer composed of Float32Arrays (1 for each channel) as asset data to create a soundcloud track. The audio buffer for a mono recording looks like this:

[Float32Array { 0=-0.0001220703125,  1=0.000579833984375,  2=0.000762939453125,  ...}]

So I assume that I should convert it to a binary string. To know the correct encoding, I took a look at http://connect.soundcloud.com/examples/recording.html It's flash but at the end the following post is done:

audio.wav
--ievejmnijdgyooinchslwnpygpivapif
Content-Disposition: form-data; name="track[asset_data]"; filename="audio.wav"
Content-Type: application/octet-stream

RIFF(WAVEfmt D¬XdataÚÿÇÿÿ+ÿc]ÿ1&»ÿUÿ(1ÊÿEc&môã...

In order to turn my audio buffer into such, I used code similar to https://gist.github.com/kevincennis/9754325 so that now my process looks like:

worker.onmessage = function( e ) {
  var blob = e.data;
  var reader = new window.FileReader();
  reader.onloadend = function() {
     var binaryData = reader.result;                
     SC.post('/tracks', {
       asset_data : binaryData,
       title : 'recording'
     });
  };
  reader.readAsText(blob);
});

Still, there should be some problem with the encoding. The above, produces the following post:

_status_code_map[302]   200
asset_data              RIFF ���WAVEfmt �����D�������data����
format                  json
oauth_token             1....
title                   recording

but the response of Soundclound API is still: "NetworkError: 422 Unprocessable Entity - https://api.soundcloud.com/tracks"

Can anybody spot what's wrong?

digital illusion
  • 497
  • 3
  • 19
  • 1
    turn it into a blob, then you can upload the blob or use FormData to turn the blob into what looks to a server like an attached file – dandavis Jun 30 '15 at 22:09
  • I followed your suggestion, but I just get: asset_data=%5Bobject%20Blob%5D – digital illusion Jun 30 '15 at 22:22
  • that high-level upload function might not handle binary data, it's relatively new. – dandavis Jun 30 '15 at 22:29
  • I've updated my code following your suggestion, but somethng is still not working about the encoding – digital illusion Jun 30 '15 at 22:31
  • https://developers.soundcloud.com/docs/api/guide#uploading – dandavis Jun 30 '15 at 22:34
  • I now know that by heart, but I don't want to record the sound using Soundcloud SDK. Is there any useful informaiton that I missed there? – digital illusion Jun 30 '15 at 22:35
  • they don't seem to have a way to upload binary files in their JS client. what you might try is running a record demo, then looking at the devtools network tab to spy on the POST. if you can re-create the raw HTTP interaction in a custom ajax call, you can replace the recorded audio with your new stuff in the same format. if they want MP3s, that's going to be a tall order, but you can probably ship WAV-format audio data, which is easy to build from raw arraybuffer samples. look into recorder.js for an arraybuffer to wav blob routine. – dandavis Jun 30 '15 at 22:40
  • looks like it's WAV, but I have no clue about how to use my audio buffer... – digital illusion Jun 30 '15 at 22:50

1 Answers1

0

Turns out that Soundcloud only accept the request if it's in multipart form encoding. It seems not possible to me to achieve this using the SDK. This is the code using angular.js:

worker.onmessage = function( e ) {
  var blob = e.data;
  var fd = new FormData();
  fd.append('oauth_token', SC.accessToken());
  fd.append('format','json');
  fd.append('track[title]', 'recording');
  fd.append('track[asset_data]', blob);

  $http.post('https://api.soundcloud.com/tracks', fd, {
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
  })
});
digital illusion
  • 497
  • 3
  • 19