Arduino  1.8.10
Adafruit_CPlay_Speaker クラス

Class that stores state and functions for the speaker on CircuitPlayground boards [詳解]

#include <Adafruit_CPlay_Speaker.h>

公開メンバ関数

 Adafruit_CPlay_Speaker (void)
 
void begin (void)
 Sets up Circuit Playground speaker for PWM audio output: enables 48 KHz high-speed PWM mode, configures Timer/Counter 4, sets PWM duty cycle to 50% (speaker idle position). [詳解]
 
void end (void)
 Turns off PWM output to the speaker. [詳解]
 
void set (uint8_t value)
 Sets speaker position, enables PWM output if needed. [詳解]
 
void playSound (const uint8_t *data, uint32_t length, uint16_t sampleRate, bool tenBit=false)
 Plays digitized 8-bit audio (optionally 10 bits on Express board) from a PROGMEM (flash memory) buffer. Maybe 1-3 seconds tops depending on sampling rate (e.g. 8000 Hz = 8 Kbytes/second). Max ~20K space avail on Circuit Playground, lots more on Circuit Playground Express. This function currently "blocks" – it will not play sounds in the background while other code runs. [詳解]
 
void say (const uint8_t *addr)
 speak the data at the passed location [詳解]
 
void enable (bool e)
 enable or disable the speaker. This function only works on 'Express' boards. [詳解]
 
void off (void)
 disable the speaker. [詳解]
 
void on (void)
 enable the speaker. [詳解]
 

詳解

Class that stores state and functions for the speaker on CircuitPlayground boards

Adafruit_CPlay_Speaker.h18 行目に定義があります。

構築子と解体子

◆ Adafruit_CPlay_Speaker()

Adafruit_CPlay_Speaker::Adafruit_CPlay_Speaker ( void  )
inline

Adafruit_CPlay_Speaker.h20 行目に定義があります。

20 { started = false; };

関数詳解

◆ begin()

void Adafruit_CPlay_Speaker::begin ( void  )

Sets up Circuit Playground speaker for PWM audio output: enables 48 KHz high-speed PWM mode, configures Timer/Counter 4, sets PWM duty cycle to 50% (speaker idle position).

Adafruit_CPlay_Speaker.cpp14 行目に定義があります。

14  {
15  if(!started) {
16 #ifdef __AVR__
17  // Set up Timer4 for fast PWM on !OC4A
18  PLLFRQ = (PLLFRQ & 0xCF) | 0x30; // Route PLL to async clk
19  TCCR4A = _BV(COM4A0) | _BV(PWM4A); // Clear on match, PWMA on
20  TCCR4B = _BV(PWM4X) | _BV(CS40); // PWM invert, 1:1 prescale
21  TCCR4D = 0; // Fast PWM mode
22  TCCR4E = 0; // Not enhanced mode
23  DT4 = 0; // No dead time
24  noInterrupts(); // TC4H accesses must be atomic
25  TC4H = 0; // Not 10-bit mode
26  OCR4C = 255; // TOP
27  TC4H = 0;
28  OCR4A = 127; // 50% duty (idle position) to start
29  interrupts();
30  pinMode(5, OUTPUT); // Enable output
31 #else
34  // PWM/timer not needed on CPlay Express, has true analog out.
35  // Set analogWrite resolution to 8 bits to match AVR calls.
36  analogWriteResolution(8);
37  pinMode(A0, OUTPUT); // Enable output
38 #endif
39  started = true;
40  }
41 }
#define interrupts()
Definition: Arduino.h:101
#define PWM4A
Definition: iom16u4.h:941
void pinMode(uint8_t, uint8_t)
#define DT4
Definition: iom16u4.h:1102
#define COM4A0
Definition: iom1284rfr2.h:975
#define TCCR4E
Definition: iom16u4.h:979
#define noInterrupts()
Definition: Arduino.h:102
void digitalWrite(uint8_t, uint8_t)
#define OCR4A
Definition: iom1284rfr2.h:1007
#define OCR4C
Definition: iom1284rfr2.h:1019
#define PWM4X
Definition: iom16u4.h:957
#define TCCR4B
Definition: iom1284rfr2.h:978
#define HIGH
Definition: Arduino.h:40
#define CPLAY_SPEAKER_SHUTDOWN
shutdown pin (Express boards only)
#define TCCR4A
Definition: iom1284rfr2.h:968
#define CS40
Definition: iom1284rfr2.h:979
#define _BV(bit)
Definition: sfr_defs.h:208
#define TC4H
Definition: iom16u4.h:934
#define OUTPUT
Definition: Arduino.h:44
#define PLLFRQ
Definition: iom16u4.h:378
#define TCCR4D
Definition: iom16u4.h:969

◆ enable()

void Adafruit_CPlay_Speaker::enable ( bool  e)

enable or disable the speaker. This function only works on 'Express' boards.

引数
epass true to enable, false to disable

Adafruit_CPlay_Speaker.cpp50 行目に定義があります。

50  {
51 #if !defined(__AVR__) // circuit playground express has nicer amp w/shutdown
53 #endif
54 }
void digitalWrite(uint8_t, uint8_t)
#define CPLAY_SPEAKER_SHUTDOWN
shutdown pin (Express boards only)

◆ end()

void Adafruit_CPlay_Speaker::end ( void  )

Turns off PWM output to the speaker.

Adafruit_CPlay_Speaker.cpp61 行目に定義があります。

61  {
62  if(started) {
63 #ifdef __AVR__
64  TCCR4A = 0; // PWMA off
65  pinMode(5, INPUT);
66 #else
67  pinMode(A0, INPUT);
68  enable(false);
69 #endif
70  started = false;
71  }
72 }
void pinMode(uint8_t, uint8_t)
void enable(bool e)
enable or disable the speaker. This function only works on 'Express' boards.
#define TCCR4A
Definition: iom1284rfr2.h:968
#define INPUT
Definition: Arduino.h:43

◆ off()

void Adafruit_CPlay_Speaker::off ( void  )
inline

disable the speaker.

覚え書き
this function only has an effect on 'Express' boards

Adafruit_CPlay_Speaker.h36 行目に定義があります。

36 { enable(false); };
void enable(bool e)
enable or disable the speaker. This function only works on 'Express' boards.

◆ on()

void Adafruit_CPlay_Speaker::on ( void  )
inline

enable the speaker.

覚え書き
this function only has an effect on 'Express' boards

Adafruit_CPlay_Speaker.h44 行目に定義があります。

44 { enable(true); };
void enable(bool e)
enable or disable the speaker. This function only works on 'Express' boards.

◆ playSound()

void Adafruit_CPlay_Speaker::playSound ( const uint8_t *  data,
uint32_t  len,
uint16_t  sampleRate,
bool  tenBit = false 
)

Plays digitized 8-bit audio (optionally 10 bits on Express board) from a PROGMEM (flash memory) buffer. Maybe 1-3 seconds tops depending on sampling rate (e.g. 8000 Hz = 8 Kbytes/second). Max ~20K space avail on Circuit Playground, lots more on Circuit Playground Express. This function currently "blocks" – it will not play sounds in the background while other code runs.

引数
datapointer to the audio data to play
lenthe length of the data in samples
sampleRatethe sample rate of the data in samples per second
tenBitOptional flag if true 10-bit mode is enabled. AVR ONLY

Adafruit_CPlay_Speaker.cpp103 行目に定義があります。

104  {
105 
106  uint32_t i;
107 
108  if(!started) begin();
109 
110 #ifdef __AVR__
111  uint16_t interval = 1000000L / sampleRate;
112 #else
113  uint32_t r2 = sampleRate / 2;
114  uint32_t startTime = micros();
115 #endif
116 
117  if(tenBit) { // 10-bit audio samples?
118  uint8_t loIdx = 4;
119 #ifdef __AVR__
120  // Because it uses 8-bit PWM for output, the AVR code must filter
121  // 10-bit data down to 8 bits. This is ONLY here for compatibility
122  // with sketches with 10-bit samples. If targeting a project for AVR,
123  // it's best to produce and use optimized 8-bit audio, else it's just
124  // wasted space! Timer/Counter 4 DOES offer a 10-bit mode, but it's
125  // not used in this library, just not worth it in the limited flash
126  // space of the 32U4 chip.
127  uint16_t idx = 0;
128  uint8_t hiBits;
129  for(i=0; i<len; i++) {
130  if(++loIdx >= 4) {
131  hiBits = pgm_read_byte(&data[idx++]);
132  loIdx = 0;
133  }
134  OCR4A = ((hiBits & 0xC0) | (pgm_read_byte(&data[idx++]) >> 2));
135  hiBits <<= 2; // Do this after write, because of masking op above
136  delayMicroseconds(interval);
137  }
138  OCR4A = 127;
139 #else
140  // Circuit Playground Express -- use 10-bit analogWrite()
141  uint32_t idx = 0;
142  uint16_t hiBits;
143  analogWriteResolution(10);
144  for(i=0; i<len; i++) {
145  if(++loIdx >= 4) {
146  hiBits = (uint16_t)pgm_read_byte(&data[idx++]);
147  loIdx = 0;
148  }
149  hiBits <<= 2; // Do this before write, because of masking op below
150  analogWrite(A0, (hiBits & 0x300) | pgm_read_byte(&data[idx++]));
151  while(((micros()-startTime+50)/100) < ((i*10000UL+r2)/sampleRate));
152  }
153  analogWriteResolution(8); // Return to 8 bits for set() compatibility
154  analogWrite(A0, 127);
155 #endif
156 
157  } else { // 8-bit audio samples
158 
159 #ifdef __AVR__
160  for(i=0; i<len; i++) {
161  OCR4A = pgm_read_byte(&data[i]);
162  delayMicroseconds(interval);
163  }
164  OCR4A = 127;
165 #else
166  for(i=0; i<len; i++) {
167  analogWrite(A0, pgm_read_byte(&data[i]));
168  while(((micros()-startTime+50)/100) < ((i*10000UL+r2)/sampleRate));
169  }
170  analogWrite(A0, 127);
171 #endif
172  }
173 }
uint8_t i
#define pgm_read_byte(address_short)
Definition: pgmspace.h:1055
void begin(void)
Sets up Circuit Playground speaker for PWM audio output: enables 48 KHz high-speed PWM mode,...
void analogWrite(uint8_t, int)
#define OCR4A
Definition: iom1284rfr2.h:1007
unsigned int startTime
Definition: ard_tcp.c:32
unsigned long micros(void)
Definition: wiring.c:79
void delayMicroseconds(unsigned int us)
Definition: wiring.c:120

◆ say()

void Adafruit_CPlay_Speaker::say ( const uint8_t *  addr)

speak the data at the passed location

引数
addrpointer to the data

talkie.cpp86 行目に定義があります。

86  {
87 
88  int16_t x0=0, x1=0, x2=0, x3=0, x4=0,
89  x5=0, x6=0, x7=0, x8=0, x9=0,
90  synthK1, synthK2, u0;
91  uint16_t synthEnergy, synthRand = 1;
92  int8_t synthK3, synthK4, synthK5, synthK6,
93  synthK7, synthK8, synthK9, synthK10;
94  uint8_t periodCounter=0, nextPwm = 0x7F, synthPeriod;
95  uint8_t iCount = TICKS;
96  uint32_t nowTime, prevTime=0;
97 
98  if(!started) begin();
99 
100  buf = bufBits = 0; // Reset 'ROM' reader (global stuff)
101  ptrAddr = addr;
102 
103  for(;;) {
104  while(((nowTime = micros()) - prevTime) < USEC);
105 #ifdef __AVR__
106  OCR4A = nextPwm;
107 #else
108  analogWrite(A0, nextPwm);
109 #endif
110  prevTime = nowTime;
111 
112  if(++iCount >= TICKS) {
113  // Read speech data, processing the variable size frames
114  uint8_t energy;
115  if((energy = getBits(4)) == 0) { // Rest frame
116  synthEnergy = 0;
117  } else if(energy == 0xF) { // Stop frame; silence
118 #ifdef __AVR__
119  TCCR4A = 0x7F;
120 #else
121  analogWrite(A0, 0x7F);
122 #endif
123  break;
124  } else {
125  synthEnergy = pgm_read_byte(&tmsEnergy[energy]);
126  uint8_t repeat = getBits(1);
127  synthPeriod = pgm_read_byte(&tmsPeriod[getBits(6)]);
128  if(!repeat) { // A repeat frame uses last coefficients
129  synthK1 = read16(tmsK1, 5); // All frames
130  synthK2 = read16(tmsK2, 5); // use the first
131  synthK3 = read8( tmsK3, 4); // 4 coefficients
132  synthK4 = read8( tmsK4, 4);
133  if(synthPeriod) {
134  synthK5 = read8(tmsK5 , 4); // Voiced
135  synthK6 = read8(tmsK6 , 4); // frames
136  synthK7 = read8(tmsK7 , 4); // use
137  synthK8 = read8(tmsK8 , 3); // six
138  synthK9 = read8(tmsK9 , 3); // extra
139  synthK10 = read8(tmsK10, 3); // coeffs
140  }
141  }
142  }
143  iCount = 0;
144  }
145 
146  if(synthPeriod) { // Voiced source
147  if(++periodCounter >= synthPeriod) periodCounter = 0;
148  u0 = (periodCounter >= sizeof(chirp)) ? 0 :
149  (pgm_read_byte(&chirp[periodCounter]) *
150  (uint32_t)synthEnergy) >> 8;
151  } else { // Unvoiced source
152  synthRand = (synthRand >> 1) ^ ((synthRand & 1) ? 0xB800 : 0);
153  u0 = (synthRand & 1) ? synthEnergy : -synthEnergy;
154  }
155  u0 -= ((synthK10 * x9) +
156  (synthK9 * x8)) >> 7;
157  x9 = x8 + ((synthK9 * u0 ) >> 7);
158  u0 -= ((synthK8 * x7 ) >> 7);
159  x8 = x7 + ((synthK8 * u0 ) >> 7);
160  u0 -= ((synthK7 * x6 ) >> 7);
161  x7 = x6 + ((synthK7 * u0 ) >> 7);
162  u0 -= ((synthK6 * x5 ) >> 7);
163  x6 = x5 + ((synthK6 * u0 ) >> 7);
164  u0 -= ((synthK5 * x4 ) >> 7);
165  x5 = x4 + ((synthK5 * u0 ) >> 7);
166  u0 -= ((synthK4 * x3 ) >> 7);
167  x4 = x3 + ((synthK4 * u0 ) >> 7);
168  u0 -= ((synthK3 * x2 ) >> 7);
169  x3 = x2 + ((synthK3 * u0 ) >> 7);
170  u0 -= ((synthK2 * (int32_t)x1 ) >> 15);
171  x2 = x1 + ((synthK2 * (int32_t)u0 ) >> 15);
172  u0 -= ((synthK1 * (int32_t)x0 ) >> 15);
173  x1 = x0 + ((synthK1 * (int32_t)u0 ) >> 15);
174 
175  if( u0 > 511) u0 = 511; // Output clamp
176  else if(u0 < -512) u0 = -512;
177 
178  x0 = u0;
179  nextPwm = (u0 >> 2) + 0x80;
180  }
181 }
#define pgm_read_byte(address_short)
Definition: pgmspace.h:1055
#define TICKS
Definition: talkie.cpp:8
#define read8(base, bits)
Definition: talkie.cpp:77
#define USEC
Definition: talkie.cpp:9
void begin(void)
Sets up Circuit Playground speaker for PWM audio output: enables 48 KHz high-speed PWM mode,...
void analogWrite(uint8_t, int)
#define OCR4A
Definition: iom1284rfr2.h:1007
#define read16(base, bits)
Definition: talkie.cpp:78
#define TCCR4A
Definition: iom1284rfr2.h:968
unsigned long micros(void)
Definition: wiring.c:79

◆ set()

void Adafruit_CPlay_Speaker::set ( uint8_t  value)

Sets speaker position, enables PWM output if needed.

引数
valuethe value to set (0-255; 127=idle)

Adafruit_CPlay_Speaker.cpp80 行目に定義があります。

80  {
81  if(!started) begin();
82 #ifdef __AVR__
83  OCR4A = value;
84 #else
85  analogWrite(A0, value);
86 #endif
87 }
void begin(void)
Sets up Circuit Playground speaker for PWM audio output: enables 48 KHz high-speed PWM mode,...
void analogWrite(uint8_t, int)
#define OCR4A
Definition: iom1284rfr2.h:1007

このクラス詳解は次のファイルから抽出されました: