Jump to content
Sign in to follow this  
NatureTM

Music with only a speaker, a LP, and 30 lines of code

Recommended Posts

Hi everyone, it's been awhile.

 

I just wanted to share something interesting lasershark mentioned on my blog awhile ago.  It's basically a way to make music with one line of code.  It took about 15 minutes to implement on a g2553 and only requires a launchpad and a speaker.  I found it pretty entertaining.  Just connect one terminal of the speaker to Port 1.2 and the other to ground, or drive it with a transistor.

 

Original project @ http://canonical.org/~kragen/bytebeat/

 

#include "msp430g2553.h"

#define MCLK						8000000
#define OUTPUT_SAMPLES_PER_SECOND	                8000

#define PIN_SPEAKER					BIT2

unsigned long t = 0;

unsigned char sample;

void main(void) {
	WDTCTL = WDTPW + WDTHOLD;
	
	DCOCTL = CALDCO_8MHZ;
	BCSCTL1 = CALBC1_8MHZ;

	P1SEL |= PIN_SPEAKER;
	P1DIR |= PIN_SPEAKER;

	TA0CTL = TASSEL_2 | MC_1;
	TA0CCR0 = (0x0001 << 8) - 1;
	TA0CCTL1 |= OUTMOD_7;

	TA1CTL = TASSEL_2 | MC_1;
	TA1CCR0 = MCLK / OUTPUT_SAMPLES_PER_SECOND - 1;
	TA1CCTL0 |= CCIE;

	_enable_interrupts();

	while(1){
		sample = ((t<<1)^((t<<1)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7;
//		sample = t*(((t>>12)|(t>>8))&(63&(t>>4)));
//		sample = (t*(t>>5|t>>8))>>(t>>16);
//		sample = t*(((t>>9)|(t>>13))&(25&(t>>6)));
//		sample = t*(((t>>11)&(t>>8))&(123&(t>>3)));

//		sample = (t*5&t>>7)|(t*3&t>>10);
//		sample = (t&t%255)-(t*3&t>>13&t>>6);
//		sample = t>>4|t&((t>>5)/(t>>7-(t>>15)&-t>>7-(t>>15)));
//		sample = (t*9&t>>4|t*5&t>>7|t*3&t/1024)-1;
//		sample = ((t*(t>>12)&(201*t/100)&(199*t/100))&(t*(t>>14)&(t*301/100)&(t*399/100)))+((t*(t>>16)&(t*202/100)&(t*198/100))-(t*(t>>17)&(t*302/100)&(t*298/100)));
//		sample = t*(t^t+(t>>15|1)^(t-1280^t)>>10);
//		sample = t&t>>8;

		//44 khz
//		sample = ((t/2*(15&(0x234568a0>>(t>>8&28))))|t/2>>(t>>11)^t>>12)+(t/16&t&24);
//		sample = ((t*("36364689"[t>>13&7]&15))/12&128)+(((((t>>12)^(t>>12)-2)%11*t)/4|t>>13)&127);

		t++;
		LPM0;
	}
}

#pragma vector = TIMER1_A0_VECTOR
__interrupt void T1A0_ISR(void)
{
	TA0CCR1 = sample;
	LPM0_EXIT;
}

 

Switch which "sample =" line is uncommented to change the "song."  The songs are from http://pelulamu.net/countercomplex/music_formula_collection.txt and there are more in that file.

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...