飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

HMS Core如何实现音源分离功能

时间:2021-12-29  作者:电脑狂魔  

HMS Core 音频编辑器套件提供音源分离功能,可以将人声、人声与伴奏、人声与乐器声音分离。下图显示了从Dream it possible 中分离出来的伴奏。 

域名

让我们看看如何实现这个功能。 

步骤 1 - 准备用于音频源分离的文件

建议使用 MP3 音频文件。如果这不可能,请按照步骤 2 中的说明将音频文件转换为 MP3 文件。如果要分离的伴奏在视频文件中怎么办?不用担心。只需参考第 2 步中的说明,先提取视频的音频即可。

第 2 步 - 集成音频编辑器套件

开发实践:准备

1.在项目级的域名le文件中配置Maven仓库地址。 

buildscript {
    repositories {
        google()
        jcenter()
        // Configure the Maven repository address for the HMS Core SDK.
        maven {url 'https://域名/repo/'}
    }
    dependencies {
        ...
        // Add the AppGallery Connect plugin configuration.
        classpath '域名nnect:agcp:1.域名'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        // Configure the Maven repository address for the HMS Core SDK.
        maven {url 'https://域名/repo/'}
    }
}

2.在文件头的声明下添加如下配置。 

apply plugin: '域名nnect'

3. 在应用级域名le文件中添加对 Audio Editor SDK 的构建依赖。 

dependencies{
    implementation '域名:audio-editor-ui:{version}'
}

4、在域名文件中申请如下权限: 

<!-- Vibrate -->
<uses-permission android:name="域名ATE" />
<!-- Microphone -->
<uses-permission android:name="域名RD_AUDIO" />
<!-- Write into storage -->
<uses-permission android:name="域名E_EXTERNAL_STORAGE" />
<!-- Read from storage -->
<uses-permission android:name="域名_EXTERNAL_STORAGE" />
<!-- Connect to Internet -->
<uses-permission android:name="域名RNET" />
<!-- Obtain the network status -->
<uses-permission android:name="域名SS_NETWORK_STATE" />
<!-- Obtain the changed network connectivity state -->
<uses-permission android:name="域名GE_NETWORK_STATE" />

代码开发

1. 创建您的应用程序的自定义活动并将其用于选择一个或多个音频文件。通过以下方式将它们的路径返回到音频编辑器 SDK 

// 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/域名";
    ArrayList<String> audioList = new ArrayList<>();
    域名(filePath);
    // Return the paths to the audio editing screen.
    Intent intent = new Intent();
    // Use 域名O_PATH_LIST provided by the Audio Editor SDK.
    域名xtra(域名O_PATH_LIST, audioList);
    // Use 域名LT_CODE provided by the Audio Editor SDK as the result code.
    域名esult(域名LT_CODE, intent);
    finish();
}

2. 在域名文件中注册活动,如以下代码所述。当您选择导入选定的音频文件时,SDK 会发送一个 action 值为域名.域名seaudio 的 Intent跳转到该 Activity。 

<activity android:name="Activity ">
<intent-filter>
<action android:name="域名.域名seaudio"/>
<category android:name="域名域名ULT"/>
</intent-filter>
</activity>

3. 启动音频编辑屏幕。当您点击添加音频 时,SDK 将自动调用之前定义的活动。然后您将能够编辑音频并为音频添加特殊效果。完成这些操作后,就可以导出编辑好的音频了。 

域名nstance().launchEditorActivity(this);

4. 将非 MP3 的音频文件格式转换为 MP3(可选)

调用transformAudioUseDefaultPath转换音频格式,并将转换后的音频保存到默认目录。

// Convert the audio format.
域名nstance().transformAudioUseDefaultPath(context,inAudioPath, audioFormat, new OnTransformCallBack() {
   // Called to receive the progress which ranges from 0 to 100.
   @Override
   public void onProgress(int progress) {
   }
   // Called when the conversion fails.
   @Override
   public void onFail(int errorCode) {
   }
   // Called when the conversion succeeds.
   @Override
   public void onSuccess(String outPutPath) {
   }
   // Called when the conversion is canceled.
   @Override
   public void onCancel() {
   }
   });
// Cancel format conversion.
域名nstance().cancelTransformAudio();

调用transformAudio进行音频格式转换,并将转换后的音频保存到指定目录。

// Convert the audio format.
域名nstance().transformAudio(context,inAudioPath, outAudioPath, new OnTransformCallBack(){
    // Called to receive the progress which ranges from 0 to 100.
    @Override
    public void onProgress(int progress) {
    }
    // Called when the conversion fails.
    @Override
    public void onFail(int errorCode) {
    }
    // Called when the conversion succeeds.
    @Override
    public void onSuccess(String outPutPath) {
    }
    // Called when the conversion is canceled.
    @Override
    public void onCancel() {
    }
    });
// Cancel format conversion.
域名nstance().cancelTransformAudio();

5. 调用extractAudio将包含待分离伴奏的视频中的音频提取到指定目录。(选修的) 

// outAudioDir (optional): path of the directory for storing the extracted audio.
// outAudioName (optional): name of the extracted audio, which does not contain the file name extension.
域名nstance().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.
域名nstance().cancelExtractAudio();

6、调用getInstruments和startSeparationTasks进行音源分离。

// Obtain the accompaniment ID using getInstruments and pass the ID to startSeparationTasks.
HAEAudioSeparationFile haeAudioSeparationFile = new HAEAudioSeparationFile();
域名nstruments(new SeparationCloudCallBack<List<SeparationBean>>() {
    @Override
public void onFinish(List<SeparationBean> response) {
// Called to receive the separation data including the accompaniment ID.
}
    @Override
    public void onError(int errorCode) {
        // Called when an error occurs during separation.
}
});
// Set the parameter for separation.
List instruments = new ArrayList<>();
域名 ("accompaniment ID")
域名nstruments(instruments);
// Start separating.
域名tSeparationTasks(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 audio source separation.
域名el();

结论

完成这些步骤后,您就可以得到您想要的伴奏了。要创建类似于演示的内容,您可以使用视频编辑程序将伴奏与图像和歌词进行合成。


湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。