The Beatles: Hey Jude in C (Programming)

Bit of fun for a Saturday afternoon… This afternoon’s procrastination was sparked off this morning when I logged in to Facebook. Someone had posted this fantastic flowchart of the Beatles’ Hey Jude lyrics:

Hey-Jude-flowchart

I immediately realised that if the song structure can be put into a flowchart, it can be coded. I’m always looking for fun ways of practicing my programming, and this was suitably humorous and trivial! So I fired up Eclipse, googled the lyrics and got writing. I’m quite proud of the results (although it was disappointingly easy!). I tried to make the code structure as close to how I think as a musician, so the main function calls individual functions that handle the intro, verses, chorus, bridge and outro. These functions then make decisions on what the lyrics should be from choices not dissimilar to the flowchart above. I also tried to make it so that no phrase (perhaps maybe with the exception of “hey Jude”) is repeated, so all repetition in the output is coded. I’d also like to point out, I’m relatively new to C programming, so if anyone spots anything that could be improved, please comment or get in touch. Why have I done this? Well, why not?! The output is shown below, and the code is below that.

/*
 ===============================================
 Name        : Hey_Jude.c
 Author      : C Barlow

 Description :
 Outputs the lyrics to Hey Jude by the Beatles
 (Lyrics © SONY/ATV TUNES LLC; SONY BEATLES LTD)
 ===============================================
 */

#include <stdio.h>
#include <stdlib.h>

void intro(void);
void verse_Line1(void);
void verse_Line2(void);
void verse_Line3(void);
void verse_Line4_5(void);
void chorus(void);
void bridge(void);
void outro(int fadeout);

int verseNo, chorusNo = 1;

int main(void)
{
	intro();

	/* 4 verses */
	for(verseNo = 1; verseNo <= 4; verseNo++)
	{
		/* No chorus between verses 1 and 2 */
		if(verseNo > 2)
		{
			chorus();
		}

		/* verse lines */
		verse_Line1();
		verse_Line2();
		verse_Line3();
		verse_Line4_5();
	}

	/* pass 1 for infinite fadeout */
	outro(0);

	return EXIT_SUCCESS;
}

void intro(void)
{
	printf("The Beatles: Hey Jude in C (programming) Lyrics © SONY/ATV TUNES LLC; SONY BEATLES LTD\n");
	printf("--- -------  --- ---- -- -  -----------  ------   -------- ----- ---  ---- ------- ---\n\n");
}

void verse_Line1(void)
{
	static char *LineOne_Options[] =
 	{
		0,
		"make it bad",
		"be afraid",
		"let me down"
	};

	printf("Hey Jude, don't ");

	/* Verse 4 is the same as verse 1 */
	if(verseNo < 4)
	{
		printf("%s.\n", LineOne_Options[verseNo]);
	}
	else
	{
		printf("%s.\n", LineOne_Options[1]);
	}
}

void verse_Line2(void)
{
	static char *LineTwo_Options[] =
	{
		0,
		"Take a sad song and make it better",
		"You were made to go out and get her",
		"You have found her, now go and get her",
	};

	/* Verse 4 is the same as verse 1 */
	if(verseNo < 4)
	{
		printf("%s.\n", LineTwo_Options[verseNo]);
	}
	else
	{
		printf("%s.\n", LineTwo_Options[1]);
	}

}

void verse_Line3(void)
{
	/* Verse 2 is different to other verses */
	if(verseNo != 2)
	{
		printf("Remember to ");
	}
	else
	{
		printf("The minute you ");
	}

	printf("let her ");

	/* These lyrics alternate between verses */
	if((verseNo % 2) != 0)
	{
		printf("into your heart");
	}
	else
	{
		printf("under your skin");
	}

	printf(",\n");
}

void verse_Line4_5(void)
{
	printf("Then you");

	/* Verse 4 says "you'll" instead of "you" */
	if(verseNo == 4)
	{
		printf("'ll");
	}

	/* These lyrics alternate between verses */
	if((verseNo % 2) != 0)
	{
		printf(" can start ");
	}
	else
	{
		printf(" begin ");
	}

	printf("to make it");

	/* verse 4 leads to bridge */
	if(verseNo != 4)
	{
		printf(" better.\n");
	}
	else
	{
		bridge();
	}

	printf("\n");
}

void chorus(void)
{
	/* Two different choruses */
	if(chorusNo == 1)
	{
		printf("And anytime you feel the pain, hey Jude, refrain,\n");
		printf("Don't carry the world upon your shoulders.\n");
		printf("For well you know that it's a fool who plays it cool,\n");
		printf("By making his world a little colder.\n");
	}
	else if(chorusNo == 2)
	{
		printf("So let it out and let it in, hey Jude, begin,\n");
		printf("You're waiting for someone to perform with.\n");
		printf("And don't you know that it's just you, hey Jude, you'll do,\n");
		printf("The movement you need is on your shoulder.\n");
	}

	printf("\n");

	chorusNo  ;
}

void bridge(void)
{
	printf(",\nBetter, ");

	int i;
	for(i = 0; i < 5; i++)
	{
		printf("better, ");
	}

	printf("WAAAAAA!\n");
}

void outro(int fadeout)
{
	/* Optional loop */
	do
	{
		int NAs;

		/* 11 NA's */
		for(NAs = 0; NAs < 11; NAs++)
		{
			printf("NA");

			/* Long NA's have commas */
			if((NAs == 0) || (NAs == 6) || (NAs == 10))
			{
				printf(",");
			}

			/* Short NA's have hyphens */
			else if ((NAs == 3) || (NAs == 7))
			{
				printf("-");
			}
			else
			{
				printf(" ");
			}
		}

		printf("hey Jude...\n");

	}
	while(fadeout);
	/* Optional loop */
}
Advertisements

2 thoughts on “The Beatles: Hey Jude in C (Programming)

  1. He he, very good. :) I wonder what the easiest song to represent with code is? I guess that 4’33” of silence would be pretty straightforward (with apologies for the Java):

    for (float time = 0; time <= 4.33; time += 0.01)
    {
    System.out.println(…);
    }

    Though I suppose that's a bit of a cheat…

Tell me what you think...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s