1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 【Android 高性能音频】Oboe 开发流程 ( Oboe 完整代码示例 )

【Android 高性能音频】Oboe 开发流程 ( Oboe 完整代码示例 )

时间:2020-03-09 16:37:38

相关推荐

【Android 高性能音频】Oboe 开发流程 ( Oboe 完整代码示例 )

文章目录

一、GitHub 地址二、Oboe C++ 代码三、日志封装四、Activity 代码五、AndroidManifest.xml 配置文件六、CMakeLists.txt 构建脚本七、应用 build.gradle 构建脚本八、工程 build.gradle 构建脚本九、Gradle 配置脚本 gradle-wrapper.properties十、配置总结

Oboe GitHub 主页 :GitHub/Oboe

① 简单使用 :Getting Started

② Oboe 全指南 :Full Guide To Oboe

③ Oboe API 参考 :API reference

④ Android 音频框架发展 :Android audio history

在 【Android 高性能音频】Oboe 开发流程 ( 导入 Oboe 库 | 使用预构建的二进制库和头文件 | 编译 Oboe 源码 ) 博客中介绍了 如何导入 Oboe 函数库到项目中 , 本博客中在导入 Oboe 函数库的基础上 , 进行 Oboe 播放器功能开发 ;

在 【Android 高性能音频】Oboe 开发流程 ( 包含头 Oboe 头文件 | 创建音频流 | 设置音频流 | 音频流回调类 AudioStreamCallback ) 介绍了如何创建 AudioStreamBuilder , 以及 创建 AudioStreamCallback 回调 ;

在 【Android 高性能音频】Oboe 开发流程 ( 创建并设置 AudioStreamCallback 对象 | 打开 Oboe 音频流 | 日志封装 logging_macros.h ) 博客中介绍了 设置 AudioStreamCallback 对象 , 打开 Oboe 音频流 操作 , 以及 Google 官方提供的日志封装有文件 ;

在 【Android 高性能音频】Oboe 开发流程 ( 检查 Oboe 音频流属性 | 开始播放 | 停止播放 | 关闭 Oboe 音频流 | 重新配置 Oboe 音频流属性 ) 博客中介绍了 如何开始 Oboe 音频流播放 , 以及 播放完毕后的收尾工作 ;

本篇博客中 , 完整的完成一个 Oboe 播放器的播放操作 ; 代码中有详细的注释 ;

一、GitHub 地址

GitHub 地址 :/han120/OboeDemo

二、Oboe C++ 代码

#include <jni.h>#include <string>#include <oboe/Oboe.h>#include "logging_macros.h"// 这部分变量是采样相关的 , 与 Oboe 操作无关// 声道个数 , 2 代表立体声static int constexpr kChannelCount = 2;static int constexpr kSampleRate = 48000;// Wave params, these could be instance variables in order to modify at runtimestatic float constexpr kAmplitude = 0.5f;// 频率static float constexpr kFrequency = 440;// PI 圆周率static float constexpr kPI = M_PI;// 2 PI 两倍圆周率static float constexpr kTwoPi = kPI * 2;// 每次累加的采样值static double constexpr mPhaseIncrement = kFrequency * kTwoPi / (double) kSampleRate;// 追踪当前波形位置float mPhase = 0.0;// Oboe 音频流回调类class MyCallback : public oboe::AudioStreamCallback {public:oboe::DataCallbackResultonAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames) {// 需要生成 AudioFormat::Float 类型数据 , 该缓冲区类型也是该类型// 生产者需要检查该格式// oboe::AudioStream *audioStream 已经转换为适当的类型// 获取音频数据缓冲区auto *floatData = static_cast<float *>(audioData);// 生成正弦波数据for (int i = 0; i < numFrames; ++i) {float sampleValue = kAmplitude * sinf(mPhase);for (int j = 0; j < kChannelCount; j++) {floatData[i * kChannelCount + j] = sampleValue;}mPhase += mPhaseIncrement;if (mPhase >= kTwoPi) mPhase -= kTwoPi;}LOGI("回调 onAudioReady");return oboe::DataCallbackResult::Continue;}};// 创建 MyCallback 对象MyCallback myCallback = MyCallback();// 声明 Oboe 音频流oboe::ManagedStream managedStream = oboe::ManagedStream();extern "C" JNIEXPORT jstring JNICALLJava_kim_hsl_oboedemo_MainActivity_stringFromJNI(JNIEnv* env,jobject /* this */) {// 1. 音频流构建器oboe::AudioStreamBuilder builder = oboe::AudioStreamBuilder();// 设置音频流方向builder.setDirection(oboe::Direction::Output);// 设置性能优先级builder.setPerformanceMode(oboe::PerformanceMode::LowLatency);// 设置共享模式 , 独占builder.setSharingMode(oboe::SharingMode::Exclusive);// 设置音频采样格式builder.setFormat(oboe::AudioFormat::Float);// 设置声道数 , 单声道/立体声builder.setChannelCount(oboe::ChannelCount::Stereo);// 设置采样率builder.setSampleRate(48000);// 设置回调对象 , 注意要设置 AudioStreamCallback * 指针类型builder.setCallback(&myCallback);// 2. 通过 AudioStreamBuilder 打开 Oboe 音频流oboe::Result result = builder.openManagedStream(managedStream);LOGI("openManagedStream result : %s", oboe::convertToText(result));// 3. 开始播放result = managedStream->requestStart();LOGI("requestStart result : %s", oboe::convertToText(result));// 返回数据到std::string hello = "Oboe Test " + std::to_string(static_cast<int>(oboe::PerformanceMode::LowLatency)) + " Result : " + oboe::convertToText(result);return env->NewStringUTF(hello.c_str());}

