当前位置:   article > 正文

鸿蒙HarmonyOS项目实战开发-调频声波App

鸿蒙HarmonyOS项目实战开发-调频声波App

概述

HarmonyOS项目实战将通过一个实战项目来学习一些HarmonyOS的开发知识。
本App通过手机扬声器发出有规律的声波,包含正弦波、方波、三角波、锯齿波,并可以调节声波频率。可以用于清理手机扬声器,或者测试听力年龄(如果你能听到 15000 赫兹声音的耳朵年龄小于 40 岁;听到 19000 赫兹的年龄为 20 岁以下。)

开发环境:

Windows 11
DevEco Studio 4.0 Release
Build Version: 4.0.0.600, built on October 17, 2023

运行环境:

华为畅享50Pro
HarmonyOS 4.0 API9

App界面

界面中央为声波频率,频率左右加减按钮可以加减频率数值。点击界面底部播放/停止按钮可以控制声波的播放/停止

HarmonyOS项目实战:调频声波App(一)概述-鸿蒙开发者社区

前置知识

由于HarmonyOS和Openharmony的纷繁复杂的关系,本文的参考资料取自Openharmony3.2的官方文档,同样适用于HarmonyOS 4.0。没有比官方文档更全面的参考资料了,所有的知识基本都能在其中找到
本教程假设您已经学会如何创建一个HarmonyOS项目
并了解基本的ArkTS布局语法状态管理
本App涉及音频播放,使用AudioRenderer播放声音


UI布局

  1. 首先我们实现频率调整的模块

HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区

  1. Row() {
  2. Button("-")
  3. .onClick(async event => {
  4. const newValue = this.frequency - this.step // 1. 把当前的频率减掉预设的步进
  5. this.frequency = Math.max(newValue, 0) // 2. 控制频率大于0
  6. this.updateFrequency() // 3. 让播放器更新频率
  7. })
  8. .fontSize(60)
  9. .fontColor(this.mainColor)
  10. .backgroundColor("opaque")
  11. Text(`${this.frequency} Hz`)
  12. .fontSize(50)
  13. .fontWeight(FontWeight.Bold)
  14. .fontColor(this.mainColor)
  15. Button("+")
  16. .onClick(async event => {
  17. const newValue = this.frequency + this.step // 4. 把当前的频率增加预设的步进
  18. this.frequency = Math.min(newValue, 30000) // 5. 控制频率小于三万
  19. this.updateFrequency() // 6. 让播放器更新频率
  20. })
  21. .fontSize(60)
  22. .fontColor(this.mainColor)
  23. .backgroundColor("opaque")
  24. }
  25. .margin({ top: "30%" })
  1. 频率下方加入一些使用提示

    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区

  1. Text("上下滑动屏幕\n以调整频率")
  2. .fontColor(this.subtitleColor)
  3. .textAlign(TextAlign.Center)
  4. .margin({ top: 20 })
  5. Text(this.readmeRecord ? "使用说明" : "使用必读!")
  6. .fontColor(this.readmeRecord ? "#2A1EB1" : Color.Red)
  7. .fontSize(this.readmeRecord ? 16 : 24)
  8. .margin({ top: 20 })
  9. .onClick(() => {
  10. router.pushUrl({ url: 'pages/ReadmePage' }) // 1. 跳转readme界面
  11. this.readmeRecord = true // 2. 首次使用的时候会使跳转按钮更显眼,跳转过以后就恢复正常UI。用一个state变量来控制显示状态
  12. preferences.getPreferences(getContext(this), "default").then((preference) => {
  13. preference.put("readmeRecord", true) // 3. 记录到preference
  14. preference.flush()
  15. })
  16. })
  1. 界面底部的播放/停止按钮

    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区

  1. Button(this.playing ? "停止" : "播放")
  2. .fontColor(this.bgColor)
  3. .fontSize(30)
  4. .height(60)
  5. .backgroundColor(this.mainColor)
  6. .width("100%")
  7. .type(ButtonType.Normal)
  8. .onClick(() => {
  9. this.playing ? this.stop() : this.play()
  10. this.playing = !this.playing
  11. })

