當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Processing AudioSample.read()用法及代碼示例

Processing, 類AudioSample中的read()用法介紹。

用法

  • .read(data)
  • .read(startFrame, data, startIndex, numFrames)
  • .read(frameIndex)
  • .read(frameIndex, channelIndex)

參數

  • data (float[]) 讀取數據寫入的目標數組
  • startFrame (int) 應該讀取的音頻樣本的第一幀的索引
  • startIndex (int) 數組中第一個讀取幀應寫入的位置(通常為 0)
  • numFrames (int) 應該讀取的幀數(不能大於 audiosample.channels() * data.length - startIndex)
  • frameIndex (int) 應讀取並返回的音頻樣本的單幀索引。 `frameIndex` 必須介於 0 和 `sample.frames() * sample.channels() - 1`(含)`之間。對於單聲道文件,`read(frameIndex)` 與`read(frameIndex, 0)` 相同。對於立體聲文件,除非您還指定 `channelIndex`,否則“read(frameIndex)”將按交錯順序從左右聲道返回樣本。 (有關演示,請參見聲音文件 > StereoSample 示例。)
  • channelIndex (int) 從中提取幀值的通道(0 表示左,1 表示右)。 `read(frameIndex, channelIndex)` 與調用 `read(frameIndex * this.channels() + channelIndex)` 相同。

返回

  • void or float

說明

可以通過幾種不同的方式讀取和寫入音頻樣本的底層數據:采用單個浮點數組`data` 的方法獲取當前樣本數據並將其寫入給定數組。該數組必須能夠存儲與此示例中的幀一樣多的浮點數。也可以使用帶有四個參數的方法隻讀取部分樣本數據,它允許您指定要讀取的第一幀的索引、要寫入的數組中的位置以及要寫入的幀數總共複製到數組中。最後,采用單個整數參數 `index` 的方法返回此索引處樣本的單個音頻幀的值作為浮點數。

例子

import processing.sound.*;
AudioSample sample;

void setup() {
  size(640, 360);
  background(255);

  // Create a new audiosample
  sample = new AudioSample(this, 100000, 22050);

  // Read some data from it (the following calls are just for demonstration,
  // a freshly initiated audiosample actually contains nothing but zeros)

  // Read the very first frame:
  float frame = sample.read(0);

  // Read the entire sample
  float[] sampleContent = new float[100000];
  sample.read(sampleContent);

  // Read only a part of the sample data
  float[] subSample = new float[50000];
  // Read 500000 frames, starting at frame 30000
  sample.read(30000, subSample, 0, 50000);
}      

void draw() {
}

相關用法


注:本文由純淨天空篩選整理自processing.org大神的英文原創作品 AudioSample.read()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。