三、日志封装

Google 中提供了一个很好的 log 封装头文件 , logging_macros.h , 很多官方示例中都使用这个头文件 ;

#ifndef __SAMPLE_ANDROID_DEBUG_H__#define __SAMPLE_ANDROID_DEBUG_H__#include <android/log.h>#if 1#ifndef MODULE_NAME#define MODULE_NAME "octopus"#endif#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, MODULE_NAME, __VA_ARGS__)#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, MODULE_NAME, __VA_ARGS__)#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, MODULE_NAME, __VA_ARGS__)#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,MODULE_NAME, __VA_ARGS__)#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,MODULE_NAME, __VA_ARGS__)#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,MODULE_NAME, __VA_ARGS__)#define ASSERT(cond, ...) if (!(cond)) {__android_log_assert(#cond, MODULE_NAME, __VA_ARGS__);}#else#define LOGV(...)#define LOGD(...)#define LOGI(...)#define LOGW(...)#define LOGE(...)#define LOGF(...)#define ASSERT(cond, ...)#endif#endif // __SAMPLE_ANDROID_DEBUG_H__

四、Activity 代码

package kim.hsl.oboedemoimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)sample_text.text = stringFromJNI()}external fun stringFromJNI(): Stringcompanion object {init {System.loadLibrary("native-lib")}}}

五、AndroidManifest.xml 配置文件

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="kim.hsl.oboedemo"><uses-feature android:name="android.hardware.audio.output" android:required="true" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

六、CMakeLists.txt 构建脚本

cmake_minimum_required(VERSION 3.4.1)add_library(native-libSHAREDnative-lib.cpp )find_package (oboe REQUIRED CONFIG)target_link_libraries(native-liboboe::oboelog )

七、应用 build.gradle 构建脚本

apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'android {compileSdkVersion 29buildToolsVersion "30.0.2"defaultConfig {applicationId "kim.hsl.oboedemo"minSdkVersion 18targetSdkVersion 29versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"externalNativeBuild {cmake {cppFlags ""arguments "-DANDROID_STL=c++_shared"}}}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}externalNativeBuild {cmake {path "src/main/cpp/CMakeLists.txt"version "3.10.2"}}buildFeatures {prefab true}}dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"implementation 'androidx.appcompat:appcompat:1.2.0'implementation 'androidx.core:core-ktx:1.3.2'implementation 'androidx.constraintlayout:constraintlayout:2.0.2'testImplementation 'junit:junit:4.12'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'implementation 'com.google.oboe:oboe:1.4.3'}

八、工程 build.gradle 构建脚本

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {ext.kotlin_version = '1.3.71'repositories {google()jcenter()}dependencies {classpath 'com.android.tools.build:gradle:4.1.0'classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"}}allprojects {repositories {google()jcenter()}}task clean(type: Delete) {delete rootProject.buildDir}

九、Gradle 配置脚本 gradle-wrapper.properties

#Tue Oct 13 10:20:09 CST distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdistributionUrl=https\:///distributions/gradle-6.6.1-bin.zip

十、配置总结

配置总结 :

Android Studio 版本 :4.1Gradle 版本 :6.6.1Gradle 插件版本 :4.1.0

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。