Implementation of Bytebeat algorithm on Pokitto. Bytebeat is generated music which is made by algorithm using usually one line function. The music is created as 8-bit samples. The simplest Bytebeat function is " t & (t>>8)" which combines two sawtooth waves: one higher frequency tone (t) and the other lower frequency (t<<8) tone. Read more in: http://countercomplex.blogspot.fi/2011/10/algorithmic-symphonies-from-one-line-of.html
Dependencies: PokittoLib
ByteBeat3 - mathematical beats
Implementation of Bytebeat algorithm on Pokitto. Bytebeat is generated music which is made by algorithm using usually one line function. The music is created as 8-bit samples.
The simplest Bytebeat function is " t & (t>>8)" which combines two sawtooth waves: one higher frequency tone (t) and the other lower frequency (t<<8) tone.
Read more in: http://countercomplex.blogspot.fi/2011/10/algorithmic-symphonies-from-one-line-of.html
main.cpp@0:89ca7e98cd78, 2018-03-30 (annotated)
- Committer:
- Haviital
- Date:
- Fri Mar 30 20:09:40 2018 +0000
- Revision:
- 0:89ca7e98cd78
- Child:
- 1:ca29888d96a9
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Haviital | 0:89ca7e98cd78 | 1 | #include "Pokitto.h" |
Haviital | 0:89ca7e98cd78 | 2 | |
Haviital | 0:89ca7e98cd78 | 3 | Pokitto::Core mygame; |
Haviital | 0:89ca7e98cd78 | 4 | |
Haviital | 0:89ca7e98cd78 | 5 | void generateBuffer(uint32_t bufferIndex, uint32_t nextT) { |
Haviital | 0:89ca7e98cd78 | 6 | |
Haviital | 0:89ca7e98cd78 | 7 | uint32_t index= 0; |
Haviital | 0:89ca7e98cd78 | 8 | for(uint32_t t=nextT; t<nextT+BUFFER_SIZE; t++,index++) |
Haviital | 0:89ca7e98cd78 | 9 | //buffers[nextBufferIndexToFill][index] = t & (t>>8); |
Haviital | 0:89ca7e98cd78 | 10 | buffers[bufferIndex][index] = ((t<<1)^((t<<1)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7; |
Haviital | 0:89ca7e98cd78 | 11 | //buffers[bufferIndex][index] = (t*9&t>>4|t*5&t>>7|t*3&t/1024)-1; |
Haviital | 0:89ca7e98cd78 | 12 | } |
Haviital | 0:89ca7e98cd78 | 13 | |
Haviital | 0:89ca7e98cd78 | 14 | |
Haviital | 0:89ca7e98cd78 | 15 | int main () { |
Haviital | 0:89ca7e98cd78 | 16 | mygame.begin(); |
Haviital | 0:89ca7e98cd78 | 17 | mygame.display.persistence = true; |
Haviital | 0:89ca7e98cd78 | 18 | mygame.display.paletteptr[2] = COLOR_YELLOW; |
Haviital | 0:89ca7e98cd78 | 19 | |
Haviital | 0:89ca7e98cd78 | 20 | // Generate 4 buffers ahead |
Haviital | 0:89ca7e98cd78 | 21 | uint32_t nextT = 0; |
Haviital | 0:89ca7e98cd78 | 22 | generateBuffer(0, nextT); nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 23 | generateBuffer(1, nextT); nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 24 | generateBuffer(2, nextT); nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 25 | generateBuffer(3, nextT); nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 26 | |
Haviital | 0:89ca7e98cd78 | 27 | // Set global variables |
Haviital | 0:89ca7e98cd78 | 28 | currentBuffer = 0; |
Haviital | 0:89ca7e98cd78 | 29 | currentPtr = buffers[currentBuffer]; |
Haviital | 0:89ca7e98cd78 | 30 | endPtr = currentPtr + BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 31 | |
Haviital | 0:89ca7e98cd78 | 32 | pokPlayStream(); // activate stream |
Haviital | 0:89ca7e98cd78 | 33 | mygame.sound.ampEnable(true); |
Haviital | 0:89ca7e98cd78 | 34 | mygame.sound.playMusicStream(); |
Haviital | 0:89ca7e98cd78 | 35 | |
Haviital | 0:89ca7e98cd78 | 36 | mygame.display.set_cursor(0,0); |
Haviital | 0:89ca7e98cd78 | 37 | mygame.display.println("ByteBeat function:"); |
Haviital | 0:89ca7e98cd78 | 38 | mygame.display.println(""); |
Haviital | 0:89ca7e98cd78 | 39 | mygame.display.color = 2; |
Haviital | 0:89ca7e98cd78 | 40 | mygame.display.println("((t<<1)^((t<<1)+(t>>7)"); |
Haviital | 0:89ca7e98cd78 | 41 | mygame.display.println("&t>>12))|t>>(4-(1^7&"); |
Haviital | 0:89ca7e98cd78 | 42 | mygame.display.println("(t>>19)))|t>>7"); |
Haviital | 0:89ca7e98cd78 | 43 | |
Haviital | 0:89ca7e98cd78 | 44 | uint32_t nextBufferIndexToFill = 0; |
Haviital | 0:89ca7e98cd78 | 45 | while (mygame.isRunning()) { |
Haviital | 0:89ca7e98cd78 | 46 | if (mygame.update()) { |
Haviital | 0:89ca7e98cd78 | 47 | |
Haviital | 0:89ca7e98cd78 | 48 | if((uint32_t)currentBuffer != nextBufferIndexToFill) { |
Haviital | 0:89ca7e98cd78 | 49 | |
Haviital | 0:89ca7e98cd78 | 50 | // Generate buffer if already played. |
Haviital | 0:89ca7e98cd78 | 51 | generateBuffer(nextBufferIndexToFill, nextT); |
Haviital | 0:89ca7e98cd78 | 52 | |
Haviital | 0:89ca7e98cd78 | 53 | if(++nextBufferIndexToFill > 3) |
Haviital | 0:89ca7e98cd78 | 54 | nextBufferIndexToFill = 0; |
Haviital | 0:89ca7e98cd78 | 55 | nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 56 | } |
Haviital | 0:89ca7e98cd78 | 57 | } |
Haviital | 0:89ca7e98cd78 | 58 | } |
Haviital | 0:89ca7e98cd78 | 59 | } |