Android Open Source - voicesmith Stft Preprocessor






From Project

Back to project page voicesmith.

License

The source code is released under:

GNU General Public License

If you think the Android project voicesmith listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Voicesmith <http://voicesmith.jurihock.de/>
 *//from  w w w.  j a  v  a 2s. c om
 * Copyright (c) 2011-2014 Juergen Hock
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.jurihock.voicesmith.dsp.stft;

import static de.jurihock.voicesmith.dsp.Math.max;
import static de.jurihock.voicesmith.dsp.Math.min;
import de.jurihock.voicesmith.Disposable;
import de.jurihock.voicesmith.Preferences;
import de.jurihock.voicesmith.dsp.KissFFT;
import de.jurihock.voicesmith.dsp.Window;
import de.jurihock.voicesmith.dsp.processors.AmplifyProcessor;
import de.jurihock.voicesmith.dsp.processors.DenoiseProcessor;
import de.jurihock.voicesmith.dsp.processors.OffsetProcessor;
import de.jurihock.voicesmith.dsp.processors.VadProcessor;
import de.jurihock.voicesmith.io.AudioDevice;

/**
 * Reads frames referred to "Constant Overlapp And Add" procedure from the audio
 * source. Frames get shifted by the given hop size, normalized, weighted,
 * optional denoised and transformed into frequency domain.
 * */
public final class StftPreprocessor implements Disposable
{
  private final AudioDevice    input;
  private final int        frameSize;
  private final int        hopSize;
  private final boolean      doForwardFFT;

    private final OffsetProcessor   deoffset;
  private final VadProcessor     vad;
    private final AmplifyProcessor  amplifier;
    private final DenoiseProcessor  denoiser;

  private KissFFT          fft  = null;
  private final float[]      window;

  private final short[]      prevFrame, nextFrame;
  private int            frameCursor;

  public StftPreprocessor(AudioDevice input, int frameSize, int hopSize, boolean doForwardFFT)
  {
    this.input = input;
    this.frameSize = frameSize;
    this.hopSize = hopSize;
    this.doForwardFFT = doForwardFFT;

        deoffset = new OffsetProcessor(input.getContext());
    vad = new VadProcessor(input.getSampleRate(), input.getContext());
    amplifier = new AmplifyProcessor(input.getContext());
        denoiser = new DenoiseProcessor(input.getSampleRate(), input.getContext());

    fft = new KissFFT(frameSize);
    window = new Window(frameSize, true).hann();

    prevFrame = new short[frameSize];
    nextFrame = new short[frameSize];
    frameCursor = -1;
  }

  public void dispose()
  {
    if (fft != null)
    {
      fft.dispose();
      fft = null;
    }
  }

  public void processFrame(float[] frame)
  {
    // Handle the first frame
    if (frameCursor == -1)
    {
      frameCursor = frameSize;
      input.read(nextFrame);

      // Pre-preprocess frame
            deoffset.processFrame(nextFrame);
            vad.processFrame(nextFrame);
      amplifier.processFrame(nextFrame);
    }
    // Handle frame cursor
    else if (frameCursor >= frameSize)
    {
      // Reset frame cursor
      frameCursor -= frameSize;

      // Prepare frame buffers
      System.arraycopy(nextFrame, 0, prevFrame, 0, frameSize);

      // Read next frame
      input.read(nextFrame);

            // Pre-preprocess frame
            deoffset.processFrame(nextFrame);
            vad.processFrame(nextFrame);
            amplifier.processFrame(nextFrame);
    }

    // Prepare left frame part
    analyzeFrame(
      prevFrame, frameCursor,
      frame, 0,
      frameSize - frameCursor,
      window);

    // Prepare right frame part
    analyzeFrame(
      nextFrame, 0,
      frame, frameSize - frameCursor,
      frameCursor,
      window);

    if (doForwardFFT)
        {
            fft.fft(frame);
            denoiser.processFrame(frame);
        }

    // Increment frame cursor
    frameCursor += hopSize;
  }

  /**
   * Performs the analysis step with weighting.
   * */
  private static void analyzeFrame(short[] src, int offsetSrc, float[] dst, int offsetDst, int count, float[] window)
  {
    if (count == 0) return;

    for (int i = 0; i < count; i++)
    {
      // Get and normalize source value
      float value = (float) src[i + offsetSrc] / 32767F;
      value = min(1F, max(-1F, value));

      // Multiply with window coefficient
      value *= window[i + offsetDst];

      // Copy it to destination array
      dst[i + offsetDst] = value;
    }
  }
}




Java Source Code List

de.jurihock.voicesmith.AAF.java
de.jurihock.voicesmith.Application.java
de.jurihock.voicesmith.ChangeLog.java
de.jurihock.voicesmith.DAFX.java
de.jurihock.voicesmith.Disposable.java
de.jurihock.voicesmith.FrameType.java
de.jurihock.voicesmith.Preferences.java
de.jurihock.voicesmith.Utils.java
de.jurihock.voicesmith.activities.AafActivity.java
de.jurihock.voicesmith.activities.AboutActivity.java
de.jurihock.voicesmith.activities.AboutApplicationActivity.java
de.jurihock.voicesmith.activities.AboutLicenseActivity.java
de.jurihock.voicesmith.activities.AudioServiceActivity.java
de.jurihock.voicesmith.activities.ContributionActivity.java
de.jurihock.voicesmith.activities.DafxActivity.java
de.jurihock.voicesmith.activities.HomeActivity.java
de.jurihock.voicesmith.activities.PreferenceActivity.java
de.jurihock.voicesmith.activities.SupportActivity.java
de.jurihock.voicesmith.audio.AudioDeviceManager.java
de.jurihock.voicesmith.audio.HeadsetManagerListener.java
de.jurihock.voicesmith.audio.HeadsetManager.java
de.jurihock.voicesmith.audio.HeadsetMode.java
de.jurihock.voicesmith.dsp.KissFFT.java
de.jurihock.voicesmith.dsp.LuenbergerObserver.java
de.jurihock.voicesmith.dsp.Math.java
de.jurihock.voicesmith.dsp.SchmittTrigger.java
de.jurihock.voicesmith.dsp.Window.java
de.jurihock.voicesmith.dsp.processors.AmplifyProcessor.java
de.jurihock.voicesmith.dsp.processors.DenoiseProcessor.java
de.jurihock.voicesmith.dsp.processors.DetuneProcessor.java
de.jurihock.voicesmith.dsp.processors.HoarsenessProcessor.java
de.jurihock.voicesmith.dsp.processors.NativeResampleProcessor.java
de.jurihock.voicesmith.dsp.processors.NativeTimescaleProcessor.java
de.jurihock.voicesmith.dsp.processors.OffsetProcessor.java
de.jurihock.voicesmith.dsp.processors.ResampleProcessor.java
de.jurihock.voicesmith.dsp.processors.RobotizeProcessor.java
de.jurihock.voicesmith.dsp.processors.SeparationProcessor.java
de.jurihock.voicesmith.dsp.processors.TimescaleProcessor.java
de.jurihock.voicesmith.dsp.processors.VadProcessor.java
de.jurihock.voicesmith.dsp.stft.StftPostprocessor.java
de.jurihock.voicesmith.dsp.stft.StftPreprocessor.java
de.jurihock.voicesmith.io.AudioDevice.java
de.jurihock.voicesmith.io.dummy.DummyInDevice.java
de.jurihock.voicesmith.io.dummy.DummyOutDevice.java
de.jurihock.voicesmith.io.file.FileDevice.java
de.jurihock.voicesmith.io.file.FileInDevice.java
de.jurihock.voicesmith.io.file.FileOutDevice.java
de.jurihock.voicesmith.io.oscillators.CosineWave.java
de.jurihock.voicesmith.io.oscillators.PhaseAccumulator.java
de.jurihock.voicesmith.io.pcm.DelayedPcmInDevice.java
de.jurihock.voicesmith.io.pcm.PcmDevice.java
de.jurihock.voicesmith.io.pcm.PcmInDevice.java
de.jurihock.voicesmith.io.pcm.PcmOutDevice.java
de.jurihock.voicesmith.services.AafService.java
de.jurihock.voicesmith.services.AudioService.java
de.jurihock.voicesmith.services.DafxService.java
de.jurihock.voicesmith.services.ServiceBinder.java
de.jurihock.voicesmith.services.ServiceFailureReason.java
de.jurihock.voicesmith.services.ServiceListener.java
de.jurihock.voicesmith.threads.AudioThread.java
de.jurihock.voicesmith.threads.DelayThread.java
de.jurihock.voicesmith.threads.DetuneThread.java
de.jurihock.voicesmith.threads.HoarsenessThread.java
de.jurihock.voicesmith.threads.LowDelayThread.java
de.jurihock.voicesmith.threads.RobotizeThread.java
de.jurihock.voicesmith.threads.TransposeThread.java
de.jurihock.voicesmith.voicebank.Record.java
de.jurihock.voicesmith.voicebank.RecordsSerializer.java
de.jurihock.voicesmith.voicebank.Records.java
de.jurihock.voicesmith.widgets.AafPicker.java
de.jurihock.voicesmith.widgets.ColoredToggleButton.java
de.jurihock.voicesmith.widgets.DafxPicker.java
de.jurihock.voicesmith.widgets.DelayPicker.java
de.jurihock.voicesmith.widgets.IntervalPicker.java
de.jurihock.voicesmith.widgets.ListPreference.java
de.jurihock.voicesmith.widgets.SeekBarPreference.java