Source file: /~heha/basteln/Haus/Telefon/Sprachausgabe.zip/AudioOutputPWM.cpp

/*
  AudioOutputPWM
  Base class for I2S interface port
  
  Copyright (C) 2017  Earle F. Philhower, III
*/

#include "i2s.h"
#include "AudioOutputPWM.h"

AudioOutputPWM::AudioOutputPWM()
{
  i2s_begin();
  mono = false;
  bps = 16;
  channels = 2;
  SetGain(1.0);
  SetRate(44100); // Default
}

AudioOutputPWM::~AudioOutputPWM()
{
  i2s_end();
}

bool AudioOutputPWM::SetRate(int hz)
{
  // TODO - have a list of allowable rates from constructor, check them
  hertz = hz;
  i2s_set_rate(AdjustI2SRate(hz));
  return true;
}

bool AudioOutputPWM::SetBitsPerSample(int bits)
{
  if ( (bits != 16) && (bits != 8) ) return false;
  bps = bits;
  return true;
}

bool AudioOutputPWM::SetChannels(int channels)
{
  if ( (channels < 1) || (channels > 2) ) return false;
  this->channels = channels;
  return true;
}

bool AudioOutputPWM::ConsumeSample(int16_t sample[2])
{
  int16_t ms[2];

  ms[0] = sample[0];
  ms[1] = sample[1];
  MakeSampleStereo16( ms );

  if (mono) {
    // Average the two samples and overwrite
    int32_t ttl = ms[LEFTCHANNEL] + ms[RIGHTCHANNEL];
    ms[LEFTCHANNEL] = ms[RIGHTCHANNEL] = (ttl>>1) & 0xffff;
  }
  uint32_t s32 = ((uint32_t)(Amplify(ms[RIGHTCHANNEL]))<<16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff);
  return i2s_write_sample_nb(s32); // If we can't store it, return false.  OTW true
}

Detected encoding: UTF-80