view src/matlab/chromsynth.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
line wrap: on
line source
function [x,CF,CM] = chromsynth(F,bp,sr)
% [x,CF,CM] = chromsynth(F,bp,sr)
%   Resynthesize a chroma feature vector to audio
%   F is 12 rows x some number of columns, one per beat
%   bp is the period of one beat in sec (or a vector of beat times)
%   sr is the sampling rate of the output waveform
%   x is returned as a 12 semitone-spaced sines modulated by F
%   CF,CM return actual sinusoid matrices passed to synthtrax
%   Actual Shepard tones now implemented! 2007-04-19
% 2006-07-14 dpwe@ee.columbia.edu

if nargin < 2; bp = 0.5; end % 120 bpm
if nargin < 3; sr = 22050; end

[nchr,nbts] = size(F);

% resynth
if length(bp) == 1
  bups = 8; % upsampling factor
  framerate = bups/bp;
  ncols = nbts*bups;
  CMbu = zeros(nchr, ncols);
  for i = 1:bups
    CMbu = CMbu + upsample(F', bups, i-1)';
  end
else
  % vector of beat times - quantize
  framerate = 50; % frames per sec
  nbeats = length(bp);
  lastbeat = bp(nbeats) + (bp(nbeats) - bp(nbeats-1));
  ncols = round(lastbeat * framerate);
  CMbu = zeros(nchr, ncols);
  xF = [zeros(12,1),F];
  for i = 1:ncols
    CMbu(:,i) = xF(:,max(find(i/framerate >= [0,bp])));
  end
end
  
%CFbu = repmat(440*2.^([0:(nchr-1)]'/nchr),1,ncols);
%x = synthtrax(CFbu,CMbu,sr,round(sr/framerate));
octs = 7;
basefrq = 27.5;  % A1; +6 octaves = 3520
CFbu = repmat(basefrq*2.^([0:(nchr-1)]'/nchr),1,ncols);
CF = [];
CM = [];
% what bin is the center freq?
f_ctr = 440;
f_sd = 0.5;
f_bins = basefrq*2.^([0:(nchr*octs - 1)]/nchr);
f_dist = log(f_bins/f_ctr)/log(2)/f_sd;  
% actually just = ([0:(nchr*octs - 1)]/nchr-log2(f_ctr/basefrq))/f_sd
% Gaussian weighting centered of f_ctr, with f_sd
f_wts = exp(-0.5*f_dist.^2);
for oct = 1:octs
  CF = [CF;(2^oct)*CFbu];
  % pick out particular weights
  CM = [CM;diag(f_wts((oct-1)*nchr+[1:nchr]))*CMbu];
end
% Remove sines above nyquist
CFok = (CF(:,1) < sr/2);
CF = CF(CFok,:);
CM = CM(CFok,:);
% Synth the sines
x = synthtrax(CF,CM,sr,round(sr/framerate));

% Playing synth along with audio:
%>> rdc = chromsynth(Dy.F(:,160+[1:300]),Dy.bts(160+[1:300]) - Dy.bts(160),sr);
%>> Dy.bts(161)*sr              
%ans =
%      279104
%>> size(rdc)
%ans =
%           1      498401
%>> ddd = db(279104+[1:498401]);
%>> soundsc([ddd,rdc'/40],sr)