Mozzi  version 2015-05-11-20:23
sound synthesis library for Arduino
 All Classes Functions Typedefs Groups
SampleHuffman.h
1 /*
2  * SampleHuffman.h
3  *
4  * Copyright 2013 Tim Barrass.
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 #ifndef SAMPLEHUFFMAN_H
12 #define SAMPLEHUFFMAN_H
13 
47 #include <util/atomic.h>
48 
50 {
51 
52 public:
53 
59  SampleHuffman(uint8_t const * SOUNDDATA, int16_t const * HUFFMAN_DATA, uint32_t const SOUNDDATA_BITS):sounddata(SOUNDDATA),huffman(HUFFMAN_DATA),sounddata_bits(SOUNDDATA_BITS)
60  {
61  setLoopingOff();
62  }
63 
64 
69  inline
70  int16_t next()
71  {
72  if(datapos >= sounddata_bits){
73  if(looping){
74  // at end of sample, restart from zero, looping the sound
75  datapos = 0;
76  }else{
77  return 0;
78  }
79  }
80 
81  int16_t dif = decode();
82  current += dif; // add differential
83  return current;
84  }
85 
86 
89  inline
90  void setLoopingOn()
91  {
92  looping=true;
93  }
94 
95 
98  inline
100  {
101  looping=false;
102  }
103 
106  inline
107  void start()
108  {
109  // atomic because start() can be called on a sample in the control interrupt
110  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
111  {
112  current = 0;
113  datapos = 0;
114  bt = 0;
115  }
116  }
117 
118 private:
119  uint8_t const * sounddata;
120  int16_t const * huffman;
121  uint32_t const sounddata_bits;
122  uint32_t datapos; // current sample position
123  int16_t current; // current amplitude value
124  bool looping;
125  uint8_t bt;
126 
127  // Get one bit from sound data
128  inline
129  bool getbit()
130  {
131  const uint8_t b = datapos&7;
132  //static uint8_t bt;
133  if(!b) bt = pgm_read_byte(sounddata+((uint32_t)datapos>>3));
134  // extract the indexed bit
135  return ((uint8_t)bt>>(7-b))&1;
136  }
137 
138 
139  // Decode bit stream using Huffman codes
140  inline
141  int16_t decode()
142  {
143  int16_t const * huffcode = huffman;
144  do {
145  if(getbit()) {
146  const int16_t offs = pgm_read_word(huffcode);
147  huffcode += offs?offs+1:2;
148  }
149  datapos++;
150  }
151  while(pgm_read_word(huffcode++));
152  return pgm_read_word(huffcode);
153  }
154 
155 
156 };
157 
163 #endif // #ifndef SAMPLEHUFFMAN_H
void setLoopingOn()
Turns looping on, with the whole sample length as the loop range.
Definition: SampleHuffman.h:90
SampleHuffman(uint8_t const *SOUNDDATA, int16_t const *HUFFMAN_DATA, uint32_t const SOUNDDATA_BITS)
Constructor.
Definition: SampleHuffman.h:59
void start()
Sets the playhead to the beginning of the sample.
void setLoopingOff()
Turns looping off.
Definition: SampleHuffman.h:99
A sample player for samples encoded with Huffman compression.
Definition: SampleHuffman.h:49
int16_t next()
Update and return the next audio sample.
Definition: SampleHuffman.h:70