Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- addEcho('input.wav', 'output_with_echo.wav', 2000);
- function addEcho(inputWavefile, outputWavefile, circularBuffSize)
- % Read the input wavefile
- [inputSignal, sampleRate] = audioread(inputWavefile);
- % Initialize circular buffer and index
- circularBuffer = zeros(circularBuffSize, size(inputSignal, 2));
- bufferIndex = uint32(1);
- % Initialize delay parameters (adjust as needed)
- feedback = 0.5; % Adjust the feedback factor (between 0 and 1)
- % Create an output signal with the same size as the input signal
- outputSignal = zeros(size(inputSignal));
- % Apply the echo effect for each channel
- for i = 1:size(inputSignal, 2)
- for j = 1:length(inputSignal)
- % Get the delayed sample from the circular buffer
- delayedSample = circularBuffer(bufferIndex, i);
- % Add the delayed sample to the output signal
- outputSignal(j, i) = inputSignal(j, i) + feedback * delayedSample;
- % Update the circular buffer with the current sample
- circularBuffer(bufferIndex, i) = inputSignal(j, i);
- % Increment the circular buffer index
- bufferIndex = bufferIndex + 1;
- % Wrap around the circular buffer index
- if bufferIndex > circularBuffSize
- bufferIndex = uint32(1);
- end
- end
- end
- % Normalize the output signal to prevent clipping
- maxAmplitude = max(abs(outputSignal(:)));
- if maxAmplitude > 1
- outputSignal = outputSignal / maxAmplitude;
- end
- % Write the output to a wavefile
- audiowrite(outputWavefile, outputSignal, sampleRate);
- % Plot the input and output signals
- figure;
- subplot(2, 1, 1);
- plot(inputSignal);
- title('Input Waveform');
- subplot(2, 1, 2);
- plot(outputSignal);
- title('Output Waveform');
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement