apache / cordova-plugin-media-capture

Apache Cordova Media Capture Plugin
https://cordova.apache.org/
Apache License 2.0
307 stars 492 forks source link

How to initialize sampling rate in cordova-plugin-media-capture? #143

Open ityoung2016 opened 5 years ago

ityoung2016 commented 5 years ago

Bug Report

iPhone sampling rate starts wrong and later 'corrects itself'

Problem

Using the plugin I record and playback audio. On my iPad everything is fine. On the iPhone the sampling rate changes between the first recording and subsequent recordings. How can I initialize the app so that all recordings are at 44100Hz?

What is expected to happen?

On my iPhone (and iPad) I expect a constant sampling rate of 44100Hz.

What does actually happen?

On my iPad (iOS 12.+) everything works as expected. On my iPhone (iOS 12.+) the first recording takes place at a sampling rate of 48000Hz. Subsequent recordings are at the desired rate of 44100Hz.

Information

I verified this using an external tone of 1000Hz and spectral analysis on the two devices. The analysis shows a frequency of 1000Hz on the iPad but the first time on the iPhone this frequency is 918.75HZ = 1000*(44100/48000). For the record, the same cordova-generated app is run on both devices, no error messages are generated, and I do not change the sampling rate on the iPhone. It happens “by itself” through the plugin.

Command or Code

function audioMedia(val) { mediaType = 'audio'; var audioNum = val; myAudio = null;

// captureAudioSuccess callback
var captureAudioSuccess = function(mediaFiles) {
    acquired[audioNum] = true;
    var path = null;
    var len = mediaFiles.length;
    for (var i = 0; i < len; i++) {
        path = mediaFiles[i].fullPath;
        console.log('media full path = '+path, len);
        readMedia(mediaFiles[0].name,audioNum);
        }
    };
// captureAudioError callback
var captureAudioError = function(error) {
    alert('No audio was recorded. Try again?');
    };
// start audio capture
navigator.device.capture.captureAudio(captureAudioSuccess, captureAudioError, audioOptions);
};

Environment, Platform, Device

iPhone 6s, iPhone 7

Version information

cordova: 9.0.0 (cordova-lib@9.0.1) From my config.xml file: "cordova-plugin-media-capture" spec="^3.0.2"

Checklist

ityoung2016 commented 5 years ago

I updated to v3.0.3 of the plugin but the problem persists.

ityoung2016 commented 3 years ago

 Dear Kevin,

44.1 kHz means that an audio signal has been sampled 44100 times per second. The highest frequency that a human can hear is about 20 kHz and proper processing (e.g. noise suppression) requires that the signal be sampled at at least twice that number. That means the sampling frequency should be greater than 40000 samples per second (40 kHz). The digital sound industry chose 44.1 kHz as meeting that requirement. Computational issues probably also played a role in that choice because 44100 is a highly composite number, (235*7)^2 = 44100, which makes for faster processing algorithms such as the FFT.

Dogs can hear above 20 kHz but you and I can’t so I don’t see any advantage to sampling at 192 kHz (high-definition audio) for human audio purposes. Selling audio equipment that uses high-definition sampling is, in general, a good way to separate people from their money. Unless one is combining many audio channels into one data stream, the higher sampling rate is overkill.

But can you go below 44.1 kHz? Yes, but you have to be careful. Undersampling, going below 44.1 kHz, may work if there is negligible energy in the higher frequencies. The human voice rarely goes above 5 kHz so a sampling rate of 11 kHz should be sufficient. Musical accompaniment in the background,however, could mess up that calculation and the resulting sound quality. Some applications suppress the energy in the higher frequencies by lowpass filtering the signal from the microphones before the audio signal is sampled. There are digital tricks as well.

Data storage and processing times improve as the number of samples per second go down but the quality of the resulting audio signal can suffer. Like life, the audio world is filled with tradeoffs.

You can read more about this hear:

https://www.headphonesty.com/2019/07/sample-rate-bit-depth-bit-rate/

And if you want to see the frequency spectrum of your voice to see what the highest frequency is, try Laboratory Exercise 3.1 in my free app:

https://apps.apple.com/nl/app/stochastic-signal-processing/id1450268179?l=en

Good luck and stay well,

Ted Young


On 1 Dec 2020, at 12:44, Kévin Perrée notifications@github.com wrote:  I get sample rates of 44.1hz too why?

Is it possible to match voice memo app quality / codecs / and settings ?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or unsubscribe.