syedhali / EZAudio

An iOS and macOS audio visualization framework built upon Core Audio useful for anyone doing real-time, low-latency audio processing and visualizations.
Other
4.93k stars 821 forks source link

Increase the audio input level #381

Open desmeit opened 4 years ago

desmeit commented 4 years ago
- (void) configureAudioSession
{

    AVAudioSession* session = [AVAudioSession
                               sharedInstance];

    // Don't activate the audio session yet
    [session setActive:NO error:NULL];

    // Play music even in background and dont stop playing music
    // even another app starts playing sound
    [session setCategory:AVAudioSessionCategoryRecord error:NULL];

    double sampleRate = 16000.0;
    [session setPreferredSampleRate:sampleRate error:NULL];

    // Active the audio session
    [session setActive:YES error:NULL];
}

- (void) initMic {
    [self configureAudioSession];
    AudioStreamBasicDescription audioStreamBasicDescription = [EZAudioUtilities monoFloatFormatWithSampleRate:16000];
    audioStreamBasicDescription.mFormatID = kAudioFormatLinearPCM;
    audioStreamBasicDescription.mSampleRate = 16000.0;
    audioStreamBasicDescription.mFramesPerPacket = 1;
    audioStreamBasicDescription.mBytesPerPacket = 2;
    audioStreamBasicDescription.mBytesPerFrame = 2;
    audioStreamBasicDescription.mChannelsPerFrame = 1;
    audioStreamBasicDescription.mBitsPerChannel = 16;
    audioStreamBasicDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
    audioStreamBasicDescription.mReserved = 0;

    NSArray *inputs = [EZAudioDevice inputDevices];
    [self.microphone setDevice:[inputs lastObject]];
    self.microphone = [EZMicrophone microphoneWithDelegate:self withAudioStreamBasicDescription:audioStreamBasicDescription];
    [self.microphone startFetchingAudio];
}

-(void) microphone:(EZMicrophone *)microphone
  hasAudioReceived:(float **)buffer
    withBufferSize:(UInt32)bufferSize
withNumberOfChannels:(UInt32)numberOfChannels {
    dispatch_async(dispatch_get_main_queue(),^{
        int result = _snowboyDetect->RunDetection(buffer[0], bufferSize);
        if (result >= 1) {
             NSLog(@"Hotword Detected");
            NSString *modelName = modelsList[result - 1];
            NSDictionary *jsonObj = @{@"message":@"Hotword Detected",@"modelname": modelName};
            [self triggerJSEventData:jsonObj];
           // [self triggerJSEvent:@"Hotword Detected"];
            detection_countdown = 30;
        } else {
            if (detection_countdown == 0){
                NSLog(@"No Hotword Detected");
                [self triggerJSEvent:@"No Hotword Detected"];
            } else {
                detection_countdown--;
            }
        }
    });
}

I use EZAudio which is working with Snowboy Hotword Detection. On Android the Hotword ist recognizing perfectly, even if I speak quiet. With ios the Hotword is not recognized if I speak very quietly. Now my question: Is it possible to increase the microphone level on IOS? I have a feeling it's related to that. Is there any way to increase the mic input level?

designerfuzzi commented 2 years ago

As of 2019 you need to allow Microphone Access with proper entry in info.plist and initiating a dialog to ask the user. Once the consent is given by the user, the microphone or any audio input signal should be available. Until then, without consent, no audio can be recorded or nothing will arrive at EZMicrophone