至此,软件的基本功能就架设完成了。接下来还可以加一点实用功能。

  1. 选择波形。由于没有找到类似iOS中的segment组件,这里直接用Text来做手动布局。

    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区

  1. @Builder
  2. waveTypeSelector() {
  3. Row() {
  4. ForEach(this.waveOptions, (item: string, index: number) => {
  5. Image(index === this.index ? item[0] : item[1])
  6. .width(50)
  7. .height(50)
  8. .backgroundColor(index === this.index ? this.selectedBgColor : this.mainColor)
  9. .padding(2)
  10. .borderRadius({
  11. topLeft: index === 0 ? 20 : 0, // 1. 第一个选项左边做圆角
  12. bottomLeft: index === 0 ? 20 : 0,
  13. topRight: index === this.waveOptions.length - 1 ? 20 : 0, // 2. 最后一个选项右边做圆角
  14. bottomRight: index === this.waveOptions.length - 1 ? 20 : 0
  15. })
  16. .onClick(() => {
  17. this.setIndex(index)
  18. })
  19. }, (item: string) => item)
  20. }
  21. .margin({ top: 20 })
  22. }

这是一个独立的模块,最后集成到build()方法里

this.waveTypeSelector()
  1. 管理预设的频率和波形

    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区

     

    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区

  1. @Builder
  2. presets() {
  3. Row() {
  4. ForEach(this.presetsData, (item: PresetModel, index: number) => {
  5. Column() {
  6. if (this.isEditMode) {
  7. Badge({ // 1. 如果是编辑模式,需要在图标右上角加一个badge,用于删除预设
  8. value: "X",
  9. style: {
  10. badgeColor: Color.Red
  11. }
  12. }) {
  13. this.presetItemImage(this.waveImageFromWaveType(item.waveType))
  14. }
  15. .onClick(event => {
  16. if (event.x > 32 && event.y < 16) { // 2. 右上角的badge不能设置点击,需要在整个badge控件上做点击位置判断,如果在badge图标的范围内,就删除预设数组相应位置的数据。
  17. this.presetsData.splice(index, 1)
  18. }
  19. })
  20. } else { // 3. 如果不是编辑模式,直接显示图片
  21. Flex() {
  22. this.presetItemImage(this.waveImageFromWaveType(item.waveType))
  23. }
  24. .width(50)
  25. .height(50)
  26. .onClick(() => {
  27. this.index = item.waveType // 4. 不是编辑模式的时候,点击图片,设置当前的波形和频率
  28. this.frequency = item.frequency
  29. })
  30. }
  31. Text(`${item.frequency} Hz`)
  32. .fontColor(this.mainColor)
  33. .fontSize(16)
  34. .margin({ top: 10 })
  35. }
  36. .width(64)
  37. .height(80)
  38. .margin({ right:
  39. index < this.presetsData.length - 1 ? 30 :
  40. this.isEditMode ? 30 :
  41. this.isPresetFull() ? 0 : 30 })
  42. }, (item: string) => item)
  43. Column() { // 5. 预设数组右边放置一个添加/完成按钮
  44. Image(this.isEditMode ? $r("app.media.prst_check") : $r("app.media.prst_add"))
  45. .width(50)
  46. .height(50)
  47. .backgroundColor(this.isEditMode ? this.mainColor : this.bgColor)
  48. .borderColor(this.mainColor)
  49. .borderWidth(4)
  50. .borderRadius(25)
  51. .onClick(() => {
  52. if (this.isEditMode) { // 6. 编辑模式的时候点击退出编辑模式
  53. this.isEditMode = false
  54. } else { // 7. 非编辑模式的时候点击添加预设,添加之后把预设数组写入preference
  55. if (this.isPresetFull()) {
  56. return
  57. }
  58. this.presetsData.push({ waveType: this.index, frequency: this.frequency })
  59. preferences.getPreferences(getContext(this), "default").then((preference) => {
  60. preference.put("presets", JSON.stringify(this.presetsData))
  61. preference.flush()
  62. })
  63. }
  64. })
  65. Text(this.isEditMode ? "完成" : "添加预设")
  66. .fontSize(16)
  67. .fontColor(this.mainColor)
  68. .margin({ top: 10 })
  69. }
  70. .width(64)
  71. .height(80)
  72. .visibility(this.isEditMode ? Visibility.Visible :
  73. this.isPresetFull() ? Visibility.None : Visibility.Visible) // 8. 预设数量有上限,达到上限以后不显示增加按钮
  74. }
  75. .margin({ top: 20 })
  76. }
  77. @Builder
  78. presetItemImage(image: Resource) {
  79. Image(image)
  80. .width(50)
  81. .height(50)
  82. .backgroundColor(this.mainColor)
  83. .borderRadius(25)
  84. .gesture(LongPressGesture()
  85. .onAction(() => {
  86. this.isEditMode = true
  87. })
  88. )
  89. }

生成声波

思路(可以跳过)

形成声波并播放是这个App的核心功能,如何实现这个功能,属实走了很多弯路。起初认为这是一个计算密集任务,在网上查到了一个生成正弦波并输出wav文件的C语言实现,并开了一个C工程来验证功能。可以成功调整声波频率,并生成wav文件。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "sndfile.h"
  5. #define SAMPLE_RATE 44100 // Sample rate in Hz
  6. #define DURATION 5.0 // Duration in seconds
  7. #define AMPLITUDE 0.5 // Amplitude of the sine wave
  8. #define FREQUENCY 440.0 // Frequency in Hz
  9. int main() {
  10. // Calculate the number of samples
  11. int num_samples = (int)(SAMPLE_RATE * DURATION);
  12. // Open the output file for writing
  13. SF_INFO sfinfo;
  14. sfinfo.samplerate = SAMPLE_RATE;
  15. sfinfo.channels = 1; // Mono
  16. sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
  17. SNDFILE* outfile = sf_open("sine_wave.wav", SFM_WRITE, &sfinfo);
  18. if (!outfile) {
  19. printf("Error: Unable to open output file\n");
  20. return 1;
  21. }
  22. // Generate and write the sine wave to the file
  23. double phase = 0.0;
  24. for (int i = 0; i < num_samples; i++) {
  25. double value = AMPLITUDE * sin(2.0 * M_PI * FREQUENCY * i / SAMPLE_RATE);
  26. if (sf_writef_double(outfile, &value, 1) != 1) {
  27. printf("Error writing to file\n");
  28. return 1;
  29. }
  30. }
  31. // Close the output file
  32. sf_close(outfile);
  33. printf("Sine wave generated and saved to 'sine_wave.wav'\n");
  34. return 0;
  35. }

可以看到这段代码里面依赖三方库sndfile。所以起初为了把这段C代码放进App里,在native包上面研究了很久。包括怎么处理三方库sndfile的依赖,以及sndfile对其他库的依赖。尝试过直接集成源码,也尝试过编译不同处理器架构的so文件。但发现工作量太大,另外涉及到的技术栈不熟悉,花太多精力搞这个功能。之后换了个思路,找了一份不依赖三方库生成正弦波的C代码。

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <math.h>
  4. #define SAMPLE_RATE 44100 // Sample rate in Hz
  5. #define DURATION 1 // Duration of the sine wave in seconds
  6. #define AMPLITUDE 0.5 // Amplitude of the sine wave
  7. #define FREQUENCY 440.0 // Frequency of the sine wave in Hz
  8. #define NUM_CHANNELS 1 // Number of audio channels (1 for mono, 2 for stereo)
  9. // Function to write a 16-bit PCM sample to a file
  10. void write_sample(FILE *file, int16_t sample) {
  11. fwrite(&sample, sizeof(int16_t), 1, file);
  12. }
  13. int main() {
  14. FILE *wav_file;
  15. int16_t sample;
  16. double t, dt;
  17. // Open the WAV file for writing
  18. wav_file = fopen("sine_wave.wav", "wb");
  19. if (!wav_file) {
  20. fprintf(stderr, "Error opening WAV file for writing\n");
  21. return 1;
  22. }
  23. // Calculate the time step (inverse of sample rate)
  24. dt = 1.0 / SAMPLE_RATE;
  25. const uint32_t chunkSize = 16;
  26. const uint16_t audioFormat = 1;
  27. const uint16_t numChannels = NUM_CHANNELS;
  28. const uint32_t sampleRate = SAMPLE_RATE;
  29. const uint32_t byteRate = SAMPLE_RATE * NUM_CHANNELS * sizeof(int16_t);
  30. const uint16_t blockAlign = NUM_CHANNELS * sizeof(int16_t);
  31. const uint16_t bitsPerSample = 16;
  32. // Write WAV file header
  33. fprintf(wav_file, "RIFF----WAVEfmt "); // Chunk ID and format
  34. fwrite(&chunkSize, 4, 1, wav_file); // Chunk size (16 for PCM)
  35. fwrite(&audioFormat, 2, 1, wav_file); // Audio format (1 for PCM)
  36. fwrite(&numChannels, 2, 1, wav_file); // Number of channels
  37. fwrite(&sampleRate, 4, 1, wav_file); // Sample rate
  38. fwrite(&byteRate, 4, 1, wav_file); // Byte rate
  39. fwrite(&blockAlign, 2, 1, wav_file); // Block align
  40. fwrite(&bitsPerSample, 2, 1, wav_file); // Bits per sample
  41. fprintf(wav_file, "data----"); // Data sub-chunk
  42. // Generate and write sine wave samples
  43. for (t = 0; t < DURATION; t += dt) {
  44. sample = AMPLITUDE * (int16_t)(32767.0 * sin(2.0 * M_PI * FREQUENCY * t));
  45. write_sample(wav_file, sample);
  46. }
  47. // Close the WAV file
  48. fclose(wav_file);
  49. return 0;
  50. }

