Upload audio training data using node.js on edge device

I would like to use Node-RED (based on node.js) to upload my recorded audio files to the ingestion service.

On page https://docs.edgeimpulse.com/reference#data-acquisition-format I have found a Node.js example for 'Content-Type': 'application/json' but is it possible to use this content type for uploading audio files ? If so how should the wav audio file be put in the body ?

If not possible: is there some example describing the upload of an audio file (or binary data) for 'Content-Type': 'application/cbor' for Node.js ?

Currently I am using janvda/node-red-contrib-cbor (which is based on hildjj/node-cbor version v5.0.2) for converting json to cbor but once the json structure becomes a bit more complex this doesn’t seem to work properly when I tested it as follows:

  1. manually copy paste the buffer output in string format from node-red debug node to https://codebeautify.org/string-hex-converter and convert it to hex.
  2. copy paste the hex string http://cbor.me/ in the right window and convert it back. In that case it reports error like: Out of bytes to decode (need at least 17 bytes more)

An example fo the problem

  1. The input json string:
{"protected":{"ver":"v1","alg":"none","iat":1595339535},"payload":{"device_name":"ac:87:a3:0a:2d:1b","device_type":"DISCO-L475VG-IOT01A","interval_ms":10,"sensors":[{"name":"accX","units":"m/s2"},{"name":"accY","units":"m/s2"},{"name":"accZ","units":"m/s2"}]}}
  1. the json string converted to cbor (in string format)
¢iprotected£cverbv1calgdnoneciat_ógpayload¤kdevice_nameqac:87:a3:0a:2d:1bkdevice_typesDISCO-L475VG-IOT01Akinterval_ms
gsensorsƒ¢dnamedaccXeunitsdm/s2¢dnamedaccYeunitsdm/s2¢dnamedaccZeunitsdm/s2
  1. cbor this time converted to hex format:
a26970726f746563746564a36376657262763163616c67646e6f6e65636961741a5f16f3f677061796c6f6164a46b6465766963655f6e616d657161633a38373a61333a30613a32643a31626b6465766963655f7479706573444953434f2d4c34373556472d494f543031416b696e74657276616c5f6d73a6773656e736f727383a2646e616d65646163635865756e697473646d2f7332a2646e616d65646163635965756e697473646d2f7332a2646e616d65646163635a65756e697473646d2f7332
  1. cbor converted back gives
{"protected": {"ver": "v1", "alg": "none", "iat": 1595339766}, "\u0006\u0017\x96\xC6\xF6\u0016JF\xB6FWf\x966U\xF6\xE6\u0016\xD6W\u0016\u00163": {["\xA6\u00133\xA3\u0006\u0013\xA3&C\xA3\u0016&\xB6FWf\x966U", undefined, h'97065734449534']: -21, simple(18): 20(-7558501), "\u0514\xF5C\u0003\u0014\u0016\xB6\x96\xE7FW'f\u0016\xC5\xF6\xD7": -1735615855}}

and reports following error: 71 unused bytes after the end of the data item

Hi @janvda,

You can upload audio samples similarly as with other sensors by filling the values field with your audio samples as an array. Also you derive the interval_ms value from your audio sampling frequency and set the sensors value as follows:

sensors: [{ name: 'audio', units: 'wav' }]

Check also the code example of our cli uploader makeWav function, it uses the ingestion service to import wav files.

Cheers,
Aurelien

1 Like

I have found the issue. The problem is located in step 2 where I am copy pasting the cbor data in string format. Most likely the cbor data in string format doesn’t contain all binary data (all bits).

I fixed this issue by replacing step 2 and 3 by a step in node-red that converts the cbor buffer to a hex string using the command buffer.toString('hex'). This hex string when used in http://cbor.me/ (see step 4) properly converted back to the original json structure.

1 Like

@janvda, interesting. You mean this one?

That decodes fine for me at cbor.me.

Or do you mean:

That’s a special case with both the CBOR file (until ff) and then binary payload in int16 format, so won’t decode fully in cbor.me/

No, the problem was related to the CBOR data I have created, not with the one on page : https://docs.edgeimpulse.com/reference#data-acquisition-format (that is working fine).

This problem is in the mean time fixed (see 2 comments ago).

FYI: I have reported my issue with sending cbor data using node-red in the node-red forum:

Yeah my suggestion with Node would be to use JSON instead. I’ve found CBOR libraries to be of disappointing quality.

I have seen the comment from @aurel concerning JSON option, but that would mean that I need to convert my binary wav file to an array.

I have found a solution:

  • instead of using application/cbor as content type, I should use application/octet-stream.

With this change I managed to upload the following valid audio sample to my edge impulse dashboard via node-red.