14 Replies - 4327 Views - Last Post: 31 May 2009 - 12:09 AM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Play music using C++ and console app

Posted 30 May 2009 - 03:15 PM

I know C/C++ is a powerful language, but is it powerful enough to design a program that would play music when ran through a console. I have no dea how to make a GUI application yet, so i just doing console apps. If so, does anyone have a link I could follow?
Is This A Good Question/Topic? 0
  • +

Replies To: Play music using C++ and console app

#2 chuckb  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 211
  • Joined: 29-May 09

Re: Play music using C++ and console app

Posted 30 May 2009 - 03:26 PM

View PostIngeniousHax, on 30 May, 2009 - 02:15 PM, said:

I know C/C++ is a powerful language, but is it powerful enough to design a program that would play music when ran through a console. I have no dea how to make a GUI application yet, so i just doing console apps. If so, does anyone have a link I could follow?

Hi,
I created a DOS Console game last year using C++. I used the FMOD library and a wrapper I created to add music. It worked like a gem.

Here's a link to the game. I didn't post any source code. Console Game with Music

Regards,
Chuck
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Play music using C++ and console app

Posted 30 May 2009 - 03:32 PM

Before a rather caustic programmer was banned form the site she was working on a snippet to this end. This is some code that was inspired by her work... This is windows specific but it can be extended to play wav's -- I am not sure about MP3's but a little checking about the web should verify that.
#include <iostream>
#include <cstdio> 
#include <cstring>
#include <windows.h>
#include <mmsystem.h>
using namespace std;

	/**
	 * mciCommands[] is an array of mci command strings that the MidiPlayer
	 * class can execute.
	 */
	const char* mciCommands[] = {
		"open \"%s\" type sequencer alias %s", //Open a midi file and assign an alias
		"play %s",			//Play a given alias
		"play %s wait",		//Play a given alias (don't return until finished -- not very helpful but there it is).
		"pause %s",			//Pause a given alias (seems to pause all midi)
		"resume %s",		//Resume a paused alias (seems to start all midi paused)
		"stop %s",			//Stop a given alias (does NOT stop all)
		"close %s"			//Close the file associated with this alias
	};
	/**
	 * MCICommand -- Enumerates the mciCommands array elements.
	 */
	enum MCICommand {
		MCIOPEN,
		MCIPLAY,
		MCIFINNISH,
		MCIPAUSE,
		MCIRESUME,
		MCISTOP,
		MCICLOSE
	};

/**
 *  MidiPlayer is a little class for playing midi files in a C++ console app.
 *  This class demonstrates how you can wrap the windows mciSendString()
 *  function to play media files.
 */
class MidiPlayer {
	private:
	/**
	 * MidiStates establshes names for the running states of a MidiPlayer object.
	 */
	enum MidiStates {
		NOFILE = 0,
		LOADED_BIT = 1,
		OPENED_BIT = 2,
		PLAYING_BIT = 4,
		PAUSED_BIT = 8,

		FILELOADED = LOADED_BIT,
		MIDIREADY = LOADED_BIT | OPENED_BIT,
		PLAYING = MIDIREADY | PLAYING_BIT,
		PAUSED =  MIDIREADY | PAUSED_BIT,
		STOPPED = MIDIREADY
	};

	MidiStates state; //Internal state of the midiplayer.
	char* fileName;  //Name of file to play
	char* alias;	  //Alias Name

	/**
	 * private function to allocate and format a mci command string.
	 */
	char* formatMCICommand(MCICommand cmd) {
		char* ptr = NULL;
		int len = 0;
		switch(cmd) {
			case MCIOPEN: //Special case because it requires 2 arguments
				len = strlen(fileName) + strlen(alias) + strlen(mciCommands[MCIOPEN]);
				ptr = new char[len];
				sprintf(ptr, mciCommands[MCIOPEN], fileName, alias);
				break;
			case MCIPLAY:
			case MCIFINNISH:
			case MCIPAUSE:
			case MCIRESUME:
			case MCISTOP:
			case MCICLOSE:
				len = strlen(alias) + strlen(mciCommands[cmd]);
				ptr = new char[len];
				sprintf(ptr, mciCommands[cmd], alias);
				break;
		};
		return ptr;
	}
	
	bool execCMD(MCICommand cmd) {
		char *cmdStr = formatMCICommand(cmd);
		int result;
		if (cmdStr) {
			result = mciSendString(cmdStr, NULL, 0, NULL);
			delete[] cmdStr;
		}
		return (result == 0);
	}

	public:
	
	MidiPlayer(const char * midiFileName, const char *aliasName) {
		int fileNameLen = strlen(midiFileName);
		int aliasLen = strlen(aliasName);
		if (fileNameLen > 0 && aliasLen >0) {
			fileName = new char[fileNameLen + 1];
			alias = new char[aliasLen + 1];
			strcpy(fileName, midiFileName);
			strcpy(alias, aliasName);
			state = FILELOADED;
		} else {
			fileName = NULL;
			alias = NULL;
			state = NOFILE;
		}
	}
			
