HarmonyOS鸿蒙Next Hi3861:自家开发版上实现蜂蜜音小调

HarmonyOS鸿蒙Next Hi3861:自家开发版上实现蜂蜜音小调 成功的在hi3861中的红绿灯板块上实现了两只老虎调调的播放。

代码如下:

/*
 * Copyright (c) 2020, HiHope Community.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_watchdog.h"
#include "wifiiot_pwm.h"

#include "hi_pwm.h"

static volatile int g_buttonPressed = 0;

static const uint16_t g_tuneFreqs[] = {
    0, // 40M Hz 对应的分频系数:
    38223, // 1046.5
    34052, // 1174.7
    30338, // 1318.5
    28635, // 1396.9
    25511, // 1568
    22728, // 1760
    20249, // 1975.5
    51021 // 5_ 783.99 // 第一个八度的 5
};

// 曲谱音符
static const uint8_t g_scoreNotes[] = {
    // 《两只老虎》简谱:http://www.jianpu.cn/pu/33/33945.htm
    1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5,
    5, 6, 5, 4, 3, 1, 5, 6, 5, 4, 3, 1, 1, 8, 1, 
    1, 8, 1, // 最后两个 5 应该是低八度的,链接图片中的曲谱不对,声音到最后听起来不太对劲
};

// 曲谱时值
static const uint8_t g_scoreDurations[] = {
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8,
    3, 1, 3, 1, 4, 4, 3, 1, 3, 1, 4, 4, 4, 4, 8,
    4, 4, 8, 4, 4, 8,
};

static void *jltfcloudcom(const char *arg)
{
    (void)arg;

    printf("jltfcloudcom start!\r\n");

    hi_pwm_set_clock(PWM_CLK_XTAL); // 设置时钟源为晶体时钟(40MHz,默认时钟源160MHz)

    for (size_t i = 0; i < sizeof(g_scoreNotes)/sizeof(g_scoreNotes[0]); i++) {
        uint32_t tune = g_scoreNotes[i]; // 音符
        uint16_t freqDivisor = g_tuneFreqs[tune];
        uint32_t tuneInterval = g_scoreDurations[i] * (125*1000);

        printf("%d %d %d %d\r\n", tune, (40*1000*1000)/freqDivisor, freqDivisor, tuneInterval);

        PwmStart(WIFI_IOT_PWM_PORT_PWM0, freqDivisor/2, freqDivisor);
        usleep(tuneInterval);
        PwmStop(WIFI_IOT_PWM_PORT_PWM0);
    }

    return NULL;
}

static void jltfcloudcn(void)
{
    osThreadAttr_t attr;

    GpioInit();

    // 蜂鸣器引脚 设置为 PWM 功能
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT);
    PwmInit(WIFI_IOT_PWM_PORT_PWM0);

    WatchDogDisable();

    attr.name = "jltfcloudcom";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024;
    attr.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)jltfcloudcom, NULL, &attr) == NULL) {
        printf("[LedExample] Falied to create jltfcloudcom!\n");
    }
}

SYS_RUN(jltfcloudcn);

本部分参考和引用了一些公开代码。


更多关于HarmonyOS鸿蒙Next Hi3861:自家开发版上实现蜂蜜音小调的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

HarmonyOS鸿蒙Next Hi3861是一款基于鸿蒙系统的开发板,支持多种开发语言和框架。在自家开发版上实现蜂蜜音小调,可以通过鸿蒙系统的音频处理API来实现。具体步骤如下:

  1. 环境准备:确保开发环境已安装鸿蒙SDK,并配置好Hi3861开发板的连接。
  2. 创建项目:在DevEco Studio中创建一个新的鸿蒙项目,选择Hi3861作为目标设备。
  3. 导入音频文件:将蜂蜜音小调的音频文件导入到项目的资源目录中。
  4. 编写代码:使用鸿蒙系统的音频播放API,编写代码来加载和播放音频文件。例如,可以使用AudioPlayer类来实现音频的播放控制。
  5. 调试与测试:将代码编译并烧录到Hi3861开发板上,进行调试和测试,确保音频能够正常播放。

通过以上步骤,可以在Hi3861开发板上实现蜂蜜音小调的播放功能。

更多关于HarmonyOS鸿蒙Next Hi3861:自家开发版上实现蜂蜜音小调的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next Hi3861开发板上实现蜂蜜音小调,可以通过编程控制PWM(脉宽调制)输出不同频率的方波来模拟音调。首先,确定蜂蜜音小调的音阶频率,然后使用Hi3861的PWM模块生成对应频率的方波信号。通过调整PWM的占空比和频率,可以模拟出不同音高的音符。最后,按照小调的旋律顺序播放这些音符,即可实现蜂蜜音小调。

回到顶部