annotate src/matlab/Sound.m @ 0:c52bc3e8d3ad tip

user: boblsturm branch 'default' added README.md added assets/.DS_Store added assets/playButton.jpg added assets/stopButton.png added assets/swapButton.jpg added data/.DS_Store added data/fiveoctaves.mp3 added data/glock2.wav added data/sinScale.mp3 added data/speech_female.mp3 added data/sweep.wav added nimfks.m.lnk added src/.DS_Store added src/matlab/.DS_Store added src/matlab/AnalysisCache.m added src/matlab/CSS.m added src/matlab/DataHash.m added src/matlab/ExistsInCache.m added src/matlab/KLDivCost.m added src/matlab/LoadFromCache.m added src/matlab/SA_B_NMF.m added src/matlab/SaveInCache.m added src/matlab/Sound.m added src/matlab/SynthesisCache.m added src/matlab/chromagram_E.m added src/matlab/chromagram_IF.m added src/matlab/chromagram_P.m added src/matlab/chromsynth.m added src/matlab/computeSTFTFeat.m added src/matlab/controller.m added src/matlab/decibelSliderReleaseCallback.m added src/matlab/drawClickCallBack.m added src/matlab/fft2chromamx.m added src/matlab/hz2octs.m added src/matlab/ifgram.m added src/matlab/ifptrack.m added src/matlab/istft.m added src/matlab/nimfks.fig added src/matlab/nimfks.m added src/matlab/nmfFn.m added src/matlab/nmf_beta.m added src/matlab/nmf_divergence.m added src/matlab/nmf_euclidean.m added src/matlab/prune_corpus.m added src/matlab/rot_kernel.m added src/matlab/templateAdditionResynth.m added src/matlab/templateDelCb.m added src/matlab/templateScrollCb.m
author boblsturm
date Sun, 18 Jun 2017 06:26:13 -0400
parents
children
rev   line source
boblsturm@0 1 classdef Sound < handle
boblsturm@0 2 properties
boblsturm@0 3 Filename
boblsturm@0 4 Directory
boblsturm@0 5 Sampling_rate
boblsturm@0 6 Bits_per_sample
boblsturm@0 7 Audioplayer
boblsturm@0 8 Time_length
boblsturm@0 9 Signal
boblsturm@0 10 Features
boblsturm@0 11 end
boblsturm@0 12
boblsturm@0 13 methods
boblsturm@0 14 function obj = Sound(varargin)
boblsturm@0 15 if nargin == 1
boblsturm@0 16 [pathstr, name, ext] = fileparts(varargin{1});
boblsturm@0 17 obj.Filename = strcat(name, ext);
boblsturm@0 18 obj.Directory = pathstr;
boblsturm@0 19 elseif nargin == 2
boblsturm@0 20 obj.Signal = varargin{1};
boblsturm@0 21 obj.Sampling_rate = varargin{2};
boblsturm@0 22 end
boblsturm@0 23
boblsturm@0 24 obj.init;
boblsturm@0 25 end
boblsturm@0 26
boblsturm@0 27 function obj = init(obj)
boblsturm@0 28 if ~isempty(obj.Filename)
boblsturm@0 29 [Y, Fs] = audioread([obj.Directory filesep obj.Filename]);
boblsturm@0 30
boblsturm@0 31 %Convert to Monophonic sound
boblsturm@0 32 if(size(Y, 2) ~= 1)
boblsturm@0 33 Y = (Y(:,1)+Y(:,2))/2;
boblsturm@0 34 end
boblsturm@0 35
boblsturm@0 36 obj.Signal = Y;
boblsturm@0 37 obj.Sampling_rate = Fs;
boblsturm@0 38 end
boblsturm@0 39
boblsturm@0 40 obj.Time_length = length(obj.Signal)/obj.Sampling_rate;
boblsturm@0 41
boblsturm@0 42 obj.Audioplayer= audioplayer(obj.Signal, obj.Sampling_rate);
boblsturm@0 43 obj.Bits_per_sample = obj.Audioplayer.BitsPerSample;
boblsturm@0 44 end
boblsturm@0 45 end
boblsturm@0 46
boblsturm@0 47 methods
boblsturm@0 48 function control_audio(obj, action)
boblsturm@0 49 switch action
boblsturm@0 50 case 'play'
boblsturm@0 51 play(obj.Audioplayer);
boblsturm@0 52 case 'stop'
boblsturm@0 53 stop(obj.Audioplayer);
boblsturm@0 54 case 'pause'
boblsturm@0 55 obj.Audioplayer.pause;
boblsturm@0 56 case 'resume'
boblsturm@0 57 obj.Audioplayer.resume;
boblsturm@0 58 end
boblsturm@0 59 end
boblsturm@0 60
boblsturm@0 61 function save_audio(obj)
boblsturm@0 62 handles = guidata(gcf);
boblsturm@0 63 [file,path] = uiputfile({'*.wav'},'Save Sound As');
boblsturm@0 64 audiowrite([path filesep file], handles.SynthesisObject.Synthesis, handles.Sound_corpus.Sampling_rate);
boblsturm@0 65 end
boblsturm@0 66
boblsturm@0 67 function plot_spectrogram(obj, varargin)
boblsturm@0 68 if nargin > 1
boblsturm@0 69 mindB = varargin{1};
boblsturm@0 70 else
boblsturm@0 71 mindB = 80;
boblsturm@0 72 end
boblsturm@0 73
boblsturm@0 74 S = obj.Features.STFT.S;
boblsturm@0 75 F = obj.Features.STFT.F;
boblsturm@0 76 T = obj.Features.STFT.T;
boblsturm@0 77
boblsturm@0 78 dB = 20*log10(abs(S)/max(max(abs(S))));
boblsturm@0 79 sonodB = max(-mindB, dB);
boblsturm@0 80 imagesc(T,F./1000,sonodB);
boblsturm@0 81 cmap = colormap('jet');
boblsturm@0 82 cmap(1,:) = 0*ones(1,3);
boblsturm@0 83 colormap((cmap));
boblsturm@0 84 colorbar
boblsturm@0 85 axis xy; grid on;
boblsturm@0 86 axis([0 T(end) 0.01 10]);
boblsturm@0 87 set(gca,'XTick',[0:0.5:T(end)],'XTickLabel','');
boblsturm@0 88 set(gca, 'Layer', 'top');
boblsturm@0 89 ylabel('Frequency (kHz)');
boblsturm@0 90 grid on;
boblsturm@0 91 set(gca,'FontSize',16);
boblsturm@0 92 end
boblsturm@0 93
boblsturm@0 94 function plot_chromagram()
boblsturm@0 95 end
boblsturm@0 96
boblsturm@0 97 function plot_templates(obj)
boblsturm@0 98 W=abs(obj.Features.STFT.S);
boblsturm@0 99 F=abs(obj.Features.STFT.F);
boblsturm@0 100 hold on; grid on;
boblsturm@0 101 [~,I]=max(W);
boblsturm@0 102 [~,Ix] = sort(I,'ascend');
boblsturm@0 103 for jj=1:size(W,2)
boblsturm@0 104 specdB=W(:,Ix(jj))/max(max(W));
boblsturm@0 105 handle=plot3(jj*ones(size(W,1),1),F/1000,specdB, ...
boblsturm@0 106 'Color',power((size(W,2)-jj)/(size(W,2)+1),0.65)*ones(3,1), ...
boblsturm@0 107 'LineWidth',8*(2+size(W,2)-jj)/(size(W,2)));
boblsturm@0 108 end
boblsturm@0 109 ylabel('Frequency (kHz)');
boblsturm@0 110 xlabel('Template');
boblsturm@0 111 zlabel('Magnitude');
boblsturm@0 112 view(105,26);
boblsturm@0 113 end
boblsturm@0 114
boblsturm@0 115 function plot_signal(obj)
boblsturm@0 116 plot([1:length(obj.Signal)]/obj.Sampling_rate, obj.Signal, 'Color', [0, 0, 0]);
boblsturm@0 117 end
boblsturm@0 118
boblsturm@0 119 function obj = computeFeatures(obj, window, analysis)
boblsturm@0 120 obj.Features.window = window;
boblsturm@0 121
boblsturm@0 122 if(strcmp(analysis, 'STFT'))
boblsturm@0 123 obj.Features.STFT = computeSTFTFeat(obj.Signal, obj.Sampling_rate, obj.Features.window);
boblsturm@0 124 elseif(strcmp(analysis, 'CQT'))
boblsturm@0 125
boblsturm@0 126 elseif(strcmp(analysis, 'Chroma'))
boblsturm@0 127
boblsturm@0 128 end
boblsturm@0 129 end
boblsturm@0 130
boblsturm@0 131 function obj = concat(obj, sound)
boblsturm@0 132 obj.Signal = [ obj.Signal; sound.Signal ];
boblsturm@0 133
boblsturm@0 134 obj.Time_length = length(obj.Signal)/obj.Sampling_rate;
boblsturm@0 135
boblsturm@0 136 obj.Audioplayer= audioplayer(obj.Signal, obj.Sampling_rate);
boblsturm@0 137 obj.Bits_per_sample = obj.Audioplayer.BitsPerSample;
boblsturm@0 138 end
boblsturm@0 139 end
boblsturm@0 140 end