简体   繁体   English

如何从节点红色仪表板模板发送msg.payload Int16Array

[英]How to send msg.payload Int16Array from node-red dashboard template

I am trying to send an Int16Array from a node-red dashboard template. 我正在尝试从节点红色仪表板模板发送Int16Array In the template I have: 在模板中,我有:

var i16Buff = new Int16Array(i16BuffSize);

//... fill with data

scope.send({payload: i16Buff});

The buffer comes through msg.payload and I can see the data in console.log as a JSON array. 缓冲区通过msg.payload ,我可以在console.log中将数据视为JSON数组。 How do I send it from node-red dashboard template so it remains an Int16Array . 我该如何从节点红色仪表板模板发送它,使其保持Int16Array

Because this was so painful to figure out, I thought I'd just put my entire code up as an example for others: 因为很难理解,所以我认为我将整个代码作为其他示例:

<!DOCTYPE html>
<video style="height: 0px; width: 0px;"></video>
<md-button ng-click="startStopRec()">{{ label }}</md-button>

<script>

    (function(scope) {

        // Setup cross browser compatibility
        navigator.getUserMedia  = navigator.getUserMedia ||
                navigator.webkitGetUserMedia ||
                navigator.mozGetUserMedia ||
                navigator.msGetUserMedia;

        var video = document.querySelector('video');

        var startedRecording = false;
        var endRecording = false;
        var scriptNode;
        var audioCtx;

        // Check if supported by browser
        if (navigator.getUserMedia) {
            console.log('getUserMedia supported.');
            navigator.getUserMedia (

                {
                    audio: true,
                    video: false
                },

                // Success callback
                function(stream) {

                    var buffSize = 2048;
                    var buff = [];

                    video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
                    video.onloadedmetadata = function(e) {
                        video.muted = 'true';
                    };

                    audioCtx = new AudioContext();
                    var source = audioCtx.createMediaStreamSource(stream);

                    scriptNode = audioCtx.createScriptProcessor(buffSize, 1, 1);

                    scriptNode.onaudioprocess = function(APE) {

                        if(endRecording === true) {

                            // There is probably a better way to do this but it worked for me
                            scriptNode.disconnect();
                            startedRecording = false;
                            endRecording = false;

                            // This was key for creating appropriate buffer for msg.payload.
                            var rawBuffer = new ArrayBuffer(buff.length * buffSize * 2);
                            var rawView = new DataView(rawBuffer);

                            var index = 0;
                            for (var i = 0; i < buff.length; i++) {

                                // Convert multi array audio buffer to flat Int16
                                for (var j = 0; j < (buffSize); j++){
                                    rawView.setInt16(index, buff[i][j] * 0x7FFF, true);
                                    index += 2;
                                }
                            }
                            // Send msg
                            scope.send({payload: rawBuffer, sampleRate: audioCtx.sampleRate});
                            // Clear buffer for next time
                            buff = [];

                        } else {
                            // Collect audio buffers into array
                            console.log('Getting data');
                            buff.push(new Float32Array(APE.inputBuffer.getChannelData(0)));
                        }
                    }
                    source.connect(scriptNode);
                },

                // Error callback
                function(err) {
                    console.log('The following gUM error occured: ' + err);
                }
            );

        } else {
           console.log('getUserMedia not supported on your browser!');
        }

        function writeUTFBytes(view, offset, string){ 
            var lng = string.length;
            for (var i = 0; i < lng; i++){
                view.setUint8(offset + i, string.charCodeAt(i));
            }
        }

        if(scope.label === undefined) {
            scope.label = 'Record';
        }

        scope.startStopRec = function() {

            if(scope.label === 'Record') {
                scope.label = 'Send';
                scriptNode.connect(audioCtx.destination);
                video.play();
                startedRecording = true;
            } else {
                scope.label = 'Record';
                if(startedRecording === true) {
                    endRecording = true;
                    video.pause();
                }
            }
        }

    })(scope);    

</script>

This template returns (msg.payload) a buffer of raw mono audio data from the microphone and (msg.sampleRate) the sample rate through msg to the next node in the line. 该模板从麦克风返回(msg.payload)原始单声道音频数据的缓冲区,并通过msg返回(msg.sampleRate)采样率到行中的下一个节点。

NOTE: You must use HTTPS for it to work (from what I've read/experienced). 注意:您必须使用HTTPS才能正常工作(根据我的阅读/经验)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 msg.payload 从注入节点移动到节点红色仪表板中的模板节点? - How to move msg.payload from inject node to template node in node red dashboard? 如何在节点红色中增加msg.payload [i] - How to increment msg.payload[i] in node-red 在循环中的仪表板ui_template(node-red)中创建复选框,并将check-status分配给输出msg.payload - create checkbox in dashboard ui_template (node-red) in loop and assign check-status to output msg.payload node-red 无法在模板节点和 JS 标签上获取 msg.payload - node-red cannot get msg.payload on template node unde JS tag node-red 将 json 字符串解析为 msg.payload - node-red parsing a json string to msg.payload 使用 node-red 中的 msg.payload 运行 javascript 函数 - run javascript function using msg.payload in node-red Node-red:从 msg.payload 获取值并将其保存在变量中 - Node-red: get value from msg.payload and save it inside variable 您如何使用JavaScript在Node-Red中的msg.payload中解码HTML实体? - How would you decode HTML entities inside the msg.payload in Node-Red with javascript? 在节点红色中仅显示msg.payload中的值字段 - display only value fields from the msg.payload in node red 如何将msg.payload存储到ui_template节点红色内的脚本变量中? - How to store a msg.payload into a script variable inside a ui_template node red?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM