1 电脑播放简单音节
播放哆瑞咪发嗦啦西
在音频处理领域,声音合成是通过计算机算法模拟人类声音的一种技术。具体来说,模拟哆瑞咪发嗦啦西音节需要声卡先接收基本音符的信号,然后通过数字信号处理技术,将该信号转换为模拟声音输出。在这个过程中,声卡会根据预先设定的音高、音色等参数,对收到的信号进行处理,最终产生出类似于哆瑞咪发嗦啦西音节的声音效果。整个过程涉及到数字信号处理、音频合成等相关技术。
实际上就是产生对应的频率信号,单频正弦波。
% 电脑模拟
% 定义采样频率
fs = 44100; % 44.1kHz采样率
% 定义音阶频率(单位:Hz)
notes = [262, 294, 330, 349, 392, 440, 494]; % 哆来咪发唆拉西
% 创建并播放音阶
for i = 1:length(notes)
% 生成0.5秒的正弦波
t = 0:1/fs:0.5;
y = sin(2*pi*notes(i)*t);
% 播放声音
sound(y, fs);
% 音符间隔0.1秒
pause(0.6); % 0.5秒播放 + 0.1秒间隔
end
% 倒序播放
for i = length(notes):-1:1
t = 0:1/fs:0.5;
y = sin(2*pi*notes(i)*t);
sound(y, fs);
pause(0.6);
end
2 满天都是小星星的模拟
MATLAB代码
fs = 44100;%采样率
melody = [262, 262, 392, 392, 440, 440, 392, ... % 音符
349, 349, 330, 330, 294, 294, 262];
durations = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, ... % 持续时间
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1];
% 生成并播放音乐
for i = 1:length(melody)
t = 0:1/fs:durations(i)-1/fs;
y = sin(2*pi*melody(i)*t);
sound(y, fs);
pause(durations(i)+0.05); % 加小间隔
end
3 Arduino播放音节
有点吵闹,哈哈
接线图
代码
% 首先确保安装了MATLAB Support Package for Arduino Hardware
% 可通过Add-Ons安装
clear
% 连接Arduino
a = arduino();
% 定义蜂鸣器引脚
buzzerPin = 'D8';
% 定义音阶频率
notes = [262, 294, 330, 349, 392, 440, 494]; % 哆来咪发唆拉西
% 播放音阶
for i = 1:length(notes)
% 播放音符
playTone(a, buzzerPin, notes(i), 0.5); % 频率, 持续时间(秒)
pause(0.6); % 总时间=播放时间+间隔
end
% 倒序播放
for i = length(notes):-1:1
playTone(a, buzzerPin, notes(i), 0.5);
pause(0.6);
end
% 断开连接
clear a;
4 arduino播放小星星
很不好听,sorry。还是用C写吧。
%% Arduino 蜂鸣器控制 - 小星星
clear; clc;
% 创建 Arduino 连接对象
try
a = arduino();
disp('Arduino 连接成功!');
catch
error('无法连接 Arduino,请检查硬件和驱动');
end
% 定义蜂鸣器引脚
buzzerPin = 'D8';
% 定义音符频率 (Hz)
notes.C4 = 262; % 哆
notes.D4 = 294; % 来
notes.E4 = 330; % 咪
notes.F4 = 349; % 发
notes.G4 = 392; % 唆
notes.A4 = 440; % 拉
notes.B4 = 494; % 西
% 小星星乐谱 - 音符序列
melody = [
notes.C4, notes.C4, notes.G4, notes.G4, notes.A4, notes.A4, notes.G4, ...
notes.F4, notes.F4, notes.E4, notes.E4, notes.D4, notes.D4, notes.C4, ...
notes.G4, notes.G4, notes.F4, notes.F4, notes.E4, notes.E4, notes.D4, ...
notes.G4, notes.G4, notes.F4, notes.F4, notes.E4, notes.E4, notes.D4, ...
notes.C4, notes.C4, notes.G4, notes.G4, notes.A4, notes.A4, notes.G4, ...
notes.F4, notes.F4, notes.E4, notes.E4, notes.D4, notes.D4, notes.C4
];
% 音符持续时间 (四分音符=1, 二分音符=2)
durations = [
1, 1, 1, 1, 1, 1, 2, ...
1, 1, 1, 1, 1, 1, 2, ...
1, 1, 1, 1, 1, 1, 2, ...
1, 1, 1, 1, 1, 1, 2, ...
1, 1, 1, 1, 1, 1, 2, ...
1, 1, 1, 1, 1, 1, 2
];
% 设置节奏 (BPM - 每分钟节拍数)
tempo = 120;
% 计算全音符持续时间 (毫秒)
wholeNote = (60000 * 4) / tempo;
%% 播放音乐
disp('开始播放《小星星》...');
for i = 1:length(melody)
% 计算当前音符持续时间
noteDuration = wholeNote / durations(i);
% 播放音符
playTone(a, buzzerPin, melody(i), noteDuration/1000); % 转换为秒
% 音符间短暂停顿 (30% 的持续时间)
pause(noteDuration * 0.3 / 1000);
end
disp('播放完成!');
% 清理
clear a;
5 欢乐颂
只是不吵闹了
%% Arduino 蜂鸣器音乐控制 - 优化版《欢乐颂》
clear; clc;
% 创建Arduino连接
try
a = arduino();
disp('Arduino连接成功!');
catch
error('无法连接Arduino,请检查硬件和驱动');
end
% 定义蜂鸣器引脚
buzzerPin = 'D8';
% 定义音符频率 (Hz) - 优化后的频率匹配
notes.E4 = 330; % 咪
notes.F4 = 349; % 发
notes.G4 = 392; % 唆
notes.A4 = 440; % 拉
notes.B4 = 494; % 西
notes.C4 = 523; % 高音哆
notes.D4 = 587; % 高音来
notes.E4 = 659; % 高音咪
% 欢乐颂乐谱 - 优化后的音符序列
melody = [
notes.E4, notes.E4, notes.F4, notes.G4, ...
notes.G4, notes.F4, notes.E4, notes.D4, ...
notes.C4, notes.C4, notes.D4, notes.E4, ...
notes.E4, notes.D4, notes.D4, ...
notes.E4, notes.E4, notes.F4, notes.G4, ...
notes.G4, notes.F4, notes.E4, notes.D4, ...
notes.C4, notes.C4, notes.D4, notes.E4, ...
notes.D4, notes.C4, notes.C4];
% 优化后的节拍 (基础单位=0.3秒)
durations = [
1, 1, 1, 1, ...
1, 1, 1, 1, ...
1, 1, 1, 1, ...
1.5, 0.5, 2, ...
1, 1, 1, 1, ...
1, 1, 1, 1, ...
1, 1, 1, 1, ...
1.5, 0.5, 2
];
% 设置节奏参数
baseDuration = 0.3; % 基础音符持续时间(秒)
pauseRatio = 0.15; % 音符间停顿比例
%% 优化播放函数
disp('开始播放优化版《欢乐颂》...');
for i = 1:length(melody)
% 计算当前音符持续时间
noteDuration = baseDuration * durations(i);
% 使用优化后的播放方式
playTone(a, buzzerPin, melody(i), noteDuration*(1-pauseRatio));
% 精确控制停顿时间
pause(noteDuration*pauseRatio);
% 确保停止前一个音符
playTone(a, buzzerPin, 0, 0.01); % 发送停止信号
end
disp('播放完成!');
% 清理连接
clear a;
欢乐颂2
% 创建与Arduino的连接
clear
a = arduino;%('COM3'); % 根据你的Arduino连接的端口修改
% 定义音符频率数组
melody = [659, 659, 0, 659, 0, 523, 659, 0, 784, 0, 0, 0, 392, 0, 0, 0, ...
659, 659, 0, 659, 0, 523, 659, 0, 784, 0, 0, 0, 392, 0, 0, 0, ...
659, 659, 0, 659, 0, 523, 659, 0, 784, 0, 0, 0, 784, 0, 0, 0, ...
880, 0, 0, 698, 0, 0, 659, 0, 587, 0, 0, 0, 587, 0, 0, 523];
% 定义音符时长数组(单位:毫秒)
noteDurations = [125, 125, 250, 125, 250, 125, 125, 250, 125, 250, 250, 250, 125, 250, 250, 250, ...
125, 125, 250, 125, 250, 125, 125, 250, 125, 250, 250, 250, 125, 250, 250, 250, ...
125, 125, 250, 125, 250, 125, 125, 250, 125, 250, 250, 250, 125, 250, 250, 250, ...
125, 250, 250, 125, 250, 250, 125, 250, 250, 250, 125, 250, 250, 125, 125, 500];
% 播放音乐
for i = 1:length(melody)
if melody(i) == 0
% 休止符
pause(noteDurations(i)/1000);
else
% 播放音符
playTone(a, 'D8', melody(i), noteDurations(i)/1000); % 使用playTone方法播放音符
end
end
% 关闭与Arduino的连接
clear a;