Algorithmic music?

This forum is for artists to post their music and graphics or discuss artistic matters that could be used in Uzebox games.
User avatar
uze6666
Site Admin
Posts: 4449
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Algorithmic music?

Post by uze6666 » Sun Mar 27, 2016 4:18 pm

I repost an entry found on Bitbox blog. It's quite interesting. Can you make music with a one line equation? As it turns out yes, you can! It has a certain low-fi industrial technoish sound but the results are amazing.



Original post explaining it all.
http://countercomplex.blogspot.ca/2011/ ... ne-of.html

User avatar
nebososo
Posts: 188
Joined: Sun Oct 04, 2009 10:33 pm
Location: Curitiba, Paraná, Brazil
Contact:

Re: Algorithmic music?

Post by nebososo » Mon Mar 28, 2016 6:14 am

I'm absolutely amazed, I had absolutely no idea this could be done. I wonder if it would be possible to hack something like this onto the kernel, 15.734khz/2 is pretty close to 8 khz ;)

User avatar
Jubatian
Posts: 1355
Joined: Thu Oct 01, 2015 9:44 pm
Location: Hungary
Contact:

Re: Algorithmic music?

Post by Jubatian » Mon Mar 28, 2016 7:22 am

It wouldn't be hard to do that regarding finding where to put them. You probably would have to replace a channel in the inline mixer so in every second call it evaulates one step of the algorithm and sends it in the mixing buffer. Some of the one liners are a bit demanding, though (although they don't need too many loads & stores). Fun stuff, how to get something passing for a music in very little size :)

User avatar
D3thAdd3r
Posts: 2422
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Algorithmic music?

Post by D3thAdd3r » Wed Mar 30, 2016 2:25 am

I figured this should be easy with the vsync mixer and it was, not saying this is the best implementation but this code is the coolest song I could get. It's fairly long and if you forgive the harshness it can be kind of catchy even sounding like multiple horribly distorted instruments at once.

The code is dead simple, just make sure to use -DSOUND_MIXER=0, and you could also do as nebososo stated and do 15khz/2 by doubling the calculated value across 2 bytes in the mix buffer. I am doing something wrong since when I copy the equations and tried the 15/2 method they don't sound like the examples, well no kernel reading or anything for a quick experiment 8-)

Code: Select all

void main(){

//	GetPrngNumber(GetTrueRandomSeed());
	int32_t t = 0;
	while(1){

		WaitVsync(1);

		for(uint8_t i=0;i<512;i+=1){//2
			int16_t  c = (t|(t>>9|t>>7))*t&(t>>11|t>>9);
			mix_buf[i+0] = c>>0;
			//mix_buf[i+1] = c>>0;
//			t+=(GetPrngNumber(0)%2);
			t++;
		}
	}

}

Attachments
harsh_grooves.hex
(24.58 KiB) Downloaded 127 times

User avatar
uze6666
Site Admin
Posts: 4449
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Algorithmic music?

Post by uze6666 » Wed Mar 30, 2016 3:13 am

The code is dead simple, just make sure to use -DSOUND_MIXER=0, and you could also do as nebososo stated and do 15khz/2 by doubling the calculated value across 2 bytes in the mix buffer. I am doing something wrong since when I copy the equations and tried the 15/2 method they don't sound like the examples, well no kernel reading or anything for a quick experiment 8-)
We kinda hear the tune. Though you fill the whole ring buffer in one shot and that probably overwrites what not been yet been played sometimes causing a lot of distortion. The way to do it is to only write to the bank that is not played and then wait til the current one finish then write to the other. Use the mix_bank global variable for this. So: if mix_bank=0, write to mix_buf[0..255] otherwise to mix_buf[256..511]. Ah and don't use WaitVsync(1) in the main loop. And probably use SetRenderingParameter(1,1) or something to maximize the cpu cycles. :)

User avatar
D3thAdd3r
Posts: 2422
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Algorithmic music?

Post by D3thAdd3r » Wed Mar 30, 2016 4:16 am

I forgot that is a double buffer and that seems to make a difference!

I didn't get it to sound right after playing with it a bit, but perhaps better. At least it seems to sound, timing wise, the same as the algorithms given; though distorted. My understanding must be wrong, as I should think a full height screen can finish in time to do this only 256 times. Also I can't understand how WaitVsync(1) couldn't be the right thing to do since it's a function of time? It seems the abstract machine given is more like a linear file output, then played back @ 8khz which actually makes the sound right.

