没有音频输出Teensy 3.2使用板载DAC或USB直通

时间:2017-09-29 09:33:23

标签: audio arduino teensy

我正在为我的音乐学位做最后的项目(想法是探索与电子乐器交互的替代输入方法),并且我正在使用Teensy 3.2和Adafruit STMPE610。我最初计划使用基于硬件的设置,但在与不同的电路进行战斗后,我决定使用我更熟悉的软件。

我遇到的问题是,我似乎无法通过板载DAC或通过计算机传输的数字USB接口播放任何音频。我可以看到该程序正在进入音频播放的位置但似乎没有什么可以工作。

添加dac1.begin()后,程序挂起并仅打印出初始项目标题。我不认为该功能是必需的,但在查看了库的源代码之后,我找到了它,所以我认为我可以试一试。

我不确定我是否在设置功能中遗漏了某些内容,或者我是否必须在循环功能中输出音频。

这里是没有dac1.begin()的串口显示器输出的副本,我随机按下触摸屏(源代码在下面):

=======================
Touch Synth
<name> - 2017
Music Major Project
=======================
Note on 
Touch location (x,y,z) -> 1884 2371 49 
Touch location (x,y,z) -> 1896 2365 37 
Touch location (x,y,z) -> 1892 2365 34  
Touch location (x,y,z) -> 1428 2152 37 
Note off 
Note on 
Touch location (x,y,z) -> 1985 2440 55 
Touch location (x,y,z) -> 1987 2420 39 
Touch location (x,y,z) -> 2010 2381 36 
Touch location (x,y,z) -> 2023 2327 34 
Touch location (x,y,z) -> 2277 2552 34 
Touch location (x,y,z) -> 2074 2648 40 
Note off 
Note on 
Touch location (x,y,z) -> 2006 2575 35 
Touch location (x,y,z) -> 2024 2568 35 
Note off 
Note on 
Touch location (x,y,z) -> 1992 2522 34 
Touch location (x,y,z) -> 2005 2525 31 
Touch location (x,y,z) -> 2004 2523 31 
Touch location (x,y,z) -> 2007 2514 33 
Note off 
Note on 
Touch location (x,y,z) -> 2047 2409 36 
Touch location (x,y,z) -> 2036 2410 32 
Touch location (x,y,z) -> 2039 2405 31 
Touch location (x,y,z) -> 2066 2399 37 
Note off 
Note on 
Touch location (x,y,z) -> 2058 2367 36 
Touch location (x,y,z) -> 2062 2372 34 
Note off 
Note on 
Touch location (x,y,z) -> 2013 2398 34 
Touch location (x,y,z) -> 2029 2399 32 
Note off 

代码:

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>
#include "Adafruit_STMPE610.h"

// ===== Audio GUI Tool code =====
// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=97,100
AudioSynthWaveform       waveform2;      //xy=97,137
AudioMixer4              mixer1;         //xy=349,119
AudioEffectEnvelope      envelope1;      //xy=514,104
AudioMixer4              mixer2;         //xy=676,123
AudioOutputAnalog        dac1;           //xy=841,110
AudioConnection          patchCord1(waveform1, 0, mixer1, 0);
AudioConnection          patchCord2(waveform2, 0, mixer1, 1);
AudioConnection          patchCord3(mixer1, envelope1);
AudioConnection          patchCord4(envelope1, 0, mixer2, 0);
AudioConnection          patchCord5(mixer2, 0, dac1, 0);
// GUItool: end automatically generated code

// ===== Pinout setup =====
const byte PWR_LED = 13;
const byte GATE_OUT = 6;
const byte SPI_SCK = 14;
const byte SPI_MOSI = 11;
const byte SPI_MISO = 12;
const byte STMPE_CS = 15; // Touch Screen Controller Chip Select
const byte aPot = 16;
const byte rPot = 17;

// ===== Touch Screen Object Declaration =====
// SDI to MOSI, SDO to MISO, and SCL to SPI CLOCK
// Tie MODE to 3.3V and POWER CYCLE the STMPE610 (there is no reset pin)
Adafruit_STMPE610 touch = Adafruit_STMPE610(STMPE_CS, SPI_MOSI, SPI_MISO, SPI_SCK);

// ===== Function Declarations =====
void error(int);

// ===== Global variables =====
int gTouchOn = 0;
int gNoteOn = 0;

void setup() {
  Serial.begin(9600);                         // Setup serial
  Serial.println("=======================");  // ============================
  Serial.println("Touch Synth");              // 
  Serial.println("Callum Blackmore - 2017");  // Print faux splash screen to
  Serial.println("Music Major Project");      // serial output
  Serial.println("=======================");  //
  //Serial.flush();                             // ============================

  pinMode(PWR_LED, OUTPUT); //Setup power LED
  digitalWrite(PWR_LED, HIGH);

  if (! touch.begin()) {
    Serial.println("STMPE not found!");
    error(1000);
  }


  AudioMemory(50);
  //dac1.begin(); // <--- Code will hang here if uncommented
  dac1.analogReference(INTERNAL);
  waveform1.begin(WAVEFORM_SINE);
  waveform2.begin(WAVEFORM_TRIANGLE);
  mixer1.gain(0, 0.5);
  mixer1.gain(1, 0.5);
  mixer2.gain(0, 0.75);
  envelope1.delay(0);
  envelope1.attack(100);
  envelope1.hold(0);
  envelope1.decay(15);
  envelope1.sustain(0.7);
  envelope1.release(200);
}

void loop() {
  // put your main code here, to run repeatedly:
  uint16_t analogVal; // temp value for analog reads
  uint16_t x, y;  // Touch coordinates
  uint8_t z;      // ''

  analogVal = analogRead(aPot);      // =====
  //Serial.print(analogVal); Serial.print(' ');
  envelope1.attack(analogVal/10);   // Read pots and update attack and release values
  analogVal = analogRead(rPot);      //
  //Serial.print(analogVal); Serial.println(' ');
  envelope1.release(analogVal/10);  // =====

  if(touch.touched())
  {
    gTouchOn = 1; 
    while (! touch.bufferEmpty())
    {
      touch.readData(&x, &y, &z);
      waveform1.frequency((y*2)+100);
      waveform2.frequency((y*2)+100);
      Serial.print("Touch location (x,y,z) -> ");
      Serial.print(x); Serial.print(' ');
      Serial.print(y); Serial.print(' ');
      Serial.print(z); Serial.println(' ');
    }
    touch.writeRegister8(STMPE_INT_STA, 0xFF); // reset all ints
    if(gNoteOn == 0)
    {
      Serial.print("Note on"); Serial.println(' ');
      gNoteOn = 1;
      envelope1.noteOn();
    }
  }
  else if(!touch.touched() && gNoteOn == 1)
  {
    Serial.print("Note off"); Serial.println(' ');
    envelope1.noteOff();
    gNoteOn = 0;
    gTouchOn = 0;
  }
}

void error(int len){
  while(1) {
      digitalWrite(PWR_LED, HIGH);
      delay(len);
      digitalWrite(PWR_LED, LOW);
      delay(len);
    }
}

1 个答案:

答案 0 :(得分:0)

事实证明,这种感觉很糟糕。我把它换成了另一个单位而且工作得很好。

我需要修改几行代码,但除了更改waveform.begin()语句以包含开始幅度和开始频率之外,它们纯粹是语义的,如下所示:

waveform1.begin(1.0, 440, WAVEFORM_SINE);
waveform2.begin(1.0, 440, WAVEFORM_TRIANGLE);

我还没有使用数字音频,因此我使用板载DAC将音频输出到我所拥有的小型调音台,但大部分功能都在工作。