From 30914d9119240ee2df85025c2a6c8204572e31a6 Mon Sep 17 00:00:00 2001 From: Vaidegi B Date: Tue, 27 Jul 2021 19:28:11 +0530 Subject: [PATCH] Update JS APIs as per new requirement Signed-off-by: Vaidegi B --- README.md | 18 +- .../audio_manager/src/audio_manager_napi.cpp | 63 +++++- .../audio_manager/@ohos.multimedia.audio.d.ts | 198 +++++++++--------- .../include/audio_manager_napi.h | 10 +- 4 files changed, 170 insertions(+), 119 deletions(-) diff --git a/README.md b/README.md index 6530020343..660d5e8b01 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Sampling is a process to obtain discrete-time signals by extracting samples from - **Sampling rate** -Sampling rate is the number of samples extracted from a continuous signal per second to form a discrete signal. It is measured in Hz. Generally, human hearing range is from 20 Hz to 20 kHz. Common audio sampling rates include 8 kHz, 11.025 kHz, 22.05 kHz, 16 kHz, 37.8 kHz, 44.1 kHz, 48 kHz, 96 kHz, and 192 kHz. +Sampling rate is the number of samples extracted from a continuous signal per second to form a discrete signal. It is measured in Hz. Generally, human hearing range is from 20 Hz to 20 kHz. Common audio sampling rates include 8 kHz, 11.025 kHz, 22.05 kHz, 16 kHz, 37.8 kHz, 44.1 kHz, 48 kHz, and 96 kHz. - **Channel** @@ -47,7 +47,7 @@ The structure of the repository directory is as follows: /foundation/multimedia/audio_standard # Audio code ├── frameworks # Framework code │ ├── innerkitsimpl # Internal Native API Implementation. -| | Pulseaudio and libsnd file build configuration.pulseaudio-hdi modules +| | Pulseaudio, libsndfile build configuration and pulseaudio-hdi modules │ └── kitsimpl # External JS API Implementation ├── interfaces # Interfaces │ ├── innerkits # Internal Native APIs @@ -77,15 +77,15 @@ You can use APIs provided in this repository to convert audio data into audible audioRenderer->SetParams(rendererParams); ``` -4. (Optional) use audioRenderer->**GetParams**(rendererParams); to validate SetParams +4. (Optional) use audioRenderer->**GetParams**(rendererParams) to validate SetParams 5. Call **audioRenderer->Start()** function on the AudioRenderer instance to start the playback task. 6. Get the buffer length to be written, using **GetBufferSize** API . ``` audioRenderer->GetBufferSize(bufferLen); ``` -7. Read the audio data to be played from the source(example audio file) and transfer it into the bytes stream. Call the **Write** function repeatedly to write the render data. +7. Read the audio data to be played from the source(for example, an audio file) and transfer it into the bytes stream. Call the **Write** function repeatedly to write the render data. ``` - bytesToWrite = fread(buffer, 1, bufferLen, wavFile); + bytesToWrite = fread(buffer, 1, bufferLen, wavFile); while ((bytesWritten < bytesToWrite) && ((bytesToWrite - bytesWritten) > minBytes)) { bytesWritten += audioRenderer->Write(buffer + bytesWritten, bytesToWrite - bytesWritten); if (bytesWritten < 0) @@ -97,7 +97,7 @@ You can use APIs provided in this repository to convert audio data into audible 9. Call audioRenderer->**Stop()** function to Stop rendering. 10. After the playback task is complete, call the audioRenderer->**Release**() function on the AudioRenderer instance to release the resources. -Provided the basic playback usecase above. Please refer [**audio_renderer.h**](https://gitee.com/openharmony/multimedia_audio_standard/interfaces/innerkits/native/audiorenderer/include/audio_renderer.h) and [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/interfaces/innerkits/native/audiocommon/include/audio_info.h) for more APIs. +Provided the basic playback usecase above. Please refer [**audio_renderer.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/innerkits/native/audiorenderer/include/audio_renderer.h) and [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/innerkits/native/audiocommon/include/audio_info.h) for more APIs. ### Audio Recording @@ -142,14 +142,12 @@ You can use the APIs provided in this repository for your application to record 9. Call the audioRecorder->**Stop**() function on the AudioRecorder instance to stop the recording. 10. After the recording task is complete, call the audioRecorder->**Release**() function on the AudioRecorder instance to release resources. -Provided the basic recording usecase above. Please refer [**audio_recorder.h**](https://gitee.com/openharmony/multimedia_audio_standard/interfaces/innerkits/native/audiorecorder/include/audio_recorder.h) and [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/interfaces/innerkits/native/audiocommon/include/audio_info.h) for more APIs. +Provided the basic recording usecase above. Please refer [**audio_recorder.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/innerkits/native/audiorecorder/include/audio_recorder.h) and [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/innerkits/native/audiocommon/include/audio_info.h) for more APIs. ### Audio Management JS apps can use the APIs provided by audio manager to control the volume and the device.\ -Please refer the following for JS usage of audio volume and device management: - https://gitee.com/openharmony/docs/blob/master/en/application-dev/js-reference/audio-management.md - +Please refer [**audio-management.md**](https://gitee.com/openharmony/docs/blob/master/en/application-dev/js-reference/audio-management.md) for JS usage of audio volume and device management. ## Repositories Involved diff --git a/frameworks/kitsimpl/audio_manager/src/audio_manager_napi.cpp b/frameworks/kitsimpl/audio_manager/src/audio_manager_napi.cpp index d6563962b3..4f3f833f8d 100644 --- a/frameworks/kitsimpl/audio_manager/src/audio_manager_napi.cpp +++ b/frameworks/kitsimpl/audio_manager/src/audio_manager_napi.cpp @@ -92,12 +92,12 @@ static AudioSystemManager::AudioVolumeType GetNativeAudioVolumeType(int32_t volu AudioSystemManager::AudioVolumeType result = AudioSystemManager::STREAM_MUSIC; switch (volumeType) { - case AudioManagerNapi::MEDIA: - result = AudioSystemManager::STREAM_MUSIC; - break; case AudioManagerNapi::RINGTONE: result = AudioSystemManager::STREAM_RING; break; + case AudioManagerNapi::MEDIA: + result = AudioSystemManager::STREAM_MUSIC; + break; default: result = AudioSystemManager::STREAM_MUSIC; HiLog::Error(LABEL, "Unknown volume type, Set it to default MEDIA!"); @@ -160,6 +160,53 @@ static AudioDeviceDescriptor::DeviceFlag GetNativeDeviceFlag(int32_t deviceFlag) return result; } + +static AudioRingerMode GetNativeAudioRingerMode(int32_t ringMode) +{ + AudioRingerMode result = RINGER_MODE_NORMAL; + + switch (ringMode) { + case AudioManagerNapi::RINGER_MODE_SILENT: + result = RINGER_MODE_SILENT; + break; + case AudioManagerNapi::RINGER_MODE_VIBRATE: + result = RINGER_MODE_VIBRATE; + break; + case AudioManagerNapi::RINGER_MODE_NORMAL: + result = RINGER_MODE_NORMAL; + break; + default: + result = RINGER_MODE_NORMAL; + HiLog::Error(LABEL, "Unknown ringer mode requested by JS, Set it to default RINGER_MODE_NORMAL!"); + break; + } + + return result; +} + +static AudioManagerNapi::AudioRingMode GetJsAudioRingMode(int32_t ringerMode) +{ + AudioManagerNapi::AudioRingMode result = AudioManagerNapi::RINGER_MODE_NORMAL; + + switch (ringerMode) { + case RINGER_MODE_SILENT: + result = AudioManagerNapi::RINGER_MODE_SILENT; + break; + case RINGER_MODE_VIBRATE: + result = AudioManagerNapi::RINGER_MODE_VIBRATE; + break; + case RINGER_MODE_NORMAL: + result = AudioManagerNapi::RINGER_MODE_NORMAL; + break; + default: + result = AudioManagerNapi::RINGER_MODE_NORMAL; + HiLog::Error(LABEL, "Unknown ringer mode returned from native, Set it to default RINGER_MODE_NORMAL!"); + break; + } + + return result; +} + napi_status AudioManagerNapi::AddNamedProperty(napi_env env, napi_value object, const string name, int32_t enumValue) { napi_status status; @@ -409,9 +456,9 @@ napi_value AudioManagerNapi::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getMaxVolume", GetMaxVolume), DECLARE_NAPI_FUNCTION("getMinVolume", GetMinVolume), DECLARE_NAPI_FUNCTION("getDevices", GetDevices), - DECLARE_NAPI_FUNCTION("setStreamMute", SetStreamMute), - DECLARE_NAPI_FUNCTION("isStreamMute", IsStreamMute), - DECLARE_NAPI_FUNCTION("isStreamActive", IsStreamActive), + DECLARE_NAPI_FUNCTION("mute", SetStreamMute), + DECLARE_NAPI_FUNCTION("isMute", IsStreamMute), + DECLARE_NAPI_FUNCTION("isActive", IsStreamActive), DECLARE_NAPI_FUNCTION("setRingerMode", SetRingerMode), DECLARE_NAPI_FUNCTION("getRingerMode", GetRingerMode), DECLARE_NAPI_FUNCTION("setDeviceActive", SetDeviceActive), @@ -800,7 +847,7 @@ napi_value AudioManagerNapi::SetRingerMode(napi_env env, napi_callback_info info [](napi_env env, void* data) { auto context = static_cast(data); context->status = - context->objectInfo->audioMngr_->SetRingerMode(static_cast(context->ringMode)); + context->objectInfo->audioMngr_->SetRingerMode(GetNativeAudioRingerMode(context->ringMode)); }, SetFunctionAsyncCallbackComplete, static_cast(asyncContext.get()), &asyncContext->work); if (status != napi_ok) { @@ -855,7 +902,7 @@ napi_value AudioManagerNapi::GetRingerMode(napi_env env, napi_callback_info info env, nullptr, resource, [](napi_env env, void* data) { auto context = static_cast(data); - context->ringMode = context->objectInfo->audioMngr_->GetRingerMode(); + context->ringMode = GetJsAudioRingMode(context->objectInfo->audioMngr_->GetRingerMode()); context->intValue = context->ringMode; context->status = 0; }, diff --git a/interfaces/kits/js/audio_manager/@ohos.multimedia.audio.d.ts b/interfaces/kits/js/audio_manager/@ohos.multimedia.audio.d.ts index 724a1ad6d0..4ed98a3abc 100644 --- a/interfaces/kits/js/audio_manager/@ohos.multimedia.audio.d.ts +++ b/interfaces/kits/js/audio_manager/@ohos.multimedia.audio.d.ts @@ -13,7 +13,7 @@ * limitations under the License. */ import {ErrorCallback, AsyncCallback, Callback} from './basic'; -import {VideoPlayer, AudioPlayer} from '@ohos.Multimedia.media' +import {VideoPlayer, AudioPlayer} from './@ohos.multimedia.media' /** * @name audio * @since 6 @@ -25,8 +25,8 @@ declare namespace audio { /** * Obtains an AudioManager instance. - * @sysCap SystemCapability.Multimedia.Audio * @devices + * @sysCap SystemCapability.Multimedia.Audio */ function getAudioManager(): AudioManager; @@ -36,14 +36,14 @@ declare namespace audio { * @sysCap SystemCapability.Multimedia.Audio */ enum AudioVolumeType { - /** - * Audio streams for media purpose - */ - MEDIA = 1, - /** + /** * Audio streams for ring tones */ RINGTONE = 2, + /** + * Audio streams for media purpose + */ + MEDIA = 3, } /** @@ -112,23 +112,23 @@ declare namespace audio { MIC = 5, } /** - * 音频铃声枚举. + * Enumerates Audio Ringer modes * @devices * @sysCap SystemCapability.Multimedia.Audio */ enum AudioRingMode { /** - * 正常铃声模式 + * Silent mode */ - RINGER_MODE_NORMAL = 0, + RINGER_MODE_SILENT = 0, /** - * 静音铃声模式 + * Vibration mode */ - RINGER_MODE_SILENT = 1, - /** - * 振动铃声模式 + RINGER_MODE_VIBRATE, + /** + * Normal mode */ - RINGER_MODE_VIBRATE = 2, + RINGER_MODE_NORMAL, } /** @@ -142,193 +142,193 @@ declare namespace audio { * @devices * @sysCap SystemCapability.Multimedia.Audio */ - setVolume(audioType: AudioVolumeType, volume: number,callback: AsyncCallback): void; + setVolume(volumeType: AudioVolumeType, volume: number, callback: AsyncCallback): void; /** * Sets volume for a stream. This method uses a promise to return the execution result. * @devices * @sysCap SystemCapability.Multimedia.Audio */ - setVolume(audioType: AudioVolumeType, volume: number): Promise; + setVolume(volumeType: AudioVolumeType, volume: number): Promise; /** * Obtains volume of a stream. This method uses an asynchronous callback to return the execution result. * @devices * @sysCap SystemCapability.Multimedia.Audio */ - getVolume(audioType: AudioVolumeType, callback: AsyncCallback): void; + getVolume(volumeType: AudioVolumeType, callback: AsyncCallback): void; /** * Obtains the volume of a stream. This method uses a promise to return the execution result. * @devices * @sysCap SystemCapability.Multimedia.Audio */ - getVolume(audioType: AudioVolumeType): Promise; + getVolume(volumeType: AudioVolumeType): Promise; /** * Obtains the minimum volume allowed for a stream. This method uses an asynchronous callback to return the execution result. * @devices * @sysCap SystemCapability.Multimedia.Audio */ - getMinVolume(audioType: AudioVolumeType, callback: AsyncCallback): void + getMinVolume(volumeType: AudioVolumeType, callback: AsyncCallback): void; /** * Obtains the minimum volume allowed for a stream. This method uses a promise to return the execution result. * @devices * @sysCap SystemCapability.Multimedia.Audio */ - getMinVolume(audioType: AudioVolumeType): Promise; + getMinVolume(volumeType: AudioVolumeType): Promise; /** * Obtains the maximum volume allowed for a stream. This method uses an asynchronous callback to return the execution result. * @devices * @sysCap SystemCapability.Multimedia.Audio */ - getMaxVolume(audioType: AudioVolumeType, callback: AsyncCallback): void + getMaxVolume(volumeType: AudioVolumeType, callback: AsyncCallback): void; /** * Obtains the maximum volume allowed for a stream. This method uses a promise to return the execution result. * @devices * @sysCap SystemCapability.Multimedia.Audio */ - getMaxVolume(audioType: AudioVolumeType): Promise; + getMaxVolume(volumeType: AudioVolumeType): Promise; /** - * Obtains the audio devices of a specified flag. This method uses an asynchronous callback to return the execution result. - * @devices + * Sets the stream to mute. This method uses an asynchronous callback to return the execution result. * @sysCap SystemCapability.Multimedia.Audio + * @devices */ - getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback): void + mute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback): void; /** - * Obtains the audio devices with a specified flag. This method uses a promise to return the execution result. - * @devices + * Sets the stream to mute. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio */ - getDevices(deviceFlag: DeviceFlag): Promise; - /** - * 回调方式获取铃声模式。 + mute(volumeType: AudioVolumeType, mute: boolean): Promise; + /** + * Checks whether the stream is muted. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - getRingerMode(callback: AsyncCallback): void; + isMute(volumeType: AudioVolumeType, callback: AsyncCallback): void; /** - * promise方式获取铃声模式。 + * Checks whether the stream is muted. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - getRingerMode(): Promise; + isMute(volumeType: AudioVolumeType): Promise; /** - * 设置铃声模式,回调方式返回是否成功。 + * Checks whether the stream is active. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - setRingerMode(mode: AudioRingMode, callback: AsyncCallback): void; + isActive(volumeType: AudioVolumeType, callback: AsyncCallback): void; /** - * 设置铃声模式,promise方式返回是否成功。 + * Checks whether the stream is active. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - setRingerMode(mode: AudioRingMode): Promise; - /** - * 判断流是否静音,回调方式返回。 + isActive(volumeType: AudioVolumeType): Promise; + /** + * Mute/Unmutes the microphone. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - isStreamMute(volumeType: AudioVolumeType, callback: AsyncCallback): void; + setMicrophoneMute(mute: boolean, callback: AsyncCallback): void; /** - * 判断流是否静音,promise方式返回。 + * Mute/Unmutes the microphone. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - isStreamMute(volumeType: AudioVolumeType): Promise; + setMicrophoneMute(mute: boolean): Promise; /** - * 判断流是否激活,回调方式返回。 + * Checks whether the microphone is muted. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - isStreamActive(volumeType: AudioVolumeType, callback: AsyncCallback): void; + isMicrophoneMute(callback: AsyncCallback): void; /** - * 判断流是否激活,promise方式返回。 + * Checks whether the microphone is muted. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - isStreamActive(volumeType: AudioVolumeType): Promise; - /** - * 判断麦克风是否静音,回调方式返回。 + isMicrophoneMute(): Promise; + /** + * Sets the ringer mode. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - isMicrophoneMute(callback: AsyncCallback): void; + setRingerMode(mode: AudioRingMode, callback: AsyncCallback): void; /** - * 判断麦克风是否静音,promise方式返回。 + * Sets the ringer mode. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - isMicrophoneMute(): Promise; - /** - * 设置流静音,回调方式返回。 + setRingerMode(mode: AudioRingMode): Promise; + /** + * Gets the ringer mode. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - setStreamMute(volumeType: AudioVolumeType, mute: boolean, callback: AsyncCallback) : void; + getRingerMode(callback: AsyncCallback): void; /** - * 设置流静音,promise方式返回。 + * Gets the ringer mode. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - setStreamMute(volumeType: AudioVolumeType, mute: boolean): Promise; - /** - * 设置麦克风是否静音,回调方式返回。 + getRingerMode(): Promise; + /** + * Sets the audio parameter. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - setMicrophoneMute(isMute: boolean, callback: AsyncCallback): void; + setAudioParameter(key: string, value: string, callback: AsyncCallback): void; /** - * 设置麦克风是否静音,promise方式返回。 + * Sets the audio parameter. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - setMicrophoneMute(isMute: boolean): Promise; - /** - * 设备是否激活,回调方式返回。 + setAudioParameter(key: string, value: string): Promise; + /** + * Gets the audio parameter. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - isDeviceActive(deviceType: DeviceType, callback: AsyncCallback): void; + getAudioParameter(key: string, callback: AsyncCallback): void; /** - * 设备是否激活,promise方式返回。 + * Gets the audio parameter. This method uses a promise to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - isDeviceActive(deviceType: DeviceType): Promise; + getAudioParameter(key: string): Promise; /** - * 设置设备激活,回调方式返回。 - * @sysCap SystemCapability.Multimedia.Audio + * Obtains the audio devices of a specified flag. This method uses an asynchronous callback to return the execution result. * @devices + * @sysCap SystemCapability.Multimedia.Audio */ - setDeviceActive(deviceType: DeviceType, active: boolean, callback: AsyncCallback): void; + getDevices(deviceFlag: DeviceFlag, callback: AsyncCallback): void; /** - * 设置设备激活,promise方式返回。 - * @sysCap SystemCapability.Multimedia.Audio + * Obtains the audio devices with a specified flag. This method uses a promise to return the execution result. * @devices + * @sysCap SystemCapability.Multimedia.Audio */ - setDeviceActive(deviceType: DeviceType, active: boolean): Promise; - /** - * 获取音频参数,回调方式返回。 + getDevices(deviceFlag: DeviceFlag): Promise; + /** + * Activates the device. This method uses an asynchronous callback to return the execution result. + * @devices * @sysCap SystemCapability.Multimedia.Audio - * @devices */ - getAudioParameter(key: string, callback: AsyncCallback): void; + setDeviceActive(deviceType: DeviceType, active: boolean, callback: AsyncCallback): void; /** - * 获取音频参数,promise方式返回。 + * Activates the device. This method uses a promise to return the execution result. * @sysCap SystemCapability.Multimedia.Audio * @devices */ - getAudioParameter(key: string): Promise; + setDeviceActive(deviceType: DeviceType, active: boolean): Promise; /** - * 设置音频参数,回调方式返回。 + * Checks whether the device is active. This method uses an asynchronous callback to return the execution result. * @sysCap SystemCapability.Multimedia.Audio * @devices */ - setAudioParameter(key: string, value: string, callback: AsyncCallback): void; + isDeviceActive(deviceType: DeviceType, callback: AsyncCallback): void; /** - * 设置音频参数,回调方式返回。 + * Checks whether the device is active. This method uses a promise to return the execution result. * @sysCap SystemCapability.Multimedia.Audio * @devices */ - setAudioParameter(key: string, value: string): Promise; + isDeviceActive(deviceType: DeviceType): Promise; } /** diff --git a/interfaces/kits/js/audio_manager/include/audio_manager_napi.h b/interfaces/kits/js/audio_manager/include/audio_manager_napi.h index 9976ea3f98..416e59bbe2 100644 --- a/interfaces/kits/js/audio_manager/include/audio_manager_napi.h +++ b/interfaces/kits/js/audio_manager/include/audio_manager_napi.h @@ -32,8 +32,8 @@ public: ~AudioManagerNapi(); enum AudioVolumeType { - MEDIA = 1, - RINGTONE = 2 + RINGTONE = 2, + MEDIA = 3 }; enum DeviceFlag { @@ -42,6 +42,12 @@ public: ALL_DEVICES_FLAG = 3 }; + enum AudioRingMode { + RINGER_MODE_SILENT = 0, + RINGER_MODE_VIBRATE, + RINGER_MODE_NORMAL + }; + static napi_value Init(napi_env env, napi_value exports); private: -- Gitee