6573 views|16 replies

178

Posts

0

Resources
The OP
 

Problems with STM32 and passive buzzer playing sound [Copy link]

 

The pin drives the passive buzzer via PWM to produce different sounds

Looking at the examples on the Internet, the beat frequency and other parameters corresponding to the sound file are placed in an array and then called one by one to play the desired sound.

Now the question is how to get the beat and frequency parameters corresponding to a piece of audio

How do you get this data from MP3 files downloaded from the Internet?

This post is from stm32/stm8

Latest reply

The following figure shows the circuit diagram of the connection between the buzzer and STM32. The "PBeep" in the program is the macro definition of the "BEEP" IO port in the figure, which is usually defined in the header file: #define PBeep PBout(8) [attach]860506[/attach] First, find a score online, look carefully, and figure out two arrays, one for pitch and one for note length. Check the score, as follows: [attach]860505[/attach] When it comes to converting the musical notation to an array, you can see the benefit of choosing a nursery rhyme. All the tones are in the tone[] array, that is, between the bass 7 and the treble 5. Let's look at this array again: // Low 7 1 2 3 4 5 6 7 High 1 High 2 High 3 High 4 High 5 Silent uc16 tone[] = {247,262,294,330,349,392,440,494,523,587,659,698,784,1000}; // Audio data table #include "beep.h" int melody[] = {50, 50, 50, 50, 200, 200, 200, 400, 400, 500, 500, 500}; void BEEP_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //Enable port A clock GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Speed 50MHz GPIO_Init(GPIOB, &GPIO_InitStructure); //Initialize GPIOD3,6 GPIO_SetBits(GPIOB,GPIO_Pin_5); } void Sound(u16 frq) { u32 time; if(frq != 1000) { time = 500000/((u32)frq); PBeep = 1; delay_us(time); PBeep = 0; delay_us(time); }else delay_us (1000); } void Sound2(u16 time) { PBeep = 1; delay_ms(time); PBeep = 0; delay_ms(time); } void play_successful(void) { int id=0; for(id = 0;id < 12 ;id++) { Sound2(melody[id]); } } void play_failed(void) { int id=0; for(id = 11 ;id >=0 ;id--) { Sound2(melody[id]); } } void play_music(void) { // low 7 1 2 3 4 5 6 7 High 1 High 2 High 3 High 4 High 5 Silent uc16 tone[] = {247,262,294,330,349,392,440,294,523,587,659,698,784,1000}; //Audio data table //Love song in the world u8 music[] = {5,5,6, 8,7,6,5,6,13,13,//tone 5,5,6,8,7,6,5,3,13,13, 2,2,3,5,3,5,6 ,3,2,1, 6,6,5,6,5,3,6,5,13,13, 5,5,6,8,7,6,5,6,13,13, 5,5,6,8,7,6,5,3,13,13, 2,2,3,5,3, 5,6,3,2,1, 6,6,5,6,5,3,6,1, // time 2,4,2,2,2,2,2,8,4 , 4, 2,4,2,4,2,2,4,2,2,8 , 2,4,2,2,2,2,2,8,4 ,4, 2,4,2,2,2,2,2,8,4, 4, 2,4,2,2,2,2,2,8,4, 4, 2,4,2,4,2, 2,4,2,2,8, 2,4,2,2,2,2,2,8, 4, 2,2,2, 4, 2,2,2, 2,2,8, 4, 2,2,2,4,2,2,2,2,2,8, 4, 2,2, 2,4,2,2,5,2,6,2,4, 2,2 ,2,4,2,4,2,2,12}; u32 yanshi; u16 i,e; yanshi = 10; for(i=0;i<sizeof(music)/sizeof(music[0]);i++){ for(e=0;e<((u16)time)*tone[music ]/yanshi;e++){ Sound((u32)tone[music]); } } } #ifndef __BEEP__H #define __BEEP__H #include "sys.h" #include "stdlib.h" #include "delay.h" //Define GPIOB bit address variable macro, bit input macro, output macro #define PBeep PBout(5) void BEEP_Init(void); void Sound(u16 frq); void Sound2(u16 time); void play_music(void); void play_successful(void); void play_failed(void); #endif   Details Published on 2024-11-10 00:53
 

6742

Posts

2

Resources
2
 

MP3 files downloaded from the Internet cannot be converted into this kind of data. There are some audio data sharing on the Internet that you can use. Some development boards also come with audio data files.

This post is from stm32/stm8

Comments

My current project is to compare the detection value with the alarm value, and make different sounds according to the difference. These sounds are only available in MP3 files  Details Published on 2023-8-12 08:12
 
 

2w

Posts

0

Resources
3
 

The international standard for the sound a in the small letter group is 440Hz. All pitches can be obtained from this.

As for the beat, it is the duration of a certain sound.

This post is from stm32/stm8

Comments

What does it mean?  Details Published on 2023-8-12 08:22
 
 

2

Posts

1

Resources
4
 
You can use malab to process audio files
This post is from stm32/stm8
 
 
 

2

Posts

1

Resources
5
 

You can use matlab to do fft analysis on audio files

This post is from stm32/stm8
 
 
 

178

Posts

0

Resources
6
 
wangerxian posted on 2023-8-11 16:54 The MP3 downloaded from the Internet cannot be converted into this kind of data. There are some audio data shared on the Internet that you can use. Some development boards also have audio...

My current project is to compare the detection value with the alarm value, and make different sounds according to the difference. These sounds are only available in MP3 files

This post is from stm32/stm8

Comments

I don't know if there is a tool that can write code to make sounds, so that you can test the sound corresponding to the code you wrote. I don't think there is a direct conversion tool.  Details Published on 2023-8-14 09:10
 
 
 

178

Posts

0

Resources
7
 
maychang posted on 2023-8-11 17:00 The international standard for a group of a sounds in small letters is 440Hz. From this, all pitches can be obtained. As for the beat, it is the duration of a certain sound.

what does it mean?

This post is from stm32/stm8
 
 
 

4817

Posts

4

Resources
8
 

Use audio analysis software or use a programming language and corresponding audio processing library (such as Python's Librosa library)

This post is from stm32/stm8
 
 
 

5998

Posts

6

Resources
9
 

First edit the audio to get the desired segment, and then convert it into a C language array through Python

This post is from stm32/stm8
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

6742

Posts

2

Resources
10
 
Will the program be published on 2023-8-12 08:12 My current project is to compare the detection value with the alarm value, and make different sounds according to the difference. These sounds are only available in MP3 files

I don't know if there is a tool that can write code to make sounds, so that you can test the sound corresponding to the code you wrote. I don't think there is a direct conversion tool.

This post is from stm32/stm8
 
 
 

6062

Posts

4

Resources
11
 

PCM encoding.

This post is from stm32/stm8
 
 
 

6818

Posts

11

Resources
12
 

I hope the OP can learn it and post again so I can learn from it too!

This post is from stm32/stm8
 
 
 

22

Posts

0

Resources
13
 

Let's look at the simple notation, do re mi, and then map it to different frequencies. They are all standard middle C, which seems to be 440Hz.

This post is from stm32/stm8
 
 
 

4764

Posts

12

Resources
14
 
I suggest you read this article about MIDI. If you want to use software to view the spectrum, you can search for Adobe software. I recommend Goldenwave.
This post is from stm32/stm8
 
 
 

7422

Posts

2

Resources
15
 

There is a lot of this kind of data on the Internet, and chatgpt can also give it to you directly.

This post is from stm32/stm8
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

24

Posts

0

Resources
16
 
The most important thing is practice. You must have a hardware platform that can show you. Without this,
This post is from stm32/stm8
 
 
 

396

Posts

4

Resources
17
 

The following figure shows the circuit diagram of the connection between the buzzer and STM32. The "PBeep" in the program is the macro definition of the "BEEP" IO port in the figure, which is usually defined in the header file:
#define PBeep PBout(8)

First, find a score online, look carefully, and figure out two arrays, one for pitch and one for note length.
Check the score, as follows:



When it comes to converting the musical notation to an array, you can see the benefit of choosing a nursery rhyme. All the tones are in the tone[] array, that is, between the bass 7 and the treble 5. Let's look at this array again:

// Low 7 1 2 3 4 5 6 7 High 1 High 2 High 3 High 4 High 5 Silent
uc16 tone[] = {247,262,294,330,349,392,440,494,523,587,659,698,784,1000}; // Audio data table

#include "beep.h"

int melody[] = {50, 50, 50, 50, 200, 200, 200, 400, 400, 500, 500, 500};

void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //Enable port A clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Speed 50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //Initialize GPIOD3,6
GPIO_SetBits(GPIOB,GPIO_Pin_5);
}

void Sound(u16 frq)
{
u32 time;
if(frq != 1000)
{
time = 500000/((u32)frq);
PBeep = 1;
delay_us(time);
PBeep = 0;
delay_us(time);
}else
delay_us (1000);
}
void Sound2(u16 time)
{
PBeep = 1;
delay_ms(time);
PBeep = 0;
delay_ms(time);
}
void play_successful(void)
{
int id=0;
for(id = 0;id < 12 ;id++)
{
Sound2(melody[id]);
}
}
void play_failed(void)
{
int id=0;
for(id = 11 ;id >=0 ;id--)
{
Sound2(melody[id]);
}
}
void play_music(void)
{
// low 7 1 2 3 4 5 6 7 High 1 High 2 High 3 High 4 High 5 Silent
uc16 tone[] = {247,262,294,330,349,392,440,294,523,587,659,698,784,1000}; //Audio data table
//Love song in the world
u8 music[] = {5,5,6, 8,7,6,5,6,13,13,//tone
5,5,6,8,7,6,5,3,13,13,
2,2,3,5,3,5,6 ,3,2,1,
6,6,5,6,5,3,6,5,13,13,

5,5,6,8,7,6,5,6,13,13,
5,5,6,8,7,6,5,3,13,13,
2,2,3,5,3, 5,6,3,2,1,
6,6,5,6,5,3,6,1,

// time 2,4,2,2,2,2,2,8,4 , 4, 2,4,2,4,2,2,4,2,2,8
, 2,4,2,2,2,2,2,8,4 ,4,





2,4,2,2,2,2,2,8,4, 4,
2,4,2,2,2,2,2,8,4, 4,
2,4,2,4,2, 2,4,2,2,8,
2,4,2,2,2,2,2,8,

4, 2,2,2, 4, 2,2,2, 2,2,8,
4, 2,2,2,4,2,2,2,2,2,8,
4, 2,2, 2,4,2,2,5,2,6,2,4,
2,2 ,2,4,2,4,2,2,12};
u32 yanshi;
u16 i,e;
yanshi = 10;
for(i=0;i<sizeof(music)/sizeof(music[0]);i++){
for(e=0;e<((u16)time)*tone[music ]/yanshi;e++){
Sound((u32)tone[music]);
}
}
}

#ifndef __BEEP__H
#define __BEEP__H

#include "sys.h"
#include "stdlib.h"
#include "delay.h"

//Define GPIOB bit address variable macro, bit input macro, output macro
#define PBeep PBout(5)

void BEEP_Init(void);
void Sound(u16 frq);
void Sound2(u16 time);
void play_music(void);
void play_successful(void);
void play_failed(void);

#endif

This post is from stm32/stm8
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list