Off[General::"spell1"];
f0=440.;
nharm=31;
sr=32000;
rd1:=Random[Real,{-.5,.5}]
rd2:=Random[Real,{0,1.8}]
tauSude=3.;
kASude=0.9;
tauLiche=3.;
KALiche=0.9;
sude1[i_]:=kASude*0.5*(1-(((-1))^i))*Exp[((-i*tauSude)/nharm)];
S1=Prepend[sude1/@Range[nharm],1];
liche1[i_]:=KALiche*0.5*(1+(((-1))^i))*Exp[((-i*tauLiche)/nharm)];
L1=Prepend[liche1/@Range[nharm],1];
ListPlot[S1,PlotStyle->{PointSize[0.02]},PlotRange->All];
ListPlot[L1,PlotStyle->{PointSize[0.02]},PlotRange->All];

tonL[k_,t_]=(Sin[k*f0*2*Pi*#*t]&/@Range[nharm+1]).L1;
tonS[k_,t_]=(Sin[k*f0*2*Pi*#*t]&/@Range[nharm+1]).S1;

pyth1={1,(2187/2048),(9/8),(32/27),(81/64),(4/3),(729/512),(3/2),(128/81),(27/16),(16/9),(243/128),2};
temper=Table[((2.^i)^(1/12)),{i,0,12}];
fPyth[k_Integer]:=1.*pyth1[[k]]/;1≤k≤12;
fPyth[k_Integer]:=2.*fPyth[k-12]/;k>12;
fPyth[k_Integer]:=0.5*fPyth[k+12]/;k<1;
fTemper[k_Integer]:=1.*temper[[k]]/;1≤k≤12;
fTemper[k_Integer]:=2.*fTemper[k-12]/;k>12;
fTemper[k_Integer]:=0.5*fTemper[k+12]/;k<1;

fceprot[list_,doba_]:=Module[{length,pom},length=Round[doba*sr];pom=NestWhile[Join[#,#]&,list,Length[#]<length&];Take[pom,length]];

k0=-56;
k01=70;
Print["f do k01= ",k0,"   ",f0*fPyth[k0], " a odpovidajici perioda T= ",(((f0*fPyth[k0]))^(-1))," s"];
Print["f do k01= ",k01,"   ",f0*fPyth[k01], " a odpovidajici perioda T= ",(((f0*fPyth[k01]))^(-1))," s"]
fPyth/@Table[k,{k,k0,k01}];
fTemper/@Table[k,{k,k0,k01}];
ListPlot[%-%%,PlotStyle->{PointSize[0.015]},PlotRange->All];
aK[k_]:=0.01+Exp[((8.*(k0-k))/((k01-k0)))];
Plot[aK[k],{k,k0,k01},PlotRange->{Automatic,{0,1}}]

kT=1;
PsampleS[k_]:=Module[{f,T,t,tau},f=f0*fPyth[k];T=(1./f);tau=(1./sr);Table[tonS[fPyth[k],t],{t,0,kT*T,tau}]];
PsampleL[k_]:=Module[{f,T,t,tau},f=f0*fPyth[k];T=(1./f);tau=(1./sr);Table[tonL[fPyth[k],t],{t,0,kT*T,tau}]];
PtonyS=Table[aK[k]*PsampleS[k],{k,k0,k01}];
PtonyL=Table[aK[k]*PsampleL[k],{k,k0,k01}];
ListPlot[PtonyS[[10]],PlotStyle->{PointSize[0.01]},PlotRange->All];

TsampleS[k_]:=Module[{f,T,t,tau},f=f0*fTemper[k];T=(1./f);tau=(1./sr);Table[tonS[fTemper[k],t],{t,0,kT*T,tau}]];
TsampleL[k_]:=Module[{f,T,t,tau},f=f0*fTemper[k];T=(1./f);tau=(1./sr);Table[tonL[fTemper[k],t],{t,0,kT*T,tau}]];
TtonyS=Table[aK[k]*TsampleS[k],{k,k0,k01}];
TtonyL=Table[aK[k]*TsampleL[k],{k,k0,k01}];
NullTony=0.*Range[1000];

PCohrajL[{k_,a_,long_}]:=If[k0≤k≤k01,fceprot[a*PtonyL[[k-k0+1]],long],fceprot[NullTony,long]];
PCohrajS[{k_,a_,long_}]:=If[k0≤k≤k01,fceprot[a*PtonyS[[k-k0+1]],long],fceprot[NullTony,long]];
PCohraj[co_]:=PCohrajS[co]+PCohrajL[co];
TCohrajL[{k_,a_,long_}]:=If[k0≤k≤k01,fceprot[a*TtonyL[[k-k0+1]],long],fceprot[NullTony,long]];
TCohrajS[{k_,a_,long_}]:=If[k0≤k≤k01,fceprot[a*TtonyS[[k-k0+1]],long],fceprot[NullTony,long]];
TCohraj[co_]:=TCohrajS[co]+TCohrajL[co];

ClearAll[rejstrik1];
rejstrik1[{zakl_,rej_,interval_},co:{k_,a_,long_}]:=Module[{fzakl,frej,ki,ai},fzakl[ki_,ai_]:=zakl[{ki,ai,long}];frej[ki_,ai_]:=rej[{ki,ai,long}];fzakl[k,2*a]+frej[k+12+interval,a]+frej[k+24+interval,a*0.2]+frej[k+36,a*0.1]+frej[k+48,a*0.05]+frej[k-12,a*0.4]];
kvinta[{k_,long_}]:=rejstrik1[{TCohrajS,PCohrajS,8},{k,1,long}];
kvintaTry[{k_,long_}]:=rejstrik1[{TCohrajS,PCohrajL,8},{k,1,long}];
ListPlot[kvinta[{-20,.2}]];

zahraj[cohraj_]:=EmitSound[ListPlay[cohraj,SampleRate->sr,PlayRange->{-1.pLayrAnge,1.pLayrAnge}]];

osminka=.25;
takt=3;
ntaktu=30;
pLayrAnge=5;

del=1;
ton=-15;
zahraj[cohraj_]:=EmitSound[ListPlay[cohraj,SampleRate->sr,PlayRange->{-1.pLayrAnge,1.pLayrAnge}]];
zahraj[{kvinta[{ton,del}],kvinta[{ton-24,del}]}]

nvtaktu=8;
ClearAll[fK];
fK[{Δ_,list_}]:=Module[{fkv,fxx,x,pom},pom=PadRight[list,Round[((8*osminka)/Δ)], k0-100];fkv:=kvinta[{#,Δ}]&;fxx[x_?NumberQ]:=fkv[x];
fxx[x_List]:=Length[x]*Mean[fkv/@x];pom=Flatten[fxx/@pom]];
tryVstup={osminka,{-30,{-29,58},{-28,4,56},5,1,8,-20,-30}};
zahraj[fK/@{tryVstup}]