	MidiPlayer(const MidiPlayer &that) {
		int fileNameLen = strlen(that.fileName);
		int aliasLen = strlen(that.alias);
		if (fileNameLen > 0 && aliasLen >0) {
			fileName = new char[fileNameLen + 1];
			alias = new char[aliasLen + 1];
			strcpy(fileName, that.fileName);
			strcpy(alias, that.alias);
			state = FILELOADED;
		} else {
			fileName = NULL;
			alias = NULL;
			state = NOFILE;
		}
	}
	
	~MidiPlayer() {
		this->close();
	}
	
	MidiPlayer& operator=(const MidiPlayer &that) {
		int fileNameLen = strlen(that.fileName);
		int aliasLen = strlen(that.alias);
		if (fileNameLen > 0 && aliasLen >0) {
			fileName = new char[fileNameLen + 1];
			alias = new char[aliasLen + 1];
			strcpy(fileName, that.fileName);
			strcpy(alias, that.alias);
			state = FILELOADED;
		} else {
			fileName = NULL;
			alias = NULL;
			state = NOFILE;
		}
		return *this;	
	}
	
	MidiStates getState() {
		return state;
	}
	
	bool chFile(const char *midiFileName, const char* aliasName) {
		bool retVal = false;
		if (state == NOFILE) {
			int fileNameLen = strlen(midiFileName);
			int aliasLen = strlen(aliasName);
			if (fileNameLen > 0 && aliasLen >0) {
				fileName = new char[fileNameLen + 1];
				alias = new char[aliasLen + 1];
				strcpy(fileName, midiFileName);
				strcpy(alias, aliasName);
				state = FILELOADED;
			} else {
				fileName = NULL;
				alias = NULL;
				state = NOFILE;
			}
			retVal = (state == FILELOADED); //Quick convert to bool
		}
		return retVal;	
	}
	
	bool open() {
		if (state == FILELOADED) {
			if (execCMD(MCIOPEN)) { state = MIDIREADY; }
		}
		return (state == MIDIREADY);
	}
	
	bool play() {
		if (state == STOPPED) {
			if (execCMD(MCIPLAY)) { state = PLAYING; }
		}
		return (state == PLAYING);
	}

	bool stop() {
		if (state == PLAYING || state == PAUSED) {
			if (execCMD(MCISTOP)) { state = STOPPED; }
		}
		return (state == STOPPED);
	}

	bool pause() {
		if (state == PLAYING) {
			if (execCMD(MCIPAUSE)) { state = PAUSED; }
		}
		return (state == PAUSED);
	}
	
	bool resume() {
		if (state == PAUSED) {
			if (execCMD(MCIRESUME)) { state = PLAYING; }
		}
		return (state == PLAYING);
	}


	bool close() {
		if (state & FILELOADED > 0) {
			this->stop();
 			execCMD(MCICLOSE);
			state = NOFILE;
		}
		if (fileName != NULL) { delete[] fileName; fileName = NULL; }
		if (alias != NULL) { delete[] alias; alias = NULL; }		
		return (state == NOFILE);
	}
};




int main()
{
	const char midiFile1[] = "C:\\WINDOWS\\Media\\town.mid";
	const char midiFile2[] = "C:\\WINDOWS\\Media\\onestop.mid";
	MidiPlayer midi1(midiFile1, "town");
	MidiPlayer midi2(midiFile2, "onestop");
	midi1.open();
	midi2.open();

	midi1.play();
	Sleep(5000);

	midi2.play();
//	Sleep(1000);
//	midi1.resume();
	system("pause");
	midi1.stop();
	midi1.close();
	midi2.stop();
	midi2.close();
	return 0;
}
This plays 2 midi files at the same time... not sure what I was thinking but that is what it does...
Was This Post Helpful? 0
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Play music using C++ and console app

Posted 30 May 2009 - 03:37 PM

Ok, in Linux how would you go about getting that library? Im looking at the FMOD site now, however, I don't see how it would work with-in the console app.

Could I just subo apt-get install fmod or is it not packaged for distribution?

EDIT:: NickDmax, is that a full program or just the requirements for a midi player? Also, what if I wanted to play an MP3 instead of a midi file?

This post has been edited by IngeniousHax: 30 May 2009 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 chuckb  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 211
  • Joined: 29-May 09

Re: Play music using C++ and console app

Posted 30 May 2009 - 03:46 PM

View PostIngeniousHax, on 30 May, 2009 - 02:37 PM, said:

Ok, in Linux how would you go about getting that library? Im looking at the FMOD site now, however, I don't see how it would work with-in the console app.

Could I just subo apt-get install fmod or is it not packaged for distribution?

EDIT:: NickDmax, is that a full program or just the requirements for a midi player? Also, what if I wanted to play an MP3 instead of a midi file?

