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