-
Notifications
You must be signed in to change notification settings - Fork 12
/
WaveRP.h
executable file
·153 lines (144 loc) · 5.9 KB
/
WaveRP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Arduino WaveRP Library
* Copyright (C) 2009 by William Greiman
*
* This file is part of the Arduino WaveRP Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Arduino WaveRP Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef WaveRP_h
#define WaveRP_h
#include <SdFat.h>
#include <portManipulations.h>
#include <mcpDac.h>
#define PIN C,0
/**
* Use Software volume control if nonzero. Uses multiply and shift to
* decrease volume by 1 dB per step. See DAC ISR in WaveHC.cpp.
* Decreases MAX_CLOCK_RATE to 22050.
*/
#define DVOLUME 1
// Voltage Reference Selections for ADC
/** AREF, Internal Vref turned off */
uint8_t const ADC_REF_AREF = 0;
/** AVCC with external capacitor at AREF pin */
uint8_t const ADC_REF_AVCC = 1 << REFS0;
/** Internal 1.1V Voltage Reference with external capacitor at AREF pin */
uint8_t const ADC_REF_INTERNAL = (1 << REFS0) | (1 << REFS1);
/** Mask to test for valid reference selection */
uint8_t const ADC_REF_MASK = (1 << REFS0) | (1 << REFS1);
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
/** max analog pin number for mega */
uint8_t const ADC_MAX_PIN = 15;
#else // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
/** max analog pin number for 328 Arduino */
uint8_t const ADC_MAX_PIN = 7;
#endif // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Values for rpState
uint8_t const RP_STATE_STOPPED = 0;
uint8_t const RP_STATE_PLAYING = 1;
uint8_t const RP_STATE_RECORDING = 2;
// values for sdStatus
uint8_t const SD_STATUS_BUFFER_BUSY = 0;
uint8_t const SD_STATUS_BUFFER_READY = 1;
uint8_t const SD_STATUS_END_OF_DATA = 2;
uint8_t const SD_STATUS_IO_ERROR = 3;
//------------------------------------------------------------------------------
/**
* \class WaveRP
* \brief Record and play Wave files on SD and SDHC cards.
*/
class WaveRP {
public:
// static variable shared with ISR
/** Minimum sample value since last adcClearRange() call */
volatile static uint8_t adcMin;
/** Maximum sample value since last adcClearRange() call */
volatile static uint8_t adcMax;
/** number of SD busy error in ISR for ADC or DAC for current file */
volatile static uint8_t busyError;
/** Number of bits per sample for file being played. Not set for recorder.*/
volatile static uint8_t bitsPerSample;
/** Number of channels, one or two, for file being played.
* Not used by recorder. */
volatile static uint8_t Channels;
/** recorder or player pause control - has not been tested */
volatile static uint8_t rpPause;
/** state of ADC or DAC ISR */
volatile static uint8_t rpState;
/** Sample rate for file being played. Not set recorder. */
volatile static uint32_t sampleRate;
/** SD card for this file */
static Sd2Card *sdCard;
/** Status of SD ISR routine. */
volatile static uint8_t sdStatus;
/** SdFile for current record or play file */
volatile static SdBaseFile *sdFile;
/** address of first block of the file */
static uint32_t sdStartBlock;
/** address of last block of the file */
static uint32_t sdEndBlock;
/** File position for end of play data or max record position. */
volatile static uint32_t sdEndPosition;
/** current position for ISR raw read/write of SD */
volatile static uint32_t sdCurPosition;
volatile static uint16_t _crush;
volatile static bool audioThru;
#if DVOLUME || defined(__DOXYGEN__)
/** multiplier for volume control */
volatile static uint8_t volMult;
/** offset for volume control */
volatile static uint16_t volOffset;
void setVolume(uint8_t db);
#endif // DVOLUME
//----------------------------------------------------------------------------
/** Clears min and max sample values. */
void adcClearRange(void) {
adcMin = 0XFF;
adcMax = 0;
}
/** \return The range of sample values since the call to adcClearRange. */
uint8_t adcGetRange(void) {return adcMax < adcMin ? 0 : adcMax - adcMin;}
/** \return ADC max value */
uint8_t adcGetMax(void) {return adcMax;}
/** \return ADC min value */
uint8_t adcGetMin(void) {return adcMin;}
/** \return number of SD busy errors in ISRs for the ADC or DAC. */
uint8_t errors(void) {return busyError;}
/** \return The pause status. */
bool isPaused(void) {return rpPause;}
/** \return true if the player is active. */
bool isPlaying(void) {return rpState == RP_STATE_PLAYING;}
/** \return true if the recorder is active. */
bool isRecording(void) {return rpState == RP_STATE_RECORDING;}
/** Pause recorder or player. */
void pause(void) {rpPause = true;}
uint16_t getData();
bool play(SdBaseFile* file); //,uint32_t _pos=0
bool record(SdBaseFile* file, uint16_t rate, uint8_t pin, uint8_t ref);
/** Resume recorder or player. */
void resume(void) {rpPause = false;} //bit_clear(PIN);
void stop(void);
bool trim(SdBaseFile* file);
void adcInit(uint16_t rate, uint8_t pin, uint8_t ref);
//----------------------------------------------------------------------------
// untested functions
void seek(uint32_t pos);
// void rB();
uint32_t getCurPosition(void){ return sdCurPosition; };
void setSampleRate(uint32_t samplerate);
void setCrush(uint16_t _CRUSH){ _crush=_CRUSH<<2;};
void setAudioThru(bool _THRU){ audioThru=_THRU, mcpDacInit();};
};
#endif // WaveRP_h