这段代码可以直接放到native子工程里,并在js端调用。之后又花了很多精力研究了一下App文件沙盒的访问,使C语言生成的wav文件能被js访问到。然后通过AVPlayer播放wav文件。
然而,根据App的功能,需要在主界面拖动并连续调整声波频率。考虑到每次调整频率都要删除旧的wav,生成新的wav,效率可能不够。实际的验证下拉也发现频率调节会有延迟和杂音的问题。
于是,继续研究,深入阅读源码,发现整个代码的核心功能在for循环里。在// Write WAV file header注释段中,写入的是wav文件头,这段数据可以舍弃,舍弃以后的文件只有纯声波数据(pcm文件)。所以是否可以直接把声波数据播放出来呢?

最终方案(正文开始)

最终我在文档里找到了AudioRenderer,这个组件可以把声波数据直接播放出来。
创建一个AudioRendererPlayer类来控制音频的播放,以下是该类中的核心代码。本代码示例省略了很多细节,包括AudioRenderer的创建过程和写入声波数据的异步操作,为的是展示最核心的实现思路。

  1. const renderModel: audio.AudioRenderer
  2. const bufferSize = 800 // 1. bufferSize的大小经过了试验,取800是一个比较合适的数值。太大会导致一次写入的声波数据要放很久,在调整频率的时候会有延迟。太小的话,声音的播放会失败。
  3. const data = new Int16Array(bufferSize)
  4. for (let i = 0; i < bufferSize; i++) { // 2. 这是一段可以生成连续声波的循环,循环次数控制在bufferSize内,参数t连续重置
  5. data[i] = AMPLITUDE * (32767.0 * Math.sin(2.0 * Math.PI * this.frequency * this.t))
  6. this.t += dt;
  7. if (this.t >= 1.0 / this.frequency) {
  8. this.t -= 1.0 / this.frequency;
  9. }
  10. }
  11. this.renderModel.write(data.buffer) // 3. 将生成出来的声波数据由AudioRenderer写入。

除了正弦波之外,我们还可以生成其他的波形,把data[i]的赋值提取一个方法,判断当前类中设置的波形类型,生成相应的声波数据。

  1. data[i] = this.createWav()
  2. private createWav(): number {
  3. switch (this.wavType) {
  4. case WaveType.SINE: {
  5. return AMPLITUDE * (32767.0 * Math.sin(2.0 * Math.PI * this.frequency * this.t))
  6. }
  7. case WaveType.SQUARE: {
  8. const wave = (this.t < 0.5 / this.frequency) ? AMPLITUDE * 32767 : -AMPLITUDE * 32767
  9. return wave * 0.3
  10. }
  11. case WaveType.TRIANGLE: {
  12. const dividend = this.t * this.frequency
  13. const divisor = 1.0
  14. const position = ((dividend % divisor) + divisor) % divisor
  15. // Determine the triangle wave value based on the position
  16. let wave: number
  17. if (position < 0.25) {
  18. wave = AMPLITUDE * 32767 * (4 * position);
  19. } else if (position < 0.75) {
  20. wave = AMPLITUDE * 32767 * (2 - 4 * position);
  21. } else {
  22. wave = AMPLITUDE * 32767 * (4 * position - 4);
  23. }
  24. return wave
  25. }
  26. case WaveType.SAWTOOTH: {
  27. const dividend = this.t * this.frequency
  28. const divisor = 1.0
  29. const position = ((dividend % divisor) + divisor) % divisor
  30. const wave = AMPLITUDE * 32767 * (2 * position - 1);
  31. return wave * 0.5
  32. }
  33. }
  34. }

至此,本App的核心代码就讲解完成了。

最后,为了能够让大家跟上互联网时代的技术迭代,赶上互联网开发人员寒冬期间一波红利,在这里跟大家分享一下我自己近期学习心得以及参考网上资料整理出的一份最新版的鸿蒙学习提升资料,有需要的小伙伴自行领取,限时开源,先到先得~

 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/88391

推荐阅读
相关标签