how can i make a music using matlab??

63 ビュー (過去 30 日間)
Christian Prinncess Usman
Christian Prinncess Usman 2014 年 7 月 8 日
編集済み: Image Analyst 2023 年 3 月 4 日
my instructor want us to make a music using MATLAB.

採用された回答

Image Analyst
Image Analyst 2014 年 7 月 8 日
Attached is a demo, make_wav_file.m, on how to make a sound.
You can start it off with this:
NET.addAssembly('System.Speech');
obj = System.Speech.Synthesis.SpeechSynthesizer;
obj.Volume = 100;
Speak(obj,'Now you will listen to my music');
Good luck.

その他の回答 (3 件)

Chiemela Victor Amaechi
Chiemela Victor Amaechi 2019 年 11 月 24 日
編集済み: DGM 2023 年 3 月 4 日
SYNTAX FOR MUSIC IN MATLAB:
clear;
a=sin(2*pi*440*(0:0.000125:0.5));
b=sin(2*pi*493.88*(0:0.000125:0.5));
cs=sin(2*pi*554.37*(0:0.000125:0.5));
d=sin(2*pi*587.33*(0:0.000125:0.5));
e=sin(2*pi*659.26*(0:0.000125:0.5));
fs=sin(2*pi*739.99*(0:0.000125:0.5));
line1=[a,a,e,e,fs,fs,e,e,];
line2=[d,d,cs,cs,b,b,a,a,];
line3=[e,e,d,d,cs,cs,b,b];
song=[line1,line2,line3,line3,line1,line2];
%wavwrite(song,'song.wav');
audiowrite(song,'song.wav');
============================================
SAMPLE SOUND
notes={'C' 'C#' 'D' 'Eb' 'E' 'F' 'F#' 'G' 'G#' 'A' 'Bb' 'B'}
freq=[261.6 277.2 293.7 311.1 329.6 349.2...
370.0 392.0 415.3 440.0 466.2 493.9]
song={'A' 'G' 'G' 'A' 'B' 'C' 'F' 'G'} % your song
a=[]
for k=1:numel(song)
note_value=0:0.000125:0.5 % You can change the note duration
a=[a sin(2*pi* freq(strcmp(notes,song{k}))*note_value)];
end
sound(a)
============================================
MUSIC FOR A GAME
clear
tic
notes={'C' 'C#' 'D' 'Eb' 'E' 'F' 'F#' 'G' 'G#' 'A' 'Bb' 'B'};
freq=[261.6 277.2 293.7 311.1 329.6 349.2...
370.0 392.0 415.3 440.0 466.2 493.9];
song={'A' 'G' 'G' 'A' 'B' 'C' 'F' 'G'} ; % your song
a=[];
for i=2:70
for k=1:numel(song);
note_value=0:i*0.0001:0.5 ;% You can change the note duration
a=[a sin(2*pi* freq(strcmp(notes,song{k}))*note_value)];
end
end
sound(a);
toc
=============================================
CHILDREN MUSIC FOR ALPHABETS ...A,B,C,D,E,F,G,H,I,J,K,
notecreate = @(frq,dur) sin(2*pi* [1:dur]/8192 * (440*2.^((frq-1)/12)));
notename = {'A' 'A#' 'B' 'C' 'C#' 'D' 'D#' 'E' 'F' 'F#' 'G' 'G#'};
song = {'A' 'A' 'E' 'E' 'F#' 'F#' 'E' 'E' 'D' 'D' 'C#' 'C#' 'B' 'B' 'A' 'A'};
for k1 = 1:length(song)
idx = strcmp(song(k1), notename);
songidx(k1) = find(idx);
end
dur = 0.3*8192;
songnote = [];
for k1 = 1:length(songidx)
songnote = [songnote; [notecreate(songidx(k1),dur) zeros(1,75)]'];
end
soundsc(songnote, 8192)
%The frequencies of notes A, B, C#, D, E and F# are 440 Hz, 493.88 Hz,
%554.37 Hz, 587.33 Hz, 659.26 Hz and 739.99 Hz, respectively.
%how to write a MATLAB file to produce a piece of music with notes in the
%following order : A, A, E, E, F#, F#, E, E, D, D, C#, C#, B, B, A, A.
%Assign the duration of each note as 0.3s.

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2014 年 7 月 8 日
編集済み: Image Analyst 2023 年 3 月 4 日
Or you could go all modern and synthesize your own:
fs=16384; % sampling frequency
T=10; % 10-second song
time=0:1/fs:T; % time samples
y=sum(cell2mat(arrayfun(@(f,t)exp(-100*(time-t).^2).*sin(cumsum(f*exp(-(time-t).^2))/fs),kron([1 .7492],randi([400,4000],1,100)),repmat(1+rand(1,100).^4*(T-2),1,2)+kron([0 .5],ones(1,100)),'uni',0)'),1); % go crazy here
plot(y, 'b-');
xlabel('Time');
ylabel('Signal Value');
grid on;
soundsc(y,fs); % Play the sound
  3 件のコメント
Trevor Secure
Trevor Secure 2020 年 6 月 18 日
sorry to necro post, but i'm brand new to matlab and whatever that sound was that your code produced is amazing.
Image Analyst
Image Analyst 2020 年 6 月 18 日
編集済み: Image Analyst 2023 年 3 月 4 日
Here it is:
fs = 16384; % sampling frequency
T = 10; % 10-second song
timeVector = 0 : 1/fs : T; % time samples
y = sum(cell2mat(arrayfun(@(f,t)exp(-100*(timeVector-t).^2).*sin(cumsum(f*exp(-(timeVector-t).^2))/fs),kron([1 .7492],randi([400,4000],1,100)),repmat(1+rand(1,100).^4*(T-2),1,2)+kron([0 .5],ones(1,100)),'uni',0)'),1); % go crazy here
% Plot the waveform:
plot(timeVector, y, 'b-');
grid on;
fontSize = 15;
xlabel('Time', 'FontSize', fontSize);
ylabel('Signal Amplitude', 'FontSize', fontSize);
title('Audio Signal Amplitude', 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
soundsc(y,fs); % play sound
I'm also attaching mine.
% Program to create a warbling wave file with variable amplitude and pitch.
% function make_wav_file()
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create the filename where we will save the waveform.
folder = pwd;
baseFileName = 'Test_Wave.wav';
fullFileName = fullfile(folder, baseFileName);
fprintf('Full File Name = %s\n', fullFileName);
% Set up the time axis:
Fs = 8000;
duration = 2; % seconds.
t = 1 : duration * Fs; % 2 seconds
% Set up the period (pitch, frequency):
T = 13; % Constant pitch if you use this.
T = linspace(25, 8, length(t)); % Pitch changes if you use this.
% Create the maximum amplitude:
Amplitude = 32767;
% Add an exponential decay:
Amplitude = Amplitude .* exp(-0.0003*t);
% Add an ocillation on the amplitude:
% Amplitude = Amplitude .* rand(1, length(x)); % Makes a shushing/roaring sound.
Amplitude = Amplitude .* sin(2.*pi.*t./2000); % Decaying pulsing sound.
% Construct the waveform:
y = int16(Amplitude .* sin(2.*pi.*t./T));
% y = abs(int16(Amplitude .* sin(2.*pi.*x./T)));
% Plot the waveform:
plot(t, y, 'b-');
title('Waveform', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
fprintf('Writing file %s...\n', fullFileName);
% Write the waveform to a file:
audiowrite(fullFileName, y, Fs);
% Play the sound as many times as the user wants.
playAgain = true;
counter = 1;
while playAgain
% Play the sound that we just created.
fprintf('Playing file %s %d times...\n', fullFileName, counter);
player = audioplayer(y, Fs);
play(player);
% Ask user if they want to play the sound again.
promptMessage = sprintf('You have played the sound %d times.\nDo you want to play the sound again?', counter);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(button, 'No')
playAgain = false;
break;
end
counter = counter + 1;
end
% Alert user that we are done.
message = sprintf('Done playing %s.\n', fullFileName);
fprintf('%s\n', message);
promptMessage = sprintf('Done playing %s.\nClick OK to close the window\nor Cancel to leave it up.', fullFileName);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if strcmpi(button, 'OK')
close all; % Close down the figure.
end

サインインしてコメントする。


Youssef  Khmou
Youssef Khmou 2014 年 7 月 8 日
There are many files that you can load and study using frequency analysis, here is simple example :
data=load('gong.mat');
Fs=data.Fs;
y=data.y; % vector 42028x1
sound(y,Fs)
  2 件のコメント
Christian Prinncess Usman
Christian Prinncess Usman 2014 年 7 月 8 日
is there any examples of program,for me to study on how to make it??
Image Analyst
Image Analyst 2016 年 6 月 6 日
Attached is an example.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by