Arduino A2DP
SoundData.h
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 //
13 // Copyright 2020 Phil Schatzmann
14 
15 #pragma once
16 
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <algorithm> // std::min
20 
26 struct __attribute__((packed)) Frame {
27  int16_t channel1;
28  int16_t channel2;
29 
30  Frame(int v=0){
31  channel1 = channel2 = v;
32  }
33 
34  Frame(int ch1, int ch2){
35  channel1 = ch1;
36  channel2 = ch2;
37  }
38 
39 };
40 
46 enum ChannelInfo {
47  Both,
48  Left,
49  Right
50 };
51 
67 class SoundData {
68  public:
69  virtual int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data);
70  virtual int32_t getData(int32_t pos, Frame &channels);
71  virtual void setDataRaw( uint8_t* data, int32_t len);
75  bool doLoop();
76  void setLoop(bool loop);
77 
78  private:
79  bool automatic_loop;
80 };
81 
82 
90 public:
91  TwoChannelSoundData(bool loop=false);
92  TwoChannelSoundData(Frame *data, int32_t len, bool loop=false);
93  void setData( Frame *data, int32_t len);
94  void setDataRaw( uint8_t* data, int32_t len);
95  int32_t getData(int32_t pos, int32_t len, Frame *data);
96  int32_t getData(int32_t pos, Frame &channels);
97  int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data);
98  // the number of frames
99  int32_t count(){
100  return len;
101  }
102 private:
103  Frame* data;
104  int32_t len; // length of all data in base unit of subclass
105 };
106 
107 
114  public:
115  OneChannelSoundData(bool loop=false, ChannelInfo channelInfo=Both);
116  OneChannelSoundData(int16_t *data, int32_t len, bool loop=false, ChannelInfo channelInfo=Both);
117  void setData( int16_t *data, int32_t len);
118  void setDataRaw( uint8_t* data, int32_t len);
119  int32_t getData(int32_t pos, int32_t len, int16_t *data);
120  int32_t getData(int32_t pos, Frame &frame);
121  int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data);
122  private:
123  int16_t* data;
124  int32_t len;
125  ChannelInfo channelInfo;
126 };
127 
128 
135  public:
136  OneChannel8BitSoundData(bool loop=false, ChannelInfo channelInfo=Both);
137  OneChannel8BitSoundData(int8_t *data, int32_t len, bool loop=false, ChannelInfo channelInfo=Both);
138  void setData( int8_t *data, int32_t len);
139  void setDataRaw( uint8_t* data, int32_t len);
140  int32_t getData(int32_t pos, int32_t len, int8_t *data);
141  int32_t getData(int32_t pos, Frame &frame);
142  int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data);
143  private:
144  int8_t* data;
145  int32_t len;
146  ChannelInfo channelInfo;
147 };
1 Channel data is provided as signed int8 values.
Definition: SoundData.h:134
int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data)
Definition: SoundData.cpp:206
1 Channel data is provided as int16 values
Definition: SoundData.h:113
int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data)
Definition: SoundData.cpp:124
Sound data as byte stream. We support TwoChannelSoundData (uint16_t + uint16_t) and OneChannelSoundDa...
Definition: SoundData.h:67
bool doLoop()
Definition: SoundData.cpp:21
Data is provided in two channels of int16 data: so len is in 4 byte entries (int16 + int16)
Definition: SoundData.h:89
int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data)
Definition: SoundData.cpp:78