Advertisement
STANAANDREY

flanger

Oct 23rd, 2023 (edited)
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.05 KB | None | 0 0
  1. inputFilePath = 'input.wav';
  2. outputFilePath = 'output_flanger.wav';
  3. maxBufferSize = 1000;
  4. minBufferSize = 200;
  5. modulationDepth = 0.5;
  6.  
  7. flanger(inputFilePath, outputFilePath, maxBufferSize, minBufferSize, modulationDepth);
  8. function flanger(inputWavefile, outputWavefile, maxBufferSize, minBufferSize, modDepth)
  9.     % Read the input wavefile
  10.     [inputSignal, Fs] = audioread(inputWavefile);
  11.    
  12.     % Initialize circular buffer and other parameters
  13.     circularBuffer = zeros(maxBufferSize, 2);
  14.     bufferIndex = uint32(1);
  15.     modFrequency = 0.25; % You can adjust this value
  16.    
  17.     % Prepare the output signal
  18.     outputSignal = zeros(size(inputSignal));
  19.    
  20.     % Create time vector for plotting
  21.     t = (0:length(inputSignal) - 1) / Fs;
  22.    
  23.     % Initialize plots
  24.     figure;
  25.    
  26.     % Plot the input wave
  27.     subplot(2, 1, 1);
  28.     plot(t, inputSignal);
  29.     title('Input Wave');
  30.     xlabel('Time (s)');
  31.     ylabel('Amplitude');
  32.    
  33.     % Apply flanger effect
  34.     for n = 1:length(inputSignal)
  35.         % Calculate delay time using modulation
  36.         delayTime = minBufferSize + modDepth * sin(2 * pi * modFrequency * n / Fs);
  37.         delaySamples = round(delayTime * Fs);
  38.        
  39.         % Circular buffer read
  40.         readIndex = mod(bufferIndex - delaySamples - 1, maxBufferSize) + 1;
  41.         delayedSample = mean(circularBuffer(readIndex, :));
  42.        
  43.         % Apply the flanger effect to the input sample
  44.         outputSignal(n) = inputSignal(n) + delayedSample;
  45.        
  46.         % Circular buffer write
  47.         circularBuffer(bufferIndex, :) = inputSignal(n, :);
  48.         bufferIndex = mod(bufferIndex, maxBufferSize) + 1;
  49.     end
  50.    
  51.     % Normalize the output signal to prevent clipping
  52.     outputSignal = outputSignal / max(abs(outputSignal));
  53.    
  54.     % Plot the output wave
  55.     subplot(2, 1, 2);
  56.     plot(t, outputSignal);
  57.     title('Output Wave (Flanger Effect)');
  58.     xlabel('Time (s)');
  59.     ylabel('Amplitude');
  60.    
  61.     % Save the output wave to a new file
  62.     audiowrite(outputWavefile, outputSignal, Fs);
  63. end
  64.  
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement