捕获音频并将其可视化为android

时间:2014-01-27 06:21:29

标签: java android android-canvas

您好我使用画布绘制捕获的音频记录我有这个代码,但有些如何它给我音频信号的fft我不知道代码的哪一部分做到了?我该怎么做才能让它自己绘制波形而不是fft ??

public void doDraw(Canvas paramCanvas)
{
  if (mCanvasHeight == 1)
    mCanvasHeight = paramCanvas.getHeight();
  paramCanvas.drawPaint(mBackPaint); 
  /**
   * Set some base values as a starting point
   * This could be considerd as a part of the calculation process
   */
  int height = paramCanvas.getHeight();
  int BuffIndex = (mBuffer.length / 2 - paramCanvas.getWidth()) / 2;
  int width = paramCanvas.getWidth();
  int mBuffIndex = BuffIndex;
  int scale = height / m_iScaler;
  int StratX = 0;
  if (StratX >= width)
  {
    paramCanvas.save();
    return;
  }
int cu1 = 0;
/**
 * Here is where the real calculations is taken in to action
 * In this while loop, we calculate the start and stop points
 * for both X and Y
 * 
 * The line is then drawer to the canvas with drawLine method
 */
while (StratX < width -1)
{
  int StartBaseY = mBuffer[(mBuffIndex - 1)] / scale;

  int StopBaseY = mBuffer[mBuffIndex] / scale;
  if (StartBaseY > height / 2)
  {
      StartBaseY = 1 + height / 2;
    int checkSize = height / 2;
    if (StopBaseY <= checkSize)
        return;
    StopBaseY = 2 + height / 2;
  }

    int StartY = StartBaseY + height / 2;
    int StopY = StopBaseY + height / 2;
    paramCanvas.drawLine(StratX, StartY, StratX +1, StopY, mLinePaint);
    cu1++;
    mBuffIndex++;
    StratX++;
    int checkSize_again = -1 * (height / 2);
    if (StopBaseY >= checkSize_again)
      continue;
    StopBaseY = -2 + -1 * (height / 2);
  }
}

因此,主要活动基本上调用CSampler类中的三个函数

init() //which prepares the audio record and sets its configuration
Start recording() // starts the audio recorder
StartSampling() // reads data into CSampler.buffer;

名为

的StartSampling函数
public void StartSampling()
{
recordingThread = new Thread()
{
  public void run()
  {
    while (true)
    {
      if (!m_bRun.booleanValue())
      {
        m_bDead = Boolean.valueOf(true);
        m_bDead2 = Boolean.valueOf(true);
        return;
      }
      Sample();

      m_ma.setBuffer(CSampler.buffer); //m_ma is object of main activity 
    }
   }
 };
 recordingThread.start();

}


主要活动中的函数setBuffer

/** 
 * Recives the buffer from the sampler
 * @param buffert
 */
public void setBuffer(short[] paramArrayOfShort)
{
  mDrawThread = mdrawer.getThread();
  mDrawThread.setBuffer(paramArrayOfShort);
}

它基本上调用了CDrawer类中的另一个函数

CDrawer类中的函数SetBuffer使用相同的读取数据设置mbuffer = /

public void setBuffer(short[] paramArrayOfShort)
{
  synchronized (mBuffer)
  {
    mBuffer = paramArrayOfShort;
    return;
  }
}

public void Sample()
{
mSamplesRead = ar.read(buffer, 0, buffersizebytes);
}
___________________________________________________

output pic


enter image description here

1 个答案:

答案 0 :(得分:0)

实际上,显示屏显示非常短的音频信号波形。更确切地说,每个屏幕像素垂直线显示信号的一个样本值。由于信号采用44.1kHz采样,因此每秒有44100个采样值。如果您的屏幕的物理像素宽度为768像素,那么您会看到768/44100 = 0.017s = 17ms的信号。

这是相当短暂的:通过典型的男性声音,您将看到基频的两个周期。

通过快速更改信号源,您可以看到更多的信号波形:只需将SAMPPERSEC = 44100更改为例如SAMPPERSEC = 16000;

代码不包含对傅里叶变换或任何其他光谱变换的任何调用,因此它根本无法显示光谱。