Generating some sounds and present all of the sound randomly to left and right ear

2 次查看(过去 30 天)
Hello every body, I generate 45 sounds and put all of the in a cell array(5*3*3)and right now I want present all of sounds in left and right ear randomly but I can not. I mean all of sounds should select one time as well as randomly. please help me if you know it. (Thanks in advance).
here is my code that I wrote:
I have problem in line (30)
%%line 30 %% In this part I have problem, I want to present all of sounds that are inside the memory to left and right randomly as well as without repetition and all sounds should be select.
clear;
clc;
close all;
SamplingRate_Hz=48000;
F=[250 500 1000 2000 4000];
A=[5 10 15];
T=[1 2 3];
memoryR{1,1}{1,1}{1,1}=0;
memoryL{1,1}{1,1}{1,1}=0;
memory{1,1}{1,1}{1,1}=0;
for i=1:length(F)
for j=1:length(T)
for k=1:length(A)
Frequency_Hz = F(1,i);
time_p = T(1,j);
Amplitude = A(1,k);
t=0:(1/SamplingRate_Hz):(time_p-1/Frequency_Hz);
y=Amplitude*sin(2*pi*Frequency_Hz*t);
xt_ramped = toneburst(SamplingRate_Hz,y);
OutR = [zeros(size(t)); xt_ramped]'; % Right
OutL = [ xt_ramped ;zeros(size(t))]'; % Left
%%Right
memoryR{1,i}{1,j}{1,k} = OutR;
%%left
memoryL{1,i}{1,j}{1,k} = OutL;
end
end
end
%% In this part I have problem, I want to present all of sounds that are inside the memory to left and right randomly as well as without repetition and all sounds should be select.
memory={memoryL memoryR};
%%
%% here is my function(toneburst)
function xt_ramped = toneburst(SamplingRate_Hz,y)
xt =y;
fs=SamplingRate_Hz;
ramp_dur=0.0025; %ramp duration in seconds
%setup ramp
rampSamps = floor(fs*ramp_dur);
window=hanning(2*rampSamps)'; %hanning window is cosine^2 this will change depending on the kind of ramp you want
w1=window(1:ceil((length(window))/2)); %use the first half of hanning function for onramp
w2=window(ceil((length(window))/2)+1:end); %use second half of hanning function of off ramp
w_on_xt = [w1 ones(1,length(xt)-length(w1))];
w_off_xt = [ones(1,length(xt)-length(w2)) w2];
xt_ramped = xt.*w_on_xt.*w_off_xt;
end
PLEASE HELP ME EVERYBODY
  1 个评论
Mathieu NOE
Mathieu NOE 2021-9-3
hello
there is one first point that surprises me ; I believe the goal is to have at the end 45 sections of 2 channels audio and play them randomly (does the R and L channels be played randomly independently ? they have anyway to be of same time length...
now I don't understand in your code why OutR and OutL have already 2 channels (their dimension is length(t) , 2 ) .
I would guess that they should be only 1 channel data and then you combine both to make your stereo audio ?

请先登录,再进行评论。

采纳的回答

Mathieu NOE
Mathieu NOE 2021-9-3
hello again
we need to clarify a bit the data you generate (see my comment above) , but basically this is your code a bit modified to generate a stream of the 45 individual sounds put together in a random manner
so we are quite close to your goal , just precise how the right and left audio data should be organized as for the time being I believe we have to much channels ( 2 x 2 insted of simply 2 )
clearvars;
clc;
SamplingRate_Hz=48000;
F=[250 500 1000 2000 4000];
A=[5 10 15]/15; % amplitude should be +/- max for wav export
T=[1 2 3];
% memoryR{1,1}{1,1}{1,1}=0;
% memoryL{1,1}{1,1}{1,1}=0;
% memory{1,1}{1,1}{1,1}=0;
ind = 0;
for i=1:length(F)
for j=1:length(T)
for k=1:length(A)
ind = ind + 1;
Frequency_Hz = F(i);
time_p = T(j);
Amplitude = A(k);
t=0:(1/SamplingRate_Hz):(time_p-1/Frequency_Hz);
y=Amplitude*sin(2*pi*Frequency_Hz*t);
xt_ramped = toneburst(SamplingRate_Hz,y);
OutR = [zeros(size(t)); xt_ramped]'; % Right
OutL = [ xt_ramped ;zeros(size(t))]'; % Left
% %%Right
% memoryR{1,i}{1,j}{1,k} = OutR;
% %%left
% memoryL{1,i}{1,j}{1,k} = OutL;
%%Right
memoryR{1,ind} = OutR;
%%left
memoryL{1,ind} = OutL;
end
end
end
% %% In this part I have problem, I want to present all of sounds that are inside the memory to left and right randomly
% as well as without repetition and all sounds should be select.
% memory={memoryL memoryR};
% %%
r = randperm(ind); % create 45 random values
out_all = [];
for ci = 1:ind
out_all = [out_all; memoryR{1,r(ci)}];
end
% export to wav
audiowrite('testR.wav',out_all,SamplingRate_Hz);
%% here is my function(toneburst)
function xt_ramped = toneburst(SamplingRate_Hz,y)
xt =y;
fs=SamplingRate_Hz;
ramp_dur=0.0025; %ramp duration in seconds
%setup ramp
rampSamps = floor(fs*ramp_dur);
window=hanning(2*rampSamps)'; %hanning window is cosine^2 this will change depending on the kind of ramp you want
w1=window(1:ceil((length(window))/2)); %use the first half of hanning function for onramp
w2=window(ceil((length(window))/2)+1:end); %use second half of hanning function of off ramp
w_on_xt = [w1 ones(1,length(xt)-length(w1))];
w_off_xt = [ones(1,length(xt)-length(w2)) w2];
xt_ramped = xt.*w_on_xt.*w_off_xt;
end
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by