Sorry, can't help you with Linux. For FMOD and Windows, I used their FMOD.lib (and a couple of header files) for compiling and development. For distribution, I used their FMOD.dll.

Now, I did start with the basics of learning how to play a sound. After several iterations I built a 'singleton' wrapper class to manage the audio files and a means to load the sound files listed in a text file. I'm thinking of building an audio demo but that probably wouldn't be much help to you.

Good luck and sorry I couldn't really help you much.

Regards,
Chuck
Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Play music using C++ and console app

Posted 30 May 2009 - 08:34 PM

I don't know how to get it to play music that has been recorded, but if you want to make your own tunes, this thread might be helpful. Even if it's not exactly what you were looking for, it's fun to play with. :)

Fun with beep
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,198
  • Joined: 10-May 07

Re: Play music using C++ and console app

Posted 30 May 2009 - 08:51 PM

View PostIngeniousHax, on 30 May, 2009 - 04:37 PM, said:

Ok, in Linux how would you go about getting that library?
I would check out similar projects at source forge. Open source is great for finding existing functionality.

As far as C/C++ being powerful enough to do this... the language is irrelevant. You're still just pushing data. It's a matter of understanding what you are doing. In the event of sound form a console on Windows, it's a little more difficult because of HAL. You don't have the direct hardware access that you had with previous versions of Windows. In Linux, as with all Unix systems, everything is a folder or a directory. So you can do anything from as generic as opening /dev/audio with a file pointer & dumping a wave to it, to getting an audio library & hooking the device, loading mp3 headers, & doing this in a similar manor as you would Windows. Can go either way.

** Edit ** Removed a rant that didn't make sense
Was This Post Helpful? 0
  • +
  • -

#8 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Play music using C++ and console app

Posted 30 May 2009 - 09:42 PM

Oh ok, so you can link in a folder and have it just grab a file from the folder and auto-play in a sense, however you can't just hard code the song into the file and compile it adn have it auto play that way huh?
Was This Post Helpful? 0
  • +
  • -

#9 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Play music using C++ and console app

Posted 30 May 2009 - 10:00 PM

I have found out there is a man page for beep, and is controlled through a header file called IOCTL(ioctl) Which I imagine is short for "in/out control", Don't hold me to that though...

Also a neat little page that displays some frequencies aswell as the man page for beep is-->http://swoolley.org/man.cgi/beep
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,198
  • Joined: 10-May 07

Re: Play music using C++ and console app

Posted 30 May 2009 - 10:02 PM

The console beep outputs through the PC Speaker. That's not the same as playing through the audio device/sound card.
Was This Post Helpful? 0
  • +
  • -

#11 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Play music using C++ and console app

Posted 30 May 2009 - 11:02 PM

Yeah I know...But it's about the closest i'm gonna get for awhile, atleast until I learn how to program more in depth and study data structures more in depth. For now, I will settle with the beep and work my way up, I believe the book I was reading is a little over my head as it goes into sockets and copying the programs and just running them, does help you understand much at all...

I'm gonna try and get a TCP/IP book and a data structures book here pretty soon, maybe for my b-day! whoop whoop.
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,198
  • Joined: 10-May 07

Re: Play music using C++ and console app

Posted 30 May 2009 - 11:21 PM

View PostIngeniousHax, on 31 May, 2009 - 12:02 AM, said:

Yeah I know...But it's about the closest i'm gonna get for awhile


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

int main(void) {
  char ch, file_buffer[256];
  FILE *fp;
  FILE *wav;

  fp=fopen("/dev/audio");
  if(!fp) {
	printf("check whoami, run this with sudo or as root!\n");
	exit(0);
  }
  printf("Which file would you like to output to the audio device?\n");
  scanf("%s",file_buffer);
  wav=fopen("prompt for wav file");
  if(!wav) {
	printf("That file does not exist, please check the given path.\n");
	exit(0);
  }

  // Let it rip!
  while(!feof(wav)) {
	ch=fgetc(wav);
	fputc(ch,fp);
	printf("Dumping to audio...\n");
  }

  fclose(fp);
  fclose(wav);
  return 0;
}



This was tested, but on another system. & just for shits-n-giggles... it doesn't have to be a wav file. Binary makes wonderful handshake style noise over dev audio :)
Was This Post Helpful? 0
  • +
  • -

#13 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Play music using C++ and console app

Posted 30 May 2009 - 11:29 PM

Damn right, ill try it when I finish Downloading somethin, and am able to switch back to Ubuntu. Thanks a lot No2Pencil.
Was This Post Helpful? 0
  • +
  • -

#14 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,198
  • Joined: 10-May 07

Re: Play music using C++ and console app

Posted 30 May 2009 - 11:35 PM

It's cheezie++, but it'll give you an effect. Better than nothing I suppose.
Was This Post Helpful? 0
  • +
  • -

#15 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Play music using C++ and console app

Posted 31 May 2009 - 12:09 AM

Its like getting a dollar tip, it's more than ya had before.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1