DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert Files to Thumbnail Images in Java
  • Understanding Floating-Point Precision Issues in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Create a Go CLI Tool That Converts Markdowns to PDF Files

Trending

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • How to Convert Between PDF and TIFF in Java
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025

How to Convert Audio From 2D to 3D

Learn how to convert audio to 3D, then implement it in an Android app.

By 
Jackson Jiang user avatar
Jackson Jiang
DZone Core CORE ·
Jan. 22, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.0K Views

Join the DZone community and get the full member experience.

Join For Free

Immersive audio is becoming an increasingly important factor for enhancing user experience in the music, gaming, and audio/video editing fields. The spatial audio function is ideal for meetings, sports rehabilitation, and particularly for exhibitions, as it helps deliver a more immersive experience. For users who suffer from visual impairments, the function can serve as a helpful guide.

In this article, I am going to reuse the sample code on this GitHub repo. I will implement spatial audio functions in my Android app and deliver the 3D surround sound. 

Preparations

Prepare the audio for 2D-to-3D conversion, which is better as an MP3 file. If not, follow the instructions specified later to convert the format to MP3 first. If the audio is part of a video file, just extract the audio first by referring to the instructions described later.

1. Configure the Maven repository address in the project-level build.gradle file.

Groovy
buildscript {
    repositories {
        google()
        jcenter()
        // Configure the Maven repository address for the HMS Core SDK.
        maven {url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        ...
        // Add the AppGallery Connect plugin configuration.
        classpath 'com.huawei.agconnect:agcp:1.4.2.300'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        // Configure the Maven repository address for the HMS Core SDK.
        maven {url 'https://developer.huawei.com/repo/'}
    }
}

Add the following configuration under the declaration in the file header:

Groovy
apply plugin: 'com.huawei.agconnect'

2. Add the build dependency on the Audio Editor SDK in the app-level build.gradle file.

Groovy
dependencies{
    implementation 'com.huawei.hms:audio-editor-ui:{version}'
}

3. Apply for the following permissions in the AndroidManifest.xml file:

XML
<!-- Vibrate -->
<uses-permission android:name="android.permission.VIBRATE" ></uses>
<!-- Microphone -->
<uses-permission android:name="android.permission.RECORD_AUDIO" ></uses>
<!-- Write into storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses>
<!-- Read from storage -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" ></uses>
<!-- Connect to the Internet -->
<uses-permission android:name="android.permission.INTERNET" ></uses>
<!-- Obtain the network status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" ></uses>
<!-- Obtain the changed network connectivity state -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" ></uses>

Code Development

1. Create the app's custom activity for selecting one or more audio files. Return their paths to the SDK.

Java
// Return the audio file paths to the audio editing screen.
private void sendAudioToSdk() {
    // Set filePath to the obtained audio file path.
    String filePath = "/sdcard/AudioEdit/audio/music.aac";
    ArrayList<String> audioList = new ArrayList<>();
    audioList.add(filePath);
    // Return the path to the audio editing screen.
    Intent intent = new Intent();
    // Use HAEConstant.AUDIO_PATH_LIST provided by the SDK.
    intent.putExtra(HAEConstant.AUDIO_PATH_LIST, audioList);
    // Use HAEConstant.RESULT_CODE provided by the SDK as the result code.
    this.setResult(HAEConstant.RESULT_CODE, intent);
    finish();
}

2. Register the activity in the AndroidManifest.xml file as described in the following code. When you choose to import the selected audio files, the SDK will send an intent whose action value is com.huawei.hms.audioeditor.chooseaudio to jump to the activity.

XML
<activity android:name="Activity "> 
<intent-filter> 
<action android:name="com.huawei.hms.audioeditor.chooseaudio"></action> 
<category android:name="android.intent.category.DEFAULT"></category> 
</intent-filter> 
</activity>

Launch the audio editing screen. When you tap Add audio, the SDK will automatically call the activity defined earlier. Then operations like editing and adding special effects can be performed on the audio. After such operations are complete, the edited audio can be exported.

Java
HAEUIManager.getInstance().launchEditorActivity(this);

3.    Convert the file format to MP3. (Optional) 

Call transformAudioUseDefaultPath to convert the format and save the converted audio to the default directory.

Java
// Convert the audio format.
HAEAudioExpansion.getInstance().transformAudioUseDefaultPath(context,inAudioPath, audioFormat, new OnTransformCallBack() {
    // Callback when the progress is received. The value ranges from 0 to 100.
    @Override
    public void onProgress(int progress) {
    }
    // Callback when the conversion fails.
    @Override
    public void onFail(int errorCode) {
    }
    // Callback when the conversion succeeds.
    @Override
    public void onSuccess(String outPutPath) {
    }
    // Callback when the conversion is canceled.
    @Override
    public void onCancel() {
    }
    });
// Cancel format conversion.
HAEAudioExpansion.getInstance().cancelTransformAudio();


Call transformAudio to convert audio and save the converted audio to a specified directory.

Java
// Convert the audio format.
HAEAudioExpansion.getInstance().transformAudio(context,inAudioPath, outAudioPath, new OnTransformCallBack(){
    // Callback when the progress is received. The value ranges from 0 to 100.
    @Override
    public void onProgress(int progress) {
    }
    // Callback when the conversion fails.
    @Override
    public void onFail(int errorCode) {
    }
    // Callback when the conversion succeeds.
    @Override
    public void onSuccess(String outPutPath) {
    }
    // Callback when the conversion is canceled.
    @Override
    public void onCancel() {
    }
    });
// Cancel format conversion.
HAEAudioExpansion.getInstance().cancelTransformAudio();

 

Call extractAudio to extract audio from a video to a specified directory.(Optional)

Java
// outAudioDir (optional): directory path for storing extracted audio.
// outAudioName (optional): name of extracted audio, which does not contain the file name extension.
HAEAudioExpansion.getInstance().extractAudio(context,inVideoPath,outAudioDir, outAudioName,new AudioExtractCallBack() {
    @Override
    public void onSuccess(String audioPath) {
    Log.d(TAG, "ExtractAudio onSuccess : " + audioPath);
    }
    @Override
    public void onProgress(int progress) {
    Log.d(TAG, "ExtractAudio onProgress : " + progress);
    }
    @Override
    public void onFail(int errCode) {
    Log.i(TAG, "ExtractAudio onFail : " + errCode);
    }
    @Override
    public void onCancel() {
    Log.d(TAG, "ExtractAudio onCancel.");
    }
    });
// Cancel audio extraction.
HAEAudioExpansion.getInstance().cancelExtractAudio();


Call getInstruments and startSeparationTasks for audio source separation.

 
// Obtain the accompaniment ID using getInstruments and pass the ID to startSeparationTasks.
HAEAudioSeparationFile haeAudioSeparationFile = new HAEAudioSeparationFile();
haeAudioSeparationFile.getInstruments(new SeparationCloudCallBack<List<SeparationBean>>() {
    @Override
public void onFinish(List<SeparationBean> response) {
// Callback when the separation data is received. The data includes the accompaniment ID.
}
    @Override
    public void onError(int errorCode) {
        // Callback when the separation fails.
}
});
// Set the parameter for accompaniment separation.
List instruments = new ArrayList<>();
instruments.add("accompaniment ID");
haeAudioSeparationFile.setInstruments(instruments);
// Start separating.
haeAudioSeparationFile.startSeparationTasks(inAudioPath, outAudioDir, outAudioName, new AudioSeparationCallBack() {
    @Override
    public void onResult(SeparationBean separationBean) { }
    @Override
    public void onFinish(List<SeparationBean> separationBeans) {}
    @Override
    public void onFail(int errorCode) {}
    @Override
    public void onCancel() {}
});
// Cancel separating.
haeAudioSeparationFile.cancel();


Call applyAudioFile to apply spatial audio.

 
// Apply spatial audio.
// Fixed position mode.
HAESpaceRenderFile haeSpaceRenderFile = new HAESpaceRenderFile(SpaceRenderMode.POSITION);
haeSpaceRenderFile.setSpacePositionParams(
                            new SpaceRenderPositionParams(x, y, z));
// Dynamic rendering mode.
HAESpaceRenderFile haeSpaceRenderFile = new HAESpaceRenderFile(SpaceRenderMode.ROTATION);
haeSpaceRenderFile.setRotationParams( new SpaceRenderRotationParams(
                                    x, y, z, surroundTime, surroundDirection));
// Extension.
HAESpaceRenderFile haeSpaceRenderFile = new HAESpaceRenderFile(SpaceRenderMode.EXTENSION);
haeSpaceRenderFile.setExtensionParams(new SpaceRenderExtensionParams(radiusVal, angledVal));
// Call the API.
haeSpaceRenderFile.applyAudioFile(inAudioPath, outAudioDir, outAudioName, callBack);
// Cancel applying spatial audio.
haeSpaceRenderFile.cancel();


After completing these steps, you can now implement the 2D-to-3D conversion effect for your app.

Convert (command)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert Files to Thumbnail Images in Java
  • Understanding Floating-Point Precision Issues in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Create a Go CLI Tool That Converts Markdowns to PDF Files

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!