0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TeensyでAudio処理のバッファサイズと優先度を変える

Last updated at Posted at 2021-03-15

バッファサイズ

TeensyではAudio処理のバッファサイズがデフォルトで128固定だが、これは変えることができる。~/Library/Arduino15/packages/teensy/avr/cores/teensy4/AudioStream.h (Macの場合) を見るとAUDIO_BLOCK_SAMPLESをAudio.hをインクルードする前に定義すれば良いように見える。

AudioStream.h
// AUDIO_BLOCK_SAMPLES determines how many samples the audio library processes
// per update.  It may be reduced to achieve lower latency response to events,
// at the expense of higher interrupt and DMA setup overhead.
//
// Less than 32 may not work with some input & output objects.  Multiples of 16
// should be used, since some synthesis objects generate 16 samples per loop.
//
// Some parts of the audio library may have hard-coded dependency on 128 samples.
// Please report these on the forum with reproducible test cases.  The following
// audio classes are known to have problems with smaller block sizes:
//   AudioInputUSB, AudioOutputUSB, AudioPlaySdWav, AudioAnalyzeFFT256,
//   AudioAnalyzeFFT1024

#ifndef AUDIO_BLOCK_SAMPLES
#define AUDIO_BLOCK_SAMPLES  128
#endif

ただ、なんかそう定義しても動かないことがあったので、このファイルの値を直接書き換えている。16の倍数で、となっているが8までは動作確認できた。4にしたら動かなかった。

優先度

~/Library/Arduino15/packages/teensy/hardware/teensy/hardware/avr/1.59.0/cores/teensy4/AudioStream.cppを開き、"priority"で検索して見つかる下記の部分を変更するとたぶん変えられる。デフォルトの208だとCPU負荷が高くなった時にバッファアンダーランが起きたので、128に変更して使っている。もっと小さい値(優先度が高くなる)にしたら、I2Sの入力をパススルーしている部分でバリバリとノイズが乗ったので、優先度を高くしすぎてもダメなケースがある。

AudioStream.cpp
bool AudioStream::update_setup(void)
{
    if (update_scheduled) return false;
    attachInterruptVector(IRQ_SOFTWARE, software_isr);
    NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208); // 255 = lowest priority
    NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
    update_scheduled = true;
    return true;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?