function make_wav_file()
clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
folder = pwd;
baseFileName = 'Test_Wave.wav';
fullFileName = fullfile(folder, baseFileName);
fprintf('Full File Name = %s\n', fullFileName);
t = 1:15000;
T = 13;
T = linspace(25, 8, length(t));
Amplitude = 32767;
Amplitude = Amplitude .* exp(-0.0003*t);
Amplitude = Amplitude .* sin(2.*pi.*t./2000);
y = int16(Amplitude .* sin(2.*pi.*t./T));
plot(t, y, 'b-');
title('Waveform', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
fprintf('Writing file %s...\n', fullFileName);
wavwrite(y, fullFileName);
playAgain = true;
counter = 1;
while playAgain
fprintf('Playing file %s %d times...\n', fullFileName, counter);
PlaySoundFile(folder, baseFileName);
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
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;
end
function PlaySoundFile(soundFolder, baseWavFileName)
global waveFileData;
global Fs;
try
if ~exist(soundFolder, 'dir')
warningMessage = sprintf('Warning: sound folder not found:\n%s', soundFolder);
WarnUser(warningMessage);
return;
end
if strcmpi(baseWavFileName, 'random')
itWorked = false;
tryCount = 1;
while itWorked == false
filePattern = fullfile(soundFolder, '*.wav');
waveFiles = dir(filePattern);
numberOfFiles = length(waveFiles);
fileToPlay = randi(numberOfFiles, 1);
baseWavFileName = waveFiles(fileToPlay).name;
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
try
if exist(fullWavFileName, 'file')
[waveFileData, Fs, nbits, readinfo] = wavread(fullWavFileName);
sound(waveFileData, Fs);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
itWorked = true;
catch
tryCount = tryCount + 1;
if tryCount >= numberOfFiles
break;
end
end
end
else
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if exist(fullWavFileName, 'file')
[waveFileData, Fs, nbits, readinfo] = wavread(fullWavFileName);
sound(waveFileData, Fs);
else
warningMessage = sprintf('Warning: sound file not found:\n%s', fullWavFileName);
WarnUser(warningMessage);
end
end
catch ME
if strfind(ME.message, '#85')
fprintf('Error in PlaySoundFile(): %s.\nUnrecognized sound format in file:\n\n%s\n', ME.message, fullWavFileName);
baseWavFileName = 'Chime.wav';
fullWavFileName = fullfile(soundFolder, baseWavFileName);
waveFileData = -1;
if exist(fullWavFileName, 'file')
[waveFileData, Fs, nbits, readinfo] = wavread(fullWavFileName);
sound(waveFileData, Fs);
end
end
errorMessage = sprintf('Error in PlaySoundFile().\nThe error reported by MATLAB is:\n\n%s', ME.message);
fprintf('%s\n', errorMessage);
WarnUser(errorMessage);
end
return;
Best Answer