-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArdBtSource.cpp
97 lines (76 loc) · 1.91 KB
/
ArdBtSource.cpp
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
#include "ArdBtSource.h"
#ifdef ARDBTSOURCE_H
namespace stk {
char * APP_ArdBtSource = "ArdBtSource";
// Used to make the voicer available to the callback
Voicer *pArdBtSourceVoicer;
//! Default Constructor
ArdBtSource :: ArdBtSource( ) {
a2dp_source = new BluetoothA2DPSource();
}
//! Destructor
ArdBtSource :: ~ArdBtSource()
{
if (a2dp_source!=NULL)
delete a2dp_source;
}
// logic copied from WvOut;
StkFloat& clipTest( StkFloat& sample )
{
if ( sample > 1.0 ) {
sample = 1.0;
}
else if ( sample < -1.0 ) {
sample = -1.0;
}
return sample;
}
bool hasSoundData;
long logTime;
//! Callback: we just get the ticks from the pVoicer converted to Int16
int32_t get_stk_data(uint8_t *data, int32_t len) {
if (len < 0 || data == NULL) {
return 0;
}
int dataBytes = sizeof(SINT16);
int samples = len / dataBytes / 2;
SINT16 *ptr = (SINT16 *) data;
StkFloat sample;
for ( unsigned long k=0; k<samples; k++ ) {
sample = pArdBtSourceVoicer->tick() ;
clipTest( sample );
// Channel 1
*ptr = (SINT16) (sample * 32767.0);
#ifdef __LITTLE_ENDIAN__
swap16 ((unsigned char *)ptr);
#endif
ptr++;
// Channel 2
*ptr = *(ptr-1);
if (*ptr!=0){
hasSoundData = true;
}
ptr++;
}
if (logTime < millis()){
logTime = millis()+5000;
hasSoundData = false;
}
return len;
}
void ArdBtSource :: start(char* name, Voicer &voicer) {
this->pVoicer = &voicer;
pArdBtSourceVoicer = &voicer;
a2dp_source->start_raw(name, get_stk_data);
}
bool ArdBtSource :: isConnected(){
return a2dp_source->is_connected();
}
void ArdBtSource :: setNVSInit(bool doInit){
a2dp_source->set_nvs_init(doInit);
}
void ArdBtSource :: setResetBLE(bool doInit){
a2dp_source->set_reset_ble(doInit);
}
} // stk namespace
#endif