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 
41 // support for legacy name;
42 using Channels = Frame;
43 
51  Both,
52  Left,
53  Right
54 };
55 
72 class SoundData {
73  public:
74  virtual int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data) = 0;
75  virtual int32_t getData(int32_t pos, Frame &channels) = 0;
76  virtual void setDataRaw( uint8_t* data, int32_t len) = 0;
80  bool doLoop();
81  void setLoop(bool loop);
82 
83  private:
84  bool automatic_loop;
85 };
86 
87 
95 public:
96  TwoChannelSoundData(bool loop=false);
97  TwoChannelSoundData(Frame *data, int32_t len, bool loop=false);
98  void setData( Frame *data, int32_t len);
99  void setDataRaw( uint8_t* data, int32_t len);
100  int32_t getData(int32_t pos, int32_t len, Frame *data);
101  int32_t getData(int32_t pos, Frame &channels);
102  int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data);
103  // the number of frames
104  int32_t count(){
105  return len;
106  }
107 private:
108  Frame* data;
109  int32_t len; // length of all data in base unit of subclass
110 };
111 
112 
119  public:
120  OneChannelSoundData(bool loop=false, ChannelInfo channelInfo=Both);
121  OneChannelSoundData(int16_t *data, int32_t len, bool loop=false, ChannelInfo channelInfo=Both);
122  void setData( int16_t *data, int32_t len);
123  void setDataRaw( uint8_t* data, int32_t len);
124  int32_t getData(int32_t pos, int32_t len, int16_t *data);
125  int32_t getData(int32_t pos, Frame &frame);
126  int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data);
127  private:
128  int16_t* data;
129  int32_t len;
130  ChannelInfo channelInfo;
131 };
132 
133 
140  public:
141  OneChannel8BitSoundData(bool loop=false, ChannelInfo channelInfo=Both);
142  OneChannel8BitSoundData(int8_t *data, int32_t len, bool loop=false, ChannelInfo channelInfo=Both);
143  void setData( int8_t *data, int32_t len);
144  void setDataRaw( uint8_t* data, int32_t len);
145  int32_t getData(int32_t pos, int32_t len, int8_t *data);
146  int32_t getData(int32_t pos, Frame &frame);
147  int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data);
148  private:
149  int8_t* data;
150  int32_t len;
151  ChannelInfo channelInfo;
152 };
1 Channel data is provided as signed int8 values.
Definition: SoundData.h:139
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:118
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:72
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:94
int32_t get2ChannelData(int32_t pos, int32_t len, uint8_t *data)
Definition: SoundData.cpp:78
ChannelInfo
Channel Information.
Definition: SoundData.h:50
@ Left
Definition: SoundData.h:52
@ Right
Definition: SoundData.h:53
@ Both
Definition: SoundData.h:51