Advertisement
STANAANDREY

apd echo

Oct 12th, 2023
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.90 KB | None | 0 0
  1. addEcho('input.wav', 'output_with_echo.wav', 2000);
  2.  
  3. function addEcho(inputWavefile, outputWavefile, circularBuffSize)
  4.     % Read the input wavefile
  5.     [inputSignal, sampleRate] = audioread(inputWavefile);
  6.    
  7.     % Initialize circular buffer and index
  8.     circularBuffer = zeros(circularBuffSize, size(inputSignal, 2));
  9.     bufferIndex = uint32(1);
  10.  
  11.     % Initialize delay parameters (adjust as needed)
  12.     feedback = 0.5;      % Adjust the feedback factor (between 0 and 1)
  13.  
  14.     % Create an output signal with the same size as the input signal
  15.     outputSignal = zeros(size(inputSignal));
  16.  
  17.     % Apply the echo effect for each channel
  18.     for i = 1:size(inputSignal, 2)
  19.         for j = 1:length(inputSignal)
  20.             % Get the delayed sample from the circular buffer
  21.             delayedSample = circularBuffer(bufferIndex, i);
  22.            
  23.             % Add the delayed sample to the output signal
  24.             outputSignal(j, i) = inputSignal(j, i) + feedback * delayedSample;
  25.            
  26.             % Update the circular buffer with the current sample
  27.             circularBuffer(bufferIndex, i) = inputSignal(j, i);
  28.  
  29.             % Increment the circular buffer index
  30.             bufferIndex = bufferIndex + 1;
  31.  
  32.             % Wrap around the circular buffer index
  33.             if bufferIndex > circularBuffSize
  34.                 bufferIndex = uint32(1);
  35.             end
  36.         end
  37.     end
  38.  
  39.     % Normalize the output signal to prevent clipping
  40.     maxAmplitude = max(abs(outputSignal(:)));
  41.     if maxAmplitude > 1
  42.         outputSignal = outputSignal / maxAmplitude;
  43.     end
  44.  
  45.     % Write the output to a wavefile
  46.     audiowrite(outputWavefile, outputSignal, sampleRate);
  47.  
  48.     % Plot the input and output signals
  49.     figure;
  50.     subplot(2, 1, 1);
  51.     plot(inputSignal);
  52.     title('Input Waveform');
  53.     subplot(2, 1, 2);
  54.     plot(outputSignal);
  55.     title('Output Waveform');
  56. end
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement