source: rtos_arduino/trunk/arduino_lib/libraries/Audio/src/Audio.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 2.1 KB
Line 
1/*
2 * Copyright (c) 2012 by Cristian Maglie <c.maglie@arduino.cc>
3 * Audio library for Arduino Due.
4 *
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of either the GNU General Public License version 2
7 * or the GNU Lesser General Public License version 2.1, both as
8 * published by the Free Software Foundation.
9 */
10
11#include "Audio.h"
12
13void AudioClass::begin(uint32_t sampleRate, uint32_t msPreBuffer) {
14 // Allocate a buffer to keep msPreBuffer milliseconds of audio
15 bufferSize = msPreBuffer * sampleRate / 1000;
16 if (bufferSize < 1024)
17 bufferSize = 1024;
18 buffer = (uint32_t *) malloc(bufferSize * sizeof(uint32_t));
19 half = buffer + bufferSize / 2;
20 last = buffer + bufferSize;
21
22 // Buffering starts from the beginning
23 running = buffer;
24 next = buffer;
25
26 // Start DAC
27 dac->begin(VARIANT_MCK / sampleRate);
28 dac->setOnTransmitEnd_CB(onTransmitEnd, this);
29}
30
31void AudioClass::end() {
32 dac->end();
33 free( buffer);
34}
35
36void AudioClass::prepare(int16_t *buffer, int S, int volume){
37 uint16_t *ubuffer = (uint16_t*) buffer;
38 for (int i=0; i<S; i++) {
39 // set volume amplitude (signed multiply)
40 buffer[i] = buffer[i] * volume / 1024;
41 // convert from signed 16 bit to unsigned 12 bit for DAC.
42 ubuffer[i] += 0x8000;
43 ubuffer[i] >>= 4;
44 }
45}
46
47size_t AudioClass::write(const uint32_t *data, size_t size) {
48 const uint32_t TAG = 0x10000000;
49 int i;
50 for (i = 0; i < size; i++) {
51 *next = data[i] | TAG;
52 next++;
53
54 if (next == half || next == last) {
55 enqueue();
56 while (next == running)
57 ;
58 }
59 }
60
61 return i;
62}
63
64void AudioClass::enqueue() {
65 if (!dac->canQueue()) {
66 // DMA queue full
67 return;
68 }
69
70 if (next == half) {
71 // Enqueue the first half
72 dac->queueBuffer(buffer, bufferSize / 2);
73 } else {
74 // Enqueue the second half
75 dac->queueBuffer(half, bufferSize / 2);
76 next = buffer; // wrap around
77 }
78}
79
80void AudioClass::onTransmitEnd(void *_me) {
81 AudioClass *me = reinterpret_cast<AudioClass *> (_me);
82 if (me->running == me->buffer)
83 me->running = me->half;
84 else
85 me->running = me->buffer;
86}
87
88AudioClass Audio(DAC);
Note: See TracBrowser for help on using the repository browser.