Write down what you learned anew when outputting music with a microcomputer
In realizing the technology, I referred to the following article of "Chikuwa-san". http://nn-hokuson.hatenablog.com/entry/2017/09/01/092945
For the time being, I knew that sound was a composite of sine waves of various frequencies. I started by checking it in Python.
Somehow, it looks like an audio signal.
The realized code is shown below.
import numpy as np
import matplotlib.pyplot as plt
#frequency[Hz]
f1 = 100
f2 = 500
f3 = 1000
f4 = 10000
f = [f1,f2,f3,f4]
#amplitude[V]
A1 = 5
A2 = 3
A3 = 1
A4 = 0.1
A = [A1,A2,A3,A4]
#time
t = np.arange(0,0.01,0.00001)
#function
fig = plt.figure()
#Add Axes
ax = fig.add_subplot(111)
y = []
for i in range(4):
y.append(A[i]*np.sin(2*np.pi*f[i]*t))
plt.plot(t,y[i])
#Draw execution
#Axis label settings
ax.set_xlabel("time[s]", size = 14, weight = "light")
ax.set_ylabel("Volatage[V]", size = 14, weight = "light")
plt.show()
Wave composition is realized by the sum of sine waves of each frequency
plt.plot(t,sum(y))
If you vibrate the air with some kind of signal, it should be recognized as some kind of sound.
We use a speaker as a method to vibrate the air. (It seems that this speaker converts electrical signals into vibrations)
A PWM signal is generally used to give an electric signal to a speaker.
** Image of composite wave applied as input to speaker **
This time, we aimed to output MP3 files as audio. I used M5STACK as a microcomputer for voice control.
** 1. Convert MP3 file format ** In doing this work, I referred to the following site. http://nn-hokuson.hatenablog.com/entry/2017/09/01/092945
I prepared the MP3 file I want to play and converted it on the next site. https://audacity.softonic.jp/
Here, you can change the file format of the original data (prepared MP3 file) and It is possible to determine an arbitrary sampling frequency and create data with a lighter capacity than the original data. Since the memory capacity is limited when mounting on a microcomputer such as M5STACK It is necessary to consult with the playback time and sampling frequency.
** 2. Convert to text file **
Taking sampled music as an example Save in the following save format
Then the file name .raw will be generated. Put it on a linux terminal
xxd -i file name.raw
(Tentatively name the file sound.raw)
unsigned char sound_raw[] ={
//The result of sampling music
}
unsigned int sound_raw_len = xx
It is output with. Turn this result into an Arduino script By copying and pasting, the audio file is stored in the memory of Arduino. You can now save it.
** 3. Implemented in M5STACK **
In M5STACK, it could be realized with the following code.
#include <M5Stack.h>
unsigned char sound_raw[] ={
//Input the result of sampling music
}
unsigned int sound_raw_len = xx; //Number of samples
unsigned char pwm;
void play() {
for (int i = 0; i < sound_raw_len; i++) {
pwm = sound_raw[i];
dacWrite(25, pwm); //In M5STACK, 25PIN is connected to the speaker.
delayMicroseconds(xx);// xx:Depends on sampling frequency
}
dacWrite(25, 0); //speakeroff
}
void setup() {
M5.begin();
delay(500);
M5.Lcd.fillScreen(WHITE);
}
void loop()
{
if(M5.BtnA.wasPressed())//Sound when button A is pressed
{
play();
}
M5.update();
}
** Supplement ** I ran this code on an Arduino Nano and it didn't work. Simply put, it was how to define the carrier frequency and array of pwm. I hope I can write about this in another article.
Recommended Posts