0

I'm new to nativescript and developing ios version app now. I have some trouble handling Audio Queue callback.

I need capturing raw audio buffer data via device microphone.(evaluating input level in millisecond order)1 To do this, I'm trying to call ios native AudioToolBox/AudioQueueNewInput API from my custom plugin.

Finally, it run with no error, but the callback "inCallbackProc" is never fired...

Here is my code.

myplugin.ios.js

var kSamplingRate = 44100;
var inUserData = null;
var inCallbackRunLoopMode = kCFRunLoopDefaultMode;
var inFlags = 0;
//var outAQ = AudioQueueRef.alloc().init(); // error
//var outAQ = new OpaqueAudioQueue(); //error
var outAQ;
var inFormat = 
    new AudioStreamBasicDescription(
    kSamplingRate,
    kAudioFormatLinearPCM,
    kLinearPCMFormatFlagIsFloat,
    4,
    1,
    4,
    1,
    8,
    0
);

var buffers = [null, null, null];

var audiomodule = {
    inCallbackProc : function(
        inUserData, 
        inAQ, 
        inBuffer, 
        inStartTime, 
        inNumberPacketDescriptions, 
        inPacketDescs 
        ){
            console.log("callbacked");
        }
    ,record: function(){
        console.log("recordstarting");

        AudioQueueNewInput (
         inFormat, 
         this.inCallbackProc.copy,
         inUserData, 
         CFRunLoopGetCurrent(),
         inCallbackRunLoopMode, 
         inFlags, 
         outAQ
         );
        for (i in [0, 1, 2]) {
            AudioQueueAllocateBuffer(outAQ, 256, buffers[i])
            AudioQueueEnqueueBuffer(outAQ, buffers[i], 0, null)
           }

    AudioQueueStart(outAQ, null);
    }

};

module.exports = audiomodule;

app.js

var MyPlugin = require("myplugin");

exports.loaded = function(args) {
    var page = args.object;
    if (page.ios) {
        MyPlugin.record();
    }
};

When Executing this code, I can find "recordstarting" in console log with no error, but can't find "callbacked".

I have read this doc. NativeScript Doc - Marshaling

I appreciate anyone help or comment.

shige
  • 11
  • 2
  • I found that similar audio plugin exists for NativeScript and you could use it for playing and recording an audio - https://github.com/bradmartin/nativescript-audio – Nikolay Tsonev Jul 19 '16 at 13:25
  • Hi Nikolay, that plugin is based on AVAudioRecorder. And that API might not be suitable for my app, because of latency. pls see [this](http://stackoverflow.com/a/7402810/6606449). I need capturing raw audio. – shige Jul 28 '16 at 06:51
  • I was unable to find suitable plugin for your project, which is capturing raw audio. However you could use the described steps here -http://stackoverflow.com/questions/6039291/simplest-way-to-capture-raw-audio-from-audio-input-for-real-time-processing-on-a and to create your own plugin, which is providing this functionality. Another option is to log a new issue in the bradmartin's plugin repo for raw audio support. – Nikolay Tsonev Jul 28 '16 at 08:16
  • Thanks Nikolay, I decided to create my own plugin and described my experience as answer. – shige Aug 15 '16 at 04:10

1 Answers1

1

I decided to create my own plugin.
My own Objective-c library wraps native AudioRecord(Core Audio) API.
And own plugin js exposes the library functions.
Finally, it works, callback work also.

mylibrary(Objective-c)

@property (nonatomic, copy) void (^completionHandler)(NSString *result);

myplugin.ios.js

mylibrary.completionHandler = (
function(response){
    console.log(response);
}
shige
  • 11
  • 2