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@1:ca29888d96a9, 2018-03-30 (annotated)
- Committer:
- Haviital
- Date:
- Fri Mar 30 20:25:48 2018 +0000
- Revision:
- 1:ca29888d96a9
- Parent:
- 0:89ca7e98cd78
Improved comments
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 | 1:ca29888d96a9 | 5 | // Fills an audio buffer with the samples generated by the Bytebeat function |
Haviital | 0:89ca7e98cd78 | 6 | void generateBuffer(uint32_t bufferIndex, uint32_t nextT) { |
Haviital | 0:89ca7e98cd78 | 7 | |
Haviital | 0:89ca7e98cd78 | 8 | uint32_t index= 0; |
Haviital | 0:89ca7e98cd78 | 9 | for(uint32_t t=nextT; t<nextT+BUFFER_SIZE; t++,index++) |
Haviital | 0:89ca7e98cd78 | 10 | //buffers[nextBufferIndexToFill][index] = t & (t>>8); |
Haviital | 0:89ca7e98cd78 | 11 | buffers[bufferIndex][index] = ((t<<1)^((t<<1)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7; |
Haviital | 0:89ca7e98cd78 | 12 | //buffers[bufferIndex][index] = (t*9&t>>4|t*5&t>>7|t*3&t/1024)-1; |
Haviital | 0:89ca7e98cd78 | 13 | } |
Haviital | 0:89ca7e98cd78 | 14 | |
Haviital | 0:89ca7e98cd78 | 15 | |
Haviital | 0:89ca7e98cd78 | 16 | int main () { |
Haviital | 0:89ca7e98cd78 | 17 | mygame.begin(); |
Haviital | 1:ca29888d96a9 | 18 | mygame.display.persistence = true; // Do not clear screen |
Haviital | 0:89ca7e98cd78 | 19 | mygame.display.paletteptr[2] = COLOR_YELLOW; |
Haviital | 0:89ca7e98cd78 | 20 | |
Haviital | 1:ca29888d96a9 | 21 | // The audio engine uses 4 buffers. Generate all 4 buffers ahead. |
Haviital | 0:89ca7e98cd78 | 22 | uint32_t nextT = 0; |
Haviital | 0:89ca7e98cd78 | 23 | generateBuffer(0, nextT); nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 24 | generateBuffer(1, nextT); nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 25 | generateBuffer(2, nextT); nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 26 | generateBuffer(3, nextT); nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 27 | |
Haviital | 1:ca29888d96a9 | 28 | // Set global audio variables |
Haviital | 0:89ca7e98cd78 | 29 | currentBuffer = 0; |
Haviital | 0:89ca7e98cd78 | 30 | currentPtr = buffers[currentBuffer]; |
Haviital | 0:89ca7e98cd78 | 31 | endPtr = currentPtr + BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 32 | |
Haviital | 1:ca29888d96a9 | 33 | // Init audio stream. |
Haviital | 0:89ca7e98cd78 | 34 | pokPlayStream(); // activate stream |
Haviital | 0:89ca7e98cd78 | 35 | mygame.sound.ampEnable(true); |
Haviital | 0:89ca7e98cd78 | 36 | mygame.sound.playMusicStream(); |
Haviital | 0:89ca7e98cd78 | 37 | |
Haviital | 1:ca29888d96a9 | 38 | // Print to the screen once. |
Haviital | 0:89ca7e98cd78 | 39 | mygame.display.set_cursor(0,0); |
Haviital | 0:89ca7e98cd78 | 40 | mygame.display.println("ByteBeat function:"); |
Haviital | 0:89ca7e98cd78 | 41 | mygame.display.println(""); |
Haviital | 0:89ca7e98cd78 | 42 | mygame.display.color = 2; |
Haviital | 0:89ca7e98cd78 | 43 | mygame.display.println("((t<<1)^((t<<1)+(t>>7)"); |
Haviital | 0:89ca7e98cd78 | 44 | mygame.display.println("&t>>12))|t>>(4-(1^7&"); |
Haviital | 0:89ca7e98cd78 | 45 | mygame.display.println("(t>>19)))|t>>7"); |
Haviital | 0:89ca7e98cd78 | 46 | |
Haviital | 0:89ca7e98cd78 | 47 | uint32_t nextBufferIndexToFill = 0; |
Haviital | 0:89ca7e98cd78 | 48 | while (mygame.isRunning()) { |
Haviital | 0:89ca7e98cd78 | 49 | if (mygame.update()) { |
Haviital | 0:89ca7e98cd78 | 50 | |
Haviital | 1:ca29888d96a9 | 51 | // Fill the next buffer if it is not used currently |
Haviital | 0:89ca7e98cd78 | 52 | if((uint32_t)currentBuffer != nextBufferIndexToFill) { |
Haviital | 0:89ca7e98cd78 | 53 | |
Haviital | 1:ca29888d96a9 | 54 | // Generate buffer |
Haviital | 0:89ca7e98cd78 | 55 | generateBuffer(nextBufferIndexToFill, nextT); |
Haviital | 0:89ca7e98cd78 | 56 | |
Haviital | 0:89ca7e98cd78 | 57 | if(++nextBufferIndexToFill > 3) |
Haviital | 0:89ca7e98cd78 | 58 | nextBufferIndexToFill = 0; |
Haviital | 0:89ca7e98cd78 | 59 | nextT += BUFFER_SIZE; |
Haviital | 0:89ca7e98cd78 | 60 | } |
Haviital | 0:89ca7e98cd78 | 61 | } |
Haviital | 0:89ca7e98cd78 | 62 | } |
Haviital | 0:89ca7e98cd78 | 63 | } |