% 各音音符对应时长(1*number_syllables的向量), note_time=[3/2,1/2,1/2,1/2,1/2,1/2,4,3/2,... 1/2,1/2,1/2,1/2,1/2,4,3/2,1/2,1/2,...
1/2,1/2,1/2,2,1/2,1/2,1/2,1/2,3/2,1/2,1/2,1/2,1/2,1/2,4]; % 乐音数,
num_syllables=length(note_time); % 单位音节采样点量num_sample, num_sample=8000;
% 各音时长time(1*number_syllables的向量), time=num_sample*note_time;
%谐波harmonic(1*number_harmonic的向量),谐波的倍数n和对应幅值A global n; global A; n=[1 3 5 7]; A=[1 0.4 0.2 0.1];
%包络特殊点special_points(包括开始点S0、转折点T1,T2,T3、终点E4,number_sp*2的数组) global special_points;
special_points=[0 0;0.20 1.50;0.333 1.00;0.666 1.00;1.00 0];
% 第二步:
% 构造时长/频率类型矩阵和找出总时长 type_time_array=unique(time);
type_frequency_array=unique(frequency);
num_time_all=sum(time,'all');
% 构造时长/频率结构数据变量,存储模板,以便免除重复计算 % 更改时长/频率矩阵,使其便于索引
type_time_structure=struct('type',num2cell(type_time_array)); for type_index=1:length(type_time_array) type_time_structure(type_index).template=... envelope(type_time_array(type_index));
time(type_time_array(type_index)==time)=type_index; end
type_frequency_structure=struct('type',num2cell(type_frequency_array));
global Tmax;
Tmax=type_time_array(end);
for type_index=1:length(type_frequency_array)
type_frequency_structure(type_index).template=...
harmonic_template(2*pi*(type_frequency_array(type_index)));
frequency(type_frequency_array(type_index)==frequency)=type_index; end % 第三步:
% 遍历位置得出所有点的结果 result_all=zeros(num_time_all,1); result_index=1;
for syllables_index=1:num_syllables
num_current_Ttype=type_time_structure(time(syllables_index)).type; result_index_end=result_index-1+num_current_Ttype; result_all(result_index:result_index_end)=...
type_time_structure(time(syllables_index)).template... .*type_frequency_structure(frequency(syllables_index)).template(1:num_current_Ttype);
result_index=result_index_end+1; end
audiowrite('music_htys.wav',result_all,8000) ;
plot(result_all);
function [envelope]=envelope(type) global special_points;
%创立结果存储点 envelope=ones(1,type); %创立此音节的特殊点
special_points_single=[special_points(:,1)*type special_points(:,2)];
%创立各线端的对应开头与结束点
status_list=[special_points_single(1:end-1,1)+1 special_points_single(2:end,1)]; %每个不同的状态都逐点遍历
for status_index=1:length(special_points)-1
envelope(status_list(status_index,1):status_list(status_index,2))=...
interp1(special_points_single(status_index:status_index+1,1),special_points_single(status_index:status_index+1,2),status_list(status_index,1):status_list(status_index,2),'linear'); end
end
function [storage_result]=harmonic_template(w) global n; global A; global Tmax;
storage_result=1:Tmax; diag_nw=diag(w*n);
storage_result=repmat(storage_result,length(n),1); storage_result=diag_nw*storage_result; storage_result=sin(storage_result); storage_result=diag(A)*storage_result; storage_result=sum(storage_result); end