User avatar
uze6666
Site Admin
Posts: 4449
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Algorithmic music?

Post by uze6666 » Wed Mar 30, 2016 4:25 am

Two mistakes in my previous post. :? The buffers are [0..261] [262...524]. Then, yeah you can use technically use vsync check but it's redundant since mix_bank flips each vsync. In fact the best place to mix such a thing would be using a vsync handler.

User avatar
D3thAdd3r
Posts: 2422
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Algorithmic music?

Post by D3thAdd3r » Wed Mar 30, 2016 4:58 am

Haha...wow I was trying to slap it together too quick and would have been faster to actually think about the details. uint8_t for variable counting on a 524 byte buffer...well also the point they are not 256 byte buffers was quite useful to know...also using 15/2 idea. I guess it was funner than if it worked straight away. It sounds much better now I think you will agree :D

Press any button to change between a few different algorithms which I may or may not have copied correctly.

Code: Select all

void main(){

//	GetPrngNumber(GetTrueRandomSeed());
	SetMasterVolume(255);
	int32_t t = 0;
	uint16_t controllerState,prevControllerState;
	uint8_t step = 0;
	SetRenderingParameters(1,1);
	
	while(1){

		WaitVsync(1);
		prevControllerState = controllerState;
		controllerState = ReadJoypad(0);
		
		if(controllerState && !prevControllerState){
			t = 0;
			if(++step > 5)
				step = 0;
		}
		
		for(uint16_t i=(mix_bank==1?0:262);i<(mix_bank==1?261:524);i+=2){//2
			int16_t c;
			uint8_t o;
			switch(step){
				case 0:
				c = (uint16_t)(t>>6UL|t|t>>(t>>16UL))*10UL+((t>>11UL)&7UL);
				break;
				case 1:
				c = (t|(t>>9|t>>7))*t&(t>>11|t>>9);
				break;
				case 2:
				c = t*5&(t>>7)|t*3&(t*4>>10);
				break;
				case 3:
				c = (t>>7|t|t>>6)*10+4*(t&t>>13|t>>6);
				break;
				case 4:
				c = ((t&4096)?((t*(t^t%255)|(t>>4))>>1):(t>>3)|((t&8192)?t<<2:t));
				break;
				case 5:
				c = ((t*(t>>8|t>>9)&46&t>>8))^(t&t>>13|t>>6);
				default:
				break;
			}

			o = c / 1;
			mix_buf[i+0] = o;
			mix_buf[i+1] = o;
//			t+=(GetPrngNumber(0)%2);
			t++;
		}
	}

}
The second one is pretty bad ass I think, whether or not it's right.
Attachments
corrected.hex
(27.07 KiB) Downloaded 164 times

User avatar
nebososo
Posts: 188
Joined: Sun Oct 04, 2009 10:33 pm
Location: Curitiba, Paraná, Brazil
Contact:

Re: Algorithmic music?

Post by nebososo » Wed Mar 30, 2016 2:53 pm

It s
D3thAdd3r wrote:Haha...wow I was trying to slap it together too quick and would have been faster to actually think about the details. uint8_t for variable counting on a 524 byte buffer...well also the point they are not 256 byte buffers was quite useful to know...also using 15/2 idea. I guess it was funner than if it worked straight away. It sounds much better now I think you will agree :D
Awesome, it sounds perfect now :P. Now to see that actually being used in a game or demo :lol:

User avatar
D3thAdd3r
Posts: 2422
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Algorithmic music?

Post by D3thAdd3r » Wed Mar 30, 2016 8:15 pm

nebososo wrote: Now to see that actually being used in a game or demo
It is a pretty interesting idea. There are parts and pieces too that sound like decent sound effects you could just run that small piece and mix it over the top. Maybe do a tiny Uzebox game that takes advantage of user ram tiles, entirely procedural even.

I made a weird visualizer(mode 8) and threw a video up on my channel for those interested:

youtube quality distorted the sound on some, check the rom for a more accurate idea of what the algorithms sound like.
visualizer.hex
not tested on hardware...
(23.68 KiB) Downloaded 176 times
Attachments
ALGMUSIC.UZE
(8.91 KiB) Downloaded 128 times
Last edited by D3thAdd3r on Sun Feb 19, 2017 2:56 pm, edited 1 time in total.
Reason: added uze file

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests