Playing music

I suppose some sort of REAL speed test might be needed though.

Btw. are you using any extra RAM buffers or just feeding the mixed samples to the PokittoLib sound buffer?

Very little RAM is being usedā€¦

typedef struct{
    bool playSample;
    int soundPoint;
    const uint8_t *currentSound;
    uint32_t currentSoundSize;
    int volume;
    int speed;
    int repeat;
}sampletype;

extern sampletype snd[4]; // up to 4 sounds at once?

The samples are pulled straight from ROM, as is the tune data.

uint8_t mixSound()
{
    int temp = 0;
    signed int ss[4];
    for(int s=0; s<4; s++){
        snd[s].soundPoint++;

        int currentPos = (snd[s].soundPoint*snd[s].speed)>>8;

        if( currentPos >= snd[s].currentSoundSize){
            if(snd[s].repeat){
                snd[s].soundPoint = snd[s].repeat;
            }else{
                snd[s].playSample=0;
                snd[s].soundPoint=0;
            }
        }

        ss[s] = snd[s].currentSound[currentPos] -128;
        ss[s] *= snd[s].volume;
        ss[s] = ss[s]>>8;
        ss[s] *= snd[s].playSample; // will be 1 or 0, if not playing, will silence

     }
    temp = (ss[0] + ss[1] + ss[2] + ss[3])/4;
    return temp +128;
}


inline void pokSoundBufferedIRQ() {

#if POK_AUD_TRACKER
           uint32_t output = mixSound();
#else
           uint32_t output = soundbuf[soundbufindex+=Pokitto::streamon];
#endif

           if (soundbufindex==SBUFSIZE){
                soundbufindex=0;
           }

           //if (p==sizeof(beat_11025_raw)) p=0;
           //soundbuf[soundbufindex++] = output;
           //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
           //obj->pwm->MATCHREL1 = t_on;
           output *= discrete_vol_multipliers[discrete_vol];
           output >>= 8;
           dac_write(output);

           //setDRAMpoint(pixx, pixy);
}

Looks very neat and simple. Is the speed parameter related to the note pitch?

yes, I have a list of speeds,

const int note_speed[]={
16,17,18,19,20,21,23,24,25,27,29,30, // C, C#, D, D#, E, F, F#, G, G#, A, A#, B,
32,34,36,38,40,43,45,48,51,54,57,60,
64,68,72,76,81,85,91,96,102,108,114,121,
128,136,144,152,161,171,181,192,203,215,228,242,
256,271,287,304,323,342,362,384,406,431,456,483,
512,542,575,609,645,683,724,767,813,861,912,967,
1024,1085,1149,1218,1290,1367,1448,1534,1625,1722,1825,1933,
2048,2170,2299,2435,2580,2734,2896,3068,3251,3444,3649,3866,
4096,4339,4598,4871,5161,5467,5793,6137,6502,6889,7298,7732
};

// new playsound function
uint8_t playSound(int channel, const unsigned char *sound, int volume = 255, int speed=255, int offset=0){
    int dataOffset = 14 + offset;


    // get sound length
    uint32_t soundSize = 0;
    for(int t=0; t<4; t++){
        soundSize <<= 8;
        soundSize |= sound[t] & 0xFF;
    }

    int sampleRate = 0;
    for(int t=0; t<2; t++){
        sampleRate <<= 8;
        sampleRate |= sound[t+4] & 0xFF;
    }

    // get repeat start
    int repeatStart = 0;
    for(int t=0; t<4; t++){
        repeatStart <<= 8;
        repeatStart |= sound[t+6] & 0xFF;
    }

    // get repeat end
    int repeatEnd = 0;
    for(int t=0; t<4; t++){
        repeatEnd <<= 8;
        repeatEnd |= sound[t+10] & 0xFF;
    }

    // cheat, if there is a looping sample, make the size the length of the loop
    if(repeatEnd != 0){
        soundSize = repeatEnd;
        // also cheat for start loop
        if(repeatStart == 0){
           repeatStart = 1;
        }
    }

    float spd = (POK_AUD_FREQ / (float)sampleRate);

    Pokitto::snd[channel].currentSound = &sound[dataOffset];          // sound to play
    Pokitto::snd[channel].volume = volume;               // volume
    Pokitto::snd[channel].speed = (speed/spd);           // recalculated above
    Pokitto::snd[channel].currentSoundSize = soundSize;  // length of sound array
    Pokitto::snd[channel].soundPoint = 0;                // where the current sound is upto
    Pokitto::snd[channel].repeat = repeatStart * 256 / Pokitto::snd[channel].speed;          // repeat point
    Pokitto::snd[channel].playSample = 1;                // trigger sample playing

    return channel;
}

I created this from a list of note frequencies I found online and recalculated them into ā€˜precentagesā€™ out of 255 (instead of 100) then used a multiply and divide to remove the need for floats.

later today, Iā€™ll update a music player example and upload it to show whats going on.

As the mixSound() is called very often it might be worthwhile to do a separate if-branch for the silence (playSample==0) at the very beginning of the for block. In ā€œsilentā€ sample case you could just store zero to the ss[] buffer and increase and check the soundPoint counter, and skip the rest of the calculations.

Moved the discussion over here from another threadā€¦

Btw. I cannot find your PR in PokittoLib GitHub

I suppose this can handle sound effects too? I mean e.g. 3 channels for music and 1 channel for effects?

Eventually, we should support 4-bit samples too but lets do first things first.

Also, you might not know Jonneā€™s fix to the DAC audio(?). I do not know if it affects to your audio system but for streaming audio, it was essential. The fix is here in my PR ( Pokitto::dac_write() change only): https://github.com/pokitto/PokittoLib/pull/76/commits/531e507fd352121acd17c7b673d672b9c1cd4c9a

It will be about 2 months ago I think.

[edit] maybe not.
Iā€™m trying to add to a new clone of pokittolib, but now Iā€™m having trouble with mixMode screen mode.
MusicPlayer\main.cpp|223|undefined reference to `Pokitto::Display::scanTypeā€™|
Which is odd, because that has a) been working for ages and b) is defined, I can see it in the various source files.

I think I managed a PR. I hate github.

1 Like

Would you be open to some code improvement suggestions?

Also, is there a GitHub link for the code as it currently stands?

You mean change it all to a form that I can no longer understand myself?

1 Like

For anyone interested, Iā€™m using the following javascript tool to convert 8bit .wav files to those used by my code.

To use simply (choose File) -> (Convert) -> (copy code)
The (Resample) button will shorten the sample by removing every second byte. Donā€™t bother with that.


<html>
	<title>Wav to Pokitto</title>
	<style type="text/css">
		
		#progressbarOuter{
			display: block;
			border: 2px solid gray;
			color: gray;
			background-color: white;
			border-radius: 8px;
			font-size: 20px;
			font-weight: bold;
			width:204px;
			height:40px;
			position:relative;
		}

		#progressbarOuter span{
			display:block;
			text-align:center;
			width:100%;
			position:absolute;
			z-index:2;
			top:0;
			left:0;
			line-height:36px;
		}
		#progressbarInner{
			border: 0px solid gray;
			color: #000;
			background-color: #88f;
			border-radius: 3px;
			font-size: 20px;
			font-weight: bold;
			width:0px;
			height:36px;
			text-align: center;
			position:relative;
			z-index:1;
			padding:2px;
		}
		
		.custom-file-input {
			display: block;
		}
		.custom-file-input input {
			visibility: hidden;
			width: 100px;
		}
		.custom-file-input:before {
			width:148px;
			content: 'Choose File';
			display: block;
			border: 2px solid gray;
			color: gray;
			background-color: white;
			padding: 8px 12px 8px 44px;
			border-radius: 8px;
			font-size: 20px;
			font-weight: bold;
			background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pg0KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDE5LjAuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPg0KPHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCINCgkgdmlld0JveD0iMCAwIDUxMiA1MTIiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDUxMiA1MTI7IiB4bWw6c3BhY2U9InByZXNlcnZlIj4NCjxwYXRoIHN0eWxlPSJmaWxsOiNFQ0VERUY7IiBkPSJNMTA2LjMyNCw4LjAxN2MtOS40NDYsMC0xNy4xMDIsNy42NTYtMTcuMTAyLDE3LjEwMnY0NjEuNzYyYzAsOS40NDYsNy42NTYsMTcuMTAyLDE3LjEwMiwxNy4xMDINCgloMzc2LjI1MWM5LjQ0NiwwLDE3LjEwMi03LjY1NiwxNy4xMDItMTcuMTAyVjg0Ljk3N2wtNzYuOTYtNzYuOTZIMTA2LjMyNHoiLz4NCjxwYXRoIHN0eWxlPSJmaWxsOiNEOURDREY7IiBkPSJNNDM5LjgxOCw4NC45NzdoNTkuODU4bC03Ni45Ni03Ni45NnY1OS44NThDNDIyLjcxNSw3Ny4zMTksNDMwLjM3Myw4NC45NzcsNDM5LjgxOCw4NC45Nzd6Ii8+DQo8cGF0aCBzdHlsZT0iZmlsbDojQzZDQ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UtNy45Ni02Ljk4Yy0zLjk4OCwwLjAwNS03LjQ0OSwzLjA2Ni03Ljk0NCw3LjAyM2wtMTguNjIxLDE0OC45NzRsLTE2LjgxMy0xMDYuNDc1DQoJYy0wLjYwOC0zLjg1My0zLjkwMy02LjcwOS03LjgwNC02Ljc2NWMtMy44OTUtMC4wNTctNy4yNzYsMi43MDQtNy45OTQsNi41MzhsLTE4LjQ0MSw5OC4zNDZsLTguNjExLTM4Ljc0OA0KCWMtMS40NTUtNi41NDMtMTAuMjA3LTguNDQ0LTE0LjIzOS0zLjA3MUwxODcuODI1LDM2Ny43aC0zOC43NDdDMTQ0LjY1MSwzNjcuNjk5LDE0MS4wNjMsMzcxLjI4OSwxNDEuMDYzLDM3NS43MTZ6Ii8+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8Zz4NCjwvZz4NCjxnPg0KPC9nPg0KPGc+DQo8L2c+DQo8L3N2Zz4NCg==');
			background-size: 30px auto; background-position: 3px center;  background-repeat:no-repeat;
		}
		
		.button {
			display: block;
			width:208px;
			border: 2px solid gray;
			color: gray;
			background-color: white;
			padding: 8px 44px 8px 44px;
			border-radius: 8px;
			font-size: 20px;
			font-weight: bold;
		}

		*:focus {
			outline: none;
		}
	
	</style>

<script>

	// wav file format info from - https://sites.google.com/site/musicgapi/technical-documents/wav-file-format

	var inArray;
	var reSample;
	
	function init() {
		document.getElementById('files').addEventListener('change', handleFileSelect, false);
		reSample = 0;
	}
		
	function decToHex(buffer) {
		var s = '', h = '0123456789ABCDEF';
		s = h[buffer >> 4] + h[buffer & 15];
		return '0x' + s;
	}
		
	function DoConversion() {	
		// This is where your converter goes
		if (inArray.length<2) {return;}
		
		// check wave header
		if( !(inArray[0]=='R' && inArray[1]=='I' && inArray[2]=='F' && inArray[3]=='F' )){
			//	not wav file.
			alert('Not a valid .wav file.');
			return;
		}
		
		// check sample format, must be PCM
		if(inArray[20].charCodeAt() != 1){
			alert('Not PCM format' + 'Is is ' + inArray[20].charCodeAt() );
			return;
		}
		
		// check channels, must be MONO
		if(inArray[22].charCodeAt() != 1){
			alert('Not MONO' );
			return;
		}

		bitRate = 0;
		// samples, must be 8bit
		if(inArray[34].charCodeAt() == 8){
			bitRate = 8;
		}

		if(inArray[34].charCodeAt() == 16){
//			bitRate = 16;
			alert('Bit Rate = ' + bitRate + 'it must be 8-bit!');
			return;
		}

		
		txt = document.getElementById("theOutput");
		
		//var fileLength = inArray.length - 44;
		
			// 41,42,43,44
			fileLength =  inArray[43].charCodeAt() << 24;
			fileLength += inArray[42].charCodeAt() << 16;
			fileLength += inArray[41].charCodeAt() << 8;
			fileLength += inArray[40].charCodeAt();
		
		fileLength = ((fileLength+1) >> 1)<<1;
		
		txt.value = 'const unsigned char ';
		txt.value += files.value.substr(files.value.lastIndexOf('\\') + 1).split('.')[0];

		// look for 'smpl' data
		smplOffset = 44 + fileLength;
		magicString =  String.fromCharCode(inArray[smplOffset  ].charCodeAt());
		magicString += String.fromCharCode(inArray[smplOffset+1].charCodeAt());
		magicString += String.fromCharCode(inArray[smplOffset+2].charCodeAt());
		magicString += String.fromCharCode(inArray[smplOffset+3].charCodeAt());

		if(reSample == 0){
			txt.value += '[' + (fileLength + 14);
		}else{
			txt.value += '[' + (Math.floor(fileLength/2) + 6);
		}
		txt.value += ']={\n';

		txt.value += '	';

		fl = fileLength;
		if(reSample!=0) fl=Math.floor(fl/2);
		txt.value += decToHex((fl >> 24) & 255) + ', ';
		txt.value += decToHex((fl >> 16) & 255) + ', ';
		txt.value += decToHex((fl >> 8) & 255) + ', ';
		txt.value += decToHex(fl & 255) + ', ';

		txt.value += '// sample length = ' + fl + ' bytes.\n	';

		
		//sRate = parseInt(document.getElementById("samplerate").value, 10);
		//txt.value += decToHex(inArray[27].charCodeAt()) + ', ';
		//txt.value += decToHex(inArray[26].charCodeAt()) + ', ';
		
		//sRate = inArray[27].charCodeAt();
		//sRate = (sRate << 8) + inArray[26].charCodeAt();
		sRate = inArray[25].charCodeAt();
		sRate = (sRate << 8) + inArray[24].charCodeAt();

		if(reSample == 0){
			txt.value += decToHex(inArray[25].charCodeAt()) + ', ';
			txt.value += decToHex(inArray[24].charCodeAt()) + ', ';
		}else{
			sRate = Math.floor(sRate/2);
			txt.value += decToHex( (sRate >> 8)&255 ) + ', ';
			txt.value += decToHex( sRate &255 ) + ', ';
		}

		txt.value += '// sample rate = ' + sRate + 'Hz\n';

		// add loop data here
		alert('File Length = '+fileLength+'\n'+'smpl? = ' + magicString);
		if(magicString.includes('smp')){
			txt.value += '	// loop found\n';
			numLoops =  inArray[smplOffset+39].charCodeAt() << 24;
			numLoops += inArray[smplOffset+38].charCodeAt() << 16;
			numLoops += inArray[smplOffset+37].charCodeAt() << 8;
			numLoops += inArray[smplOffset+36].charCodeAt();
			//txt.value += '	// number of loops = ' + numLoops + '\n';
			
			loopStart =  inArray[smplOffset+55].charCodeAt() << 24;
			loopStart += inArray[smplOffset+54].charCodeAt() << 16;
			loopStart += inArray[smplOffset+53].charCodeAt() << 8;
			loopStart += inArray[smplOffset+52].charCodeAt();
			//txt.value += '	// start of loop = ' + loopStart + '\n';

			fl = loopStart;
			if(reSample!=0) fl=Math.floor(fl/2);
			txt.value += '	';
			txt.value += decToHex((fl >> 24) & 255) + ', ';
			txt.value += decToHex((fl >> 16) & 255) + ', ';
			txt.value += decToHex((fl >> 8) & 255) + ', ';
			txt.value += decToHex(fl & 255) + ', ';
			txt.value += '// loop start = ' + fl + '\n';

			loopEnd =  inArray[smplOffset+59].charCodeAt() << 24;
			loopEnd += inArray[smplOffset+58].charCodeAt() << 16;
			loopEnd += inArray[smplOffset+57].charCodeAt() << 8;
			loopEnd += inArray[smplOffset+56].charCodeAt();
			//txt.value += '	// end of loop = ' + loopEnd + '\n';

			fl = loopEnd;
			if(reSample!=0) fl=Math.floor(fl/2);
			txt.value += '	';
			txt.value += decToHex((fl >> 24) & 255) + ', ';
			txt.value += decToHex((fl >> 16) & 255) + ', ';
			txt.value += decToHex((fl >> 8) & 255) + ', ';
			txt.value += decToHex(fl & 255) + ', ';
			txt.value += '// loop end = ' + fl + '\n';
		
		}else{
			txt.value += '	// loop not found\n';
			txt.value += '	0x00, 0x00, 0x00, 0x00,	// no start\n';
			txt.value += '	0x00, 0x00, 0x00, 0x00,	// no finish\n';
		
		}





		txt.value += '	// sample data\n';
	
		txt.value += '	';

		var counter = 0;
		if(bitRate == 8){
		
			 fileLength+=44;
		
			// use an asyc loop so that the UI doesn't freeze.
			(function loop(i){
				
				var next_i = i+256;
				if(next_i > fileLength+1) next_i = fileLength+1;
				
				while( i < next_i){
					txt.value += decToHex(inArray[i-1].charCodeAt()) + ', ';
					if(++counter % 16 == 0 ) txt.value += '\n	';
					++i;
					if(reSample != 0){ ++i; }
				}
				var container = document.getElementById("progressbarInner");
				container.style.width = (200/fileLength) * i;
				
				container = document.getElementById("percentage");
				container.innerHTML = Math.floor((100/fileLength) * i) + '%';
				
				if(i<fileLength+1){
					setTimeout(function(){ loop(i) },1); 
				} else {
					txt.value += '\n};\n\n';
				}
			}(45)); // start copying at byte 44
		} // 8bit
				
	}

	function handleFileSelect(evt) {
		// This takes the file selected (and should be easily convertable to handle many files, but sod that for now!!) 
		// and plonks the binarydata into inArray.
		
		var files = evt.target.files
		for (var i = 0, f; f = files[i]; i++) {
			var reader = new FileReader()
			
			reader.onload = (function(theFile) {
					// (2) - This is next, and is what happens once the file (below) has loaded.
				return function(e) {
					inArray=e.target.result;	// This plonks the array into the array buffer variable thing
				}
			})(f)
				// (1) - This says "Read the file", and happens first
			reader.readAsBinaryString(f)
		}
	}
	
	function copyCode(){
		txt = document.getElementById("theOutput");
		
		txt.select();
        try{
			return document.execCommand("copy");  // Security exception may be thrown by some browsers.
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        }
		
	}
	
	function doReSample(){
		reSample = 1;
		DoConversion();
	}

</script>

<body onload="init()">

<h1>Wav to Pokitto converter 1.0</h1>

<span>Input file must be 8-bit mono PCM .wav</span><br/><br/>

<label class="custom-file-input">
	<input id="files" type="file" accept=".wav">
</label>

<button onclick="reSample=0; DoConversion();return false;" class="button">Convert</button><br/>

<button onclick="doReSample(); return false;" class="button">Resample</button><br/>

<div id="progressbarOuter"><span id="percentage">0%</span><div id="progressbarInner"></div></div><br/>

<button onclick="copyCode();return false;" class="button">Copy Code</button><br/>

<textarea id="theOutput" name="theOutput" rows="25" cols="120"></textarea>

<br/><br/>
<div>Code by <a href="https://twitter.com/Spinal_Cord" title="@Spinal_Cord">@Spinal_Cord</a>, <a href="https://twitter.com/DiziHelen" title="@DiziHelen">@DiziHelen</a> and <a href="https://twitter.com/Jayenkai" title="@Jayenkai">@Jayenkai</a></div>
<div>Icons made by <a href="https://www.freepik.com/" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" 			    title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" 			    title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div>
</body></html>

Are you using a modified version of the pokitto library?

Pokitto::snd and Pokitto::Display::scanType donā€™t seem to exist.

I can explain every single change I make.

If anything they should make the code slightly easier to understand because Iā€™d be getting rid of some of the low-level stuff in favour of some equivalent structs.

1 Like

I think simple changes that improve clarity are good, like adding structs or enums.

Adding templates are frightening for some people.

Thatā€™s what the main focus would be.

E.g. instead of all the byte copying in playSound, a Sound struct can be used.

People seem to forget that you donā€™t have to store your data as arrays of bytes, you can just use structs.

Templates really donā€™t deserve the level of FUD people like to spread.

Thereā€™s some really ridiculous examples that do crazy things,
but those kinds of uses are either very special cases (like Boost.Spirit) or just people showing off for the sake of it.

The most common uses are pretty easy to understand.
Most people can understand the idea of std::min(a, b) and std::size(array) even if they donā€™t know how they work internally.

Examples

For example, std::min is no more complex than defining a min macro:

template< typename Type >
const Type & min(const Type & a, const Type & b)
{
	return (a < b) ? a : b;
}

And you use it in exactly the same way, min(a, b).

Occaisionally the two types donā€™t match and you have to specify which type to use, e.g. min<int>(a, b), but thatā€™s fairly rare and not too difficult to overcome.

And making a function adapt to arrays of different sizesā€¦

template< typename Type, std::size_t size >
void print(const Type (&array)[size])
{
	for(std::size_t index = 0; index < size; ++index)
		Pokitto::Display::println(array[index]);
}

Looks odd at first, but itā€™s easy to use, just print(array) and the compiler infers Type and size for you.

The compiler is smart enough to figure out that if a variable is defined as int array[5] then Type becomes int and size becomes 5 - a process known as template substitution.

Template parameters are a lot like function parameters,
the only real difference is that they can only change at compile time.

Ultimately templates are just C++'s implementation of generic programming and compile-time pattern matching.
Languages like C# and Java also feature generic programming.

1 Like

Yup, I did do a PR with the changes. But as always, Not sure if it was done correctly as I have trouble understanding the exact process with github.

I can see where youā€™re introducing snd.
I can replace that with a local snd for now.

As for scanType, it seems that was introduced a while ago and I havenā€™t updated my library in a while.


Even with a fully updated library Iā€™m getting:
main.cpp:267: undefined reference to 'Pokitto::Display::scanType'.

I assume your setup doesnā€™t have the mixmode example?
Just change the screen mode to something else in the settings.h and donā€™t use my screen junk. Itā€™s only there because I like that screen mode.