当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。