Prince of Persia PC Speaker Music

Prince of Persia related subjects that do not have their own boards.
Post Reply
Nico
Efendi
Efendi
Posts: 15
Joined: January 26th, 2009, 3:11 am

Prince of Persia PC Speaker Music

Post by Nico »

Hi! Does anyone know if it's possible to make a program to play the PC speaker sounds of POP 1?

I have this information about the format which music is stored in the files IBM_SND1.DAT and IBM_SND2.DAT but I don't understand it... perhaps someone can help me with it.

3.7. Internal PC Speaker
Header: 3 bytes
First byte: 0x00 (or 0x80 sometimes).
Next 2 bytes: how many beats per 2 seconds.

Body: numberOfNotes*3
Then, 3 bytes for each note:
- 2 bytes: US for frequency in hertz (0 if no sound, 1 or 2 if marker).
- 1 byte: UC for length in beats.

Footer: 2 bytes
Last 2 bytes: 0x12 0x00.

I'll appreciate any help

Bye!
Nico
Efendi
Efendi
Posts: 15
Joined: January 26th, 2009, 3:11 am

Re: Prince of Persia PC Speaker Music

Post by Nico »

Hi!

I'd like to know how does the PC sound coding works.

If you can make a program to read the music from these files I'll be great! can u write it in C and can u attach the source code so that I can see how it works?

Thanks for your reply, bye!
Nico
Efendi
Efendi
Posts: 15
Joined: January 26th, 2009, 3:11 am

Re: Prince of Persia PC Speaker Music

Post by Nico »

Thanks for the code, I modified one of your codes to compile it with Borland C++ 3.1.

Here is the code:

Code: Select all

// Compilar con Borland C++ 3.1
#include <iostream.h>
#include <math.h>
#include <stdio.h>
#include <dos.h>

int freq2note ( int freq );

int main ( int argc, char* argv[] )
{
	 cout << "POP PC speaker format parser" << endl << endl;

	 if ( argc != 2 && argc != 3 )
	 {
		  cout << "Usage" << endl;
		  cout << "  " << argv[0] << " filename.pcs" << endl;
		  return 0;
	 }

	 int tempoMultiply = 1;  // change this to affect tempo, bigger values - slower

	 FILE * inputF;
	 inputF = fopen ( argv[1], "r" );

	 if ( inputF == NULL )
	 {
		  cout << "  ERROR: Could not load " << argv[1] << " for input" << endl;
		  return 0;
	 }

	 int tempo;
	 unsigned short a, b, c;

	 // Read tempo: two bytes in little-endian
	 a = fgetc ( inputF );
	 b = fgetc ( inputF );
	 tempo = a + 256*b;

	 cout << "  File opened OK." << endl;
	 cout << "  Tempo: " << tempo << " ticks per two seconds" << endl;

	 int freq, length;

	 delay(256);

	 for(;;) // infinite loop
	 {
		  // Read two bytes from file
		  a = fgetc ( inputF );
		  b = fgetc ( inputF );

		  // If those bytes are 0x12 and 0x00, it's end of file - so break the loop, otherwise it's frequency
		  if ( a == 0x12 && b == 0x00 ) break;

		  // Looks like it's a note. So read the third byte - note length
		  c = fgetc ( inputF );
		  length = c;

		  // Calculate the frequency
		  freq = a + 256*b;

		  // Now we have frequency (int hertz) in int freq
		  // and length (in ticks) in int length.
		  if ( freq == 0 )
		  {
				cout << "  Rest, duration: " << length << " ticks = " << ((float)length/(float)tempo) << " sec." << endl;
				delay( (int)floor((float)length*1000/(float)tempo) * tempoMultiply );
        }
        else
        {
				cout << "  Frequency: " << freq << " Hz (MIDI: " <<
					freq2note ( freq ) << "), duration: " << length <<
					" ticks = " << ((float)length/(float)tempo) << " sec." << endl;
				sound(freq);
				delay((int)floor((float)length*1000/(float)tempo) * tempoMultiply );
        }
    }

    cout << "  End of file" << endl;

    fclose ( inputF );

    return 0;
}

int freq2note ( int freq )
{
    // This function converts frequencies in Hertz to MIDI notes
    // http://www.phys.unsw.edu.au/jw/graphics/notes.GIF - MIDI notes table

    float note;
    note = freq;
    note = 12 * log ( note / 440 ) / log ( 2 );
    note = floor ( note );
    note = 69 + note;
    return (int)note;
}
What are u using to get the PC Speaker format from the IBM_SND*.DAT files??

I'm using Prince Resource Manager V1.2 but it seems like it doesn't work very good to remove the sound files.
Nico
Efendi
Efendi
Posts: 15
Joined: January 26th, 2009, 3:11 am

Re: Prince of Persia PC Speaker Music

Post by Nico »

programmer wrote:I am using David's version of PR1.1 to export DAT files and it works very well.
Where can I download it?
MasterXploder
Efendi
Efendi
Posts: 5
Joined: March 22nd, 2009, 3:41 pm
Location: NOWHERE HAHAHA

Re: Prince of Persia PC Speaker Music

Post by MasterXploder »

pc spekaer suckz no use 4 it and teh princez music suckz 2
i want 2 make a mod thatz techno mod 8)
David
The Prince of Persia
The Prince of Persia
Posts: 2848
Joined: December 11th, 2008, 9:48 pm
Location: Hungary

Re: Prince of Persia PC Speaker Music

Post by David »

programmer wrote:There was a player some time ago. I can make one if you want to.
It was written by me, and you can download it here: (binary+source) http://www.freeweb.hu/princepersia/en/pcsplay_dl.htm
It's written in C, and runs under DOS.
Oh, and you need to extract the sounds with pr using the --raw option.
Post Reply