DEV Community

HarmonyOS
HarmonyOS

Posted on

How to Maximize Volume When Playing Audio Stream with OHAudio

Read the original article:How to Maximize Volume When Playing Audio Stream with OHAudio

Requirement Description

How to maximize the volume when playing an audio stream using OHAudio?

Background Knowledge

To effectively control audio volume in OHAudio, we should know these key concepts:

  • Volume Normalization: OHAudio uses a normalized volume range (0.0-1.0) where 1.0 represents maximum system volume without clipping
  • Output Device Priority: The system follows a specific device selection hierarchy (external devices > app-specified defaults > system defaults)
  • PCM Manipulation: Raw audio data can be amplified through scalar multiplication, but requires careful clipping prevention
  • Stream Type Considerations: Certain audio stream types (like VoIP) have special device routing capabilities

Implementation Steps

To maximize the volume when playing an audio stream with OHAudio, you can use the following three methods:

  1. Set Volume via OH_AudioRenderer_SetVolume: Use this API to directly adjust the audio stream volume (range: 0.0 to 1.0).
  2. Force Speaker Output via OH_AudioRenderer_SetDefaultOutputDevice: For voice/VoIP streams, set the default output device to the built-in speaker.
  3. Manually Amplify PCM Data: Apply scalar multiplication to the raw PCM stream for volume boosting.

Code Snippet / Configuration

- Developers can use the OH_AudioRenderer_SetVolume interface to set the current audio stream volume value, where 1.0 is the maximum value. Example code:

// Volume value to set, range is [0.0, 1.0].
float volume = 0.5f;

// Set the current audio stream volume value.
OH_AudioStream_Result OH_AudioRenderer_SetVolume(audioRenderer, volume);
Enter fullscreen mode Exit fullscreen mode

- When the audio stream type (OH_AudioStream_Usage) is for voice messages, VoIP voice calls, or VoIP video calls, you can use OH_AudioRenderer_SetDefaultOutputDevice() to set the built-in speaker as the output device for maximum volume.

// Set the built-in speaker as the output device.
OH_AudioStream_Result OH_AudioRenderer_SetDefaultOutputDevice(audioRenderer, AUDIO_DEVICE_TYPE_SPEAKER);
Enter fullscreen mode Exit fullscreen mode

- The PCM stream itself can be amplified by scalar multiplication. Example code:

// buf: Pointer to the start address of the audio data block to adjust. size: Length. uRepeat: Repeat count (usually 1). vol: Gain multiplier (can be less than 1).
void RaiseVolume(char* buf, UINT32 size, UINT32 uRepeat, double vol)  
{  
    if (!size) {  
        return;  
    }  
    for (int i = 0; i < size; i += 2) {  
        // Set bounds based on PCM bit depth (16-bit PCM example here).
        signed long minData = -0x8000; 
        signed long maxData = 0x7FFF;
        // Combine a 16-bit sample.
        signed short wData = buf[i + 1];  
        wData = MAKEWORD(buf[i], buf[i + 1]);  
        signed long dwData = wData;  
        // Apply scalar multiplication and clamp values.
        for (int j = 0; j < uRepeat; j++) {  
            dwData = dwData * vol;  
            if (dwData < -0x8000){  
                dwData = -0x8000;  
            }  
            else if (dwData > 0x7FFF) {  
                dwData = 0x7FFF;  
            }  
        }  
        wData = LOWORD(dwData);  
        // Save the processed data.
        buf[i] = LOBYTE(wData);  
        buf[i + 1] = HIBYTE(wData);  
    }  
}
Enter fullscreen mode Exit fullscreen mode

Limitations or Considerations

  • Note: The OH_AudioRenderer_SetDefaultOutputDevice() interface can be called at any time after the AudioRenderer is created.
  • The system records the default built-in output device set by the application.
  • If an external device (e.g., Bluetooth headphones/wired headphones) is connected when playback starts, the system prioritizes output through the external device; otherwise, it follows the default built-in output device set by the application.
  • This example supports API Version 18 Release and above.
  • This example supports HarmonyOS 5.1.0 Release SDK and above.
  • This example requires DevEco Studio 5.1.0 Release and above for compilation and runtime.

Related Documents or Links

Written by Can Kankaya

Top comments (0)