The audio source separation function of Huawei Audio Editing Service supports human voice and human voice, human voice and accompaniment, human voice and instrument elements (that is, all instruments are extracted separately).
First, let's take a look at a demo of the "Dream it Possible" accompaniment extracted using Huawei's audio editing service.
(To view the example effect, please visit:
https://developer.huawei.com/consumer/cn/forum/topic/0202660512438950947?fid=18?ha_source=hms1)
Next, I will show you how to achieve the effect of accompaniment separation in Demo↓↓↓:
In the first step, we need to prepare the song material that we want to get the accompaniment.
MP3 format is the best. For conversion of other audio formats, please refer to the second step 2.4 for conversion.
If there is only video format, please refer to the second step 2.5 for audio extraction.
The second step is to access Huawei's audio editing service.
Development combat
Development preparation
1.1 Configure the Maven warehouse address in the project-level build.gradle
buildscript {
repositories {
google()
jcenter()
// 配置HMS Core SDK的Maven仓地址。
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
// 增加agcp插件配置。
classpath 'com.huawei.agconnect:agcp:1.4.2.300'
}
}
allprojects {
repositories {
google()
jcenter()
// 配置HMS Core SDK的Maven仓地址。
maven {url 'https://developer.huawei.com/repo/'}
}
}
1.2 File header increase configuration
apply plugin: 'com.huawei.agconnect'
1.3 Configure SDK dependencies in application-level build.gradle
dependencies{
implementation 'com.huawei.hms:audio-editor-ui:{version}'
}
4 Apply for the following permissions in the AndroidManifest.xml file
<!--震动权限--> <uses-permission android:name="android.permission.VIBRATE" /> <!--麦克风权限--> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <!--写存储权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--读存储权限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--网络权限--> <uses-permission android:name="android.permission.INTERNET" /> <!--网络状态权限--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--网络状态变化权限--> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
Code development
2.1 Create a custom activity interface for your application to select audio and return the audio file path to the audio editing SDK in the following way.
// 将音频文件路径List返回给音频编辑页面
private void sendAudioToSdk() {
// 获取到的音频文件路径 filePath
String filePath = "/sdcard/AudioEdit/audio/music.aac";
ArrayList<String> audioList = new ArrayList<>();
audioList.add(filePath);
// 将音频文件路径返回给音频编辑页面
Intent intent = new Intent();
// 使用sdk提供的HAEConstant.AUDIO_PATH_LIST
intent.putExtra(HAEConstant.AUDIO_PATH_LIST, audioList);
// 使用sdk提供的HAEConstant.RESULT_CODE为结果CODE
this.setResult(HAEConstant.RESULT_CODE, intent);
finish();
}
2.2 When importing audio on the UI interface, the SDK will send an intent with an action value of com.huawei.hms.audioeditor.chooseaudio to jump to the activity. Therefore, the registration form in the activity "AndroidManifest.xml" is as follows.
<activity android:name="Activity ">
<intent-filter>
<action android:name="com.huawei.hms.audioeditor.chooseaudio"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
2.3 Start the audio editing page, click "Add Audio", the SDK will actively call the activity defined in 2.1. After adding audio, you can perform audio editing, adding special effects and other operations, and export the edited audio after completion.
HAEUIManager.getInstance().launchEditorActivity(this);
2.4 If your original song material is not in MP3 format, this step will complete the audio format conversion.
调用transformAudioUseDefaultPath接口进行音频格式转换,转换后的音频文件导出到默认路径。
// 音频格式转换接口
HAEAudioExpansion.getInstance().transformAudioUseDefaultPath(context,inAudioPath, audioFormat, new OnTransformCallBack() {
// 进度回调(0-100)
@Override
public void onProgress(int progress) {
}
// 转换失败
@Override
public void onFail(int errorCode) {
}
// 转换成功
@Override
public void onSuccess(String outPutPath) {
}
// 取消转换
@Override
public void onCancel() {
}
});
// 取消转换任务接口
HAEAudioExpansion.getInstance().cancelTransformAudio();
Call the transformAudio interface to convert the audio format, and export the converted audio file to the target path.
// 音频格式转换接口
HAEAudioExpansion.getInstance().transformAudio(context,inAudioPath, outAudioPath, new OnTransformCallBack(){
// 进度回调(0-100)
@Override
public void onProgress(int progress) {
}
// 转换失败
@Override
public void onFail(int errorCode) {
}
// 转换成功
@Override
public void onSuccess(String outPutPath) {
}
// 取消转换
@Override
public void onCancel() {
}
});
// 取消转换任务接口
HAEAudioExpansion.getInstance().cancelTransformAudio();
2.5 If your material is a video, you can call the extractAudio interface to extract audio, extract audio files from the video and export them to a specified directory.
// outAudioDir提取出的音频保存的文件夹路径,非必填
// outAudioName提取出的音频名称,不带后缀,非必填
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.");
}
});
// 取消音频提取任务接口
HAEAudioExpansion.getInstance().cancelExtractAudio();
2.6 Call getInstruments and startSeparationTasks interface to extract accompaniment.
// 获取提取伴奏类型ID,后面将此ID传给接口
HAEAudioSeparationFile haeAudioSeparationFile = new HAEAudioSeparationFile();
haeAudioSeparationFile.getInstruments(new SeparationCloudCallBack<List<SeparationBean>>() {
@Override
public void onFinish(List<SeparationBean> response) {
// 返回的数据,包括伴奏的类型ID
}
@Override
public void onError(int errorCode) {
// 失败返回
}
});
// 设置要提取的伴奏参数
List instruments = new ArrayList<>();
instruments.add(“伴奏id”);
haeAudioSeparationFile.setInstruments(instruments);
// 开始进行伴奏分离
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() {}
});
// 取消分离任务
haeAudioSeparationFile.cancel();
Finally, we can get the accompaniment of the song we want.
If you want to achieve the demo effect, you can use video editing software to add pictures and text lyrics for synthesis.
Learn more >>
Visit the official website of Huawei Developer Alliance Audio Editing Service
Obtain the development audio editing service guide document
Huawei audio editing service open source warehouse address: GitHub , Gitee
Huawei HMS Core official forum
To solve integration problems, please go to Stack Overflow
and learn about the latest technology of HMS Core for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。