Author Topic: FF7 PSX sound effects format  (Read 16176 times)

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
FF7 PSX sound effects format
« on: 2018-04-16 03:20:36 »
UPDATE : the format is mostly understood at this point, check the work here : http://sdamo.io/ff7/index.php


Hello

This always puzzled me, the PC version using hundred of megabytes worth of wave files for sound effects, while the PSX version only used a tiny 50Kb file (EFFECT.ALL).
After some searches, there seems to be nobody interested into reverse engineering this, please correct me if I'm wrong ?

I just did an attempt this evening, to get a better understaning of this format. I'm interested in sound synthesis, in fact I'm the developer of a music software called FM Composer.

So, here's my findings.


The data from byte 4 to 2829 is an offset table for each individual sounds. Each entry is 2-byte long. Some entries are "0xFFFF", I don't know why, let's say we ignore them. After byte #2829 there is an empty block made of 0xFF bytes then the actual sound data. Following this table I can extract 936 sounds from the file.
You can extract them with this little code I made, you need to put EFFECT.ALL in the same dir and create an 'out' folder. Compiled version here : http://sdamo.io/ff7sfx.zip
Or all the sounds directly extracted here : http://sdamo.io/ff7/out.zip
Code: [Select]
int main()
{
FILE *fp=fopen("EFFECT.ALL", "rb");
fseek(fp,8,SEEK_SET);
vector<int> offsets;

offsets.push_back(0);
unsigned short temp;

printf("Analyzing...\n");

int endcount=0;
do
{
fread((char*)&temp,2,1,fp);

if (temp == 0xFFFF) {
endcount++;
continue;
}
else
endcount=0;

offsets.push_back(temp);

}while (endcount<3);

printf("%d sounds found.\n",offsets.size());
printf("Extracting...\n");

fseek(fp,4096,SEEK_SET);

char *sound_data;

for (unsigned i = 0; i < offsets.size()-1; i++)
{

int size=offsets[i+1]-offsets[i];
if (size<=0)
continue;


sound_data = (char*)malloc(size);

fread(sound_data,size,1,fp);

FILE *fp2=fopen(string("out/"+to_string(i)).c_str(), "wb");
fwrite(sound_data, size, 1,fp2);
fclose(fp2);
free(sound_data);

}

fclose(fp);
printf("Done.\n");
}


All sounds seems to finish with an '0xA0' byte.

Okay it's nice but the hardest part is to figure how the format works. I've messed with the first sound of the file (which is the menu beep sound), by modifying random bytes directly in the ISO image, then restarting the game with an emulator, and yeah I've got more or less interesting results !

Check this : http://sdamo.io/ff7/sounds.zip It's the altered beep sounds, depending on which bytes i modified (naming : chk-[bytes modified])

Some results are really interesting, with some delays, pitch or noises...

Anyone interested in helping me to find how those parameters works ? (basically, which bytes/values affects which sound parameter).
« Last Edit: 2018-05-24 09:22:00 by Phanoo »

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: FF7 PSX sound effects format
« Reply #1 on: 2018-04-16 16:18:02 »
I am very interested in this....  Been waiting for someone to tackle it, as I am sadly at a loss.  No idea where to start with it.  But kudos for you delving in.

It's amazing that after all these years the effects data file in psx version is so poorly understood.


Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #2 on: 2018-04-16 17:12:01 »
Yes, i'm really surprised nobody wrote some tools to deal with them.

Some more investigation with the menu beep sound, here's its contents :
Code: [Select]
A1 01 A5 05 A4 05 0F B1 2C 81 A0 A1 00 A5 06 B1 2E A4 08 0C 08 A0
As I said, A0 is the end marker for each sound, but this one has an A0 right in the middle. It means the final sound is made of two sounds layered (played at the same time) :
EDIT : I was wrong, there is no layering thing in the sounds itself. The menu beep is just two independent sounds, that are triggered at the same time.

Code: [Select]
A1 01 A5 05 A4 05 0F B1 2C 81 A0... creates this sound : http://sdamo.io/ff7/beep_1st.wav

Code: [Select]
A1 00 A5 06 B1 2E A4 08 0C 08 A0... creates this sound : http://sdamo.io/ff7/beep_2nd.wav

About this A0 byte, it seems to follow the opcodes discovered here for the AKAO frames : http://archive.is/0PE01

The page lists other opcodes, which may help :
Code: [Select]
0xA0 (Finish Channel)
0xA1 (Load Instrument)
0xA3 (Volume Modifier)
0xA5 (Pitch Divider)
0xA8 (Channel Volume)
0xAA (Channel Pan)
0xC8 (Loop Point)
0xCA (Return to Loop Point)
0xE8 (Tempo)
0xEA (Reverb Depth)
0xC2 (Turn On Reverb)

Maybe the SFX engine and music engine share the same code, the SFX simply having more advanced opcodes for doing complex time-based things and modulations.
« Last Edit: 2018-04-23 14:58:46 by Phanoo »

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: FF7 PSX sound effects format
« Reply #3 on: 2018-04-16 17:19:51 »
the problem ishow do you faithfully recreate the sounds from that alone. the psx sound chip needs emulating too?

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #4 on: 2018-04-16 17:29:39 »
ATM i'm directly replacing the hex contents in my FF7 iso image, then run the game from an emulator (ePSXe) so I can hear how my changes affects the sound.

If we manage to guess the whole format, I dunno how hard it would be to create some rough emulation so a software could play the sounds. In fact i didnt thought about emulation, more like a little sound engine written from scratch (not sounding perfect but just to give an idea), with an editor allowing to have fun with parameters and a save feature. I know nothing about emulators, but i'm not bad at dealing with MIDI, sound synthesis and such things
« Last Edit: 2018-04-16 17:33:44 by Phanoo »

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #5 on: 2018-04-16 21:19:17 »
Mappings for the A1 xx command which is the waveform select  :)

Some of them points to INSTR.ALL samples while some others appears to be synthsized, or stored elsewhere. I've recorded those 'custom' waveforms as wave files. Samples from INSTRL.ALL can be extracted with PSound !

Code: [Select]
A1 00 => sine
A1 01 => bright_sine
A1 02 => pulse02.wav
A1 03 => pulse02-3.wav
A1 04 => INSTR.ALL_0000
A1 05 => INSTR.ALL_0001
A1 06 => INSTR.ALL_0002
A1 07 => INSTR.ALL_0003
A1 08 => pulse5.wav
A1 09 => INSTR.ALL_0004
A1 0A => INSTR.ALL_0005
A1 0B => same as 02
A1 0C => pulse3
A1 0D => pulse4
A1 0E => INSTR.ALL_0006
A1 0F => INSTR.ALL_0007
A1 10 => INSTR.ALL_0008
A1 11 => INSTR.ALL_0009
A1 12 => INSTR.ALL_0010
A1 13 => smooth_square.wav
A1 14 => smooth_saw.wav
A1 15 => smooth_triangle.wav
A1 16 => smooth_pulse.wav
A1 17 => INSTR.ALL_0011
A1 18 => INSTR.ALL_0012
A1 19 => INSTR.ALL_0013

Values greater than 19 doesn't seems to exist in the file, although i didnt checked them all.
« Last Edit: 2018-04-16 21:26:30 by Phanoo »

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: FF7 PSX sound effects format
« Reply #6 on: 2018-04-16 21:37:15 »
This is impressive work :)   I can't remember if PSX was able to synth sounds...  I bet it could do simple sine etc

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #7 on: 2018-04-18 19:02:23 »
Ok the format is complex, it's a bit inspired from MIDI since I discovered there are Running Status commands. That means, if you choose the command 0xB1 for example, you can put after as much parameters as you want until you put another command byte.

Eg : B1 52 34 65 02 31.... will play some notes with the select waveform.

It's a nice thing to save space.

The VERY annoying thing is that several parameters are merged into ONE byte. A single byte after the B1 command is enough to set the pitch, the duration and the release rate. It makes the thing quite annoying to reverse engineer  ;D

For the octave selection (A5 command), it has some annoying things too. When you reach a high pitch the next value isn't making it higher again, instead it starts from a lower note, and the next values produce other various pitches. I think maybe the note frequency is bit-shifted or something, making strange results at some point.

I may not success at writing a program that replays and allow to edit the sounds from the PSX game. But i'll release a document with my findings anyway.

The guy who developed this system can be proud of him, it's very clever work. I wonder how was the editor that Square used to design all the sounds.
« Last Edit: 2018-04-18 19:06:35 by Phanoo »

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: FF7 PSX sound effects format
« Reply #8 on: 2018-04-18 22:16:14 »
That's the million dollar question....  how did they create these sounds.  I mean... is it possible they had something that could convert a real sound of a fire into code? Akao created this sound system and he clearly intended to make it as small as possible because his experience was with small cartridges and not CD.  In early ff7 days it was going to be on N64.

It makes sense now that he's used every bit.

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #9 on: 2018-04-18 23:40:26 »
Yes it clearly seems designed for a low memory system, such optimization makes no sense for a CD-Rom game. I guess they re-used some code they made for the SNES.

I don't think they had a tool converting real sounds into synthesized ones, it's a very, very complicated task. The pitch variations are often complex in the sounds, maybe they had a way to draw the lines that get converted into the appropriate commands..

luksy

  • *
  • Posts: 375
    • View Profile
Re: FF7 PSX sound effects format
« Reply #10 on: 2018-04-19 19:42:10 »
The SNES SPU was manufactured by Sony, in fact the PS1 and SNES SPUs are quite similar so I'm guessing Akao just ported a lot of his existing code straight over.

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #11 on: 2018-04-21 13:24:53 »
Work in progress  :)



It can already play some sounds, although most of them are garbage since not all commands are understood at this point.

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #12 on: 2018-04-22 13:37:31 »
I was wondering why most of the noise-based sounds in my engine didn't sounded like the original ones, it's because FF7 sound engine is able to use the noise generator from the sound chip, instead of simply using the sampled noise-like waveform in INSTR.ALL. The one from the sound chip has a very low-fi sound to it, because it's 4-bit resolution (=16 steps) and the frequency only changes the rate at which the new random value is generated. Fortunately it's easy to emulate, using the standard C rand() function creates identical sounds !

I've also added the support for LFO (low frequency oscillator), it's triggered with the B4 command, and is quite complex because it uses several patterns.. They are all 32-steps lengths, i've implemented the first seven of them. Magic sounds makes heavy use of them !

The first version of the sfx player will be soon available... it's not accurate but already quite fun.

There are still commands I don't understand at all. All the Cx commands. They seems to be related to some looping or re-triggering of other commands. Weird.



Green highlighted parts are the understood commands :) (more or less accurately since the engine is written from scratch from my guessings, it's not an emulation)


EDIT : found what C8 and C9 means :)  C9 jumps to C8 like a loop point. The number of time it repeats depends on the parameter just after C9 !
« Last Edit: 2018-04-22 18:13:16 by Phanoo »

luksy

  • *
  • Posts: 375
    • View Profile
Re: FF7 PSX sound effects format
« Reply #13 on: 2018-04-22 19:30:21 »
This is some cool stuff, looking forward to seeing (& hearing) the player!

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #14 on: 2018-04-25 11:51:04 »
Thanks, i'll release the first prototype soon !

Please guys give me some motivation to keep working on this :) It has been very fun to guess most of the format but now it's taking a lot of time to get it right (especially, reverse engineering all the LFO patterns, frequency ramps and such is exhausting). Would you be interested in a program allowing to modify the sounds and re-exporting the EFFECT.ALL file ?
« Last Edit: 2018-04-25 11:53:24 by Phanoo »

Kaldarasha

  • *
  • Posts: 2449
  • Prince of Model Editing
    • View Profile
Re: FF7 PSX sound effects format
« Reply #15 on: 2018-04-25 12:22:09 »
It would be interesting to see, if we can implement your tool into the game to play the sound. It is also interesting for FF9. And I'm pretty sure FF8 has also problems with playing the correct sound effects even though nobody has complained about it yet.

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #16 on: 2018-04-25 12:32:53 »
I could look at FF8/FF9 sound data to see if the format is compatible, but the files are packed into a single .IMG file. Is there a way to extract the separate files, modify them, and rebuild the whole thing ?
At least I'd need an offset to look at, because SFX data doesn't have any magic string that helps identifying it

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: FF7 PSX sound effects format
« Reply #17 on: 2018-04-25 16:46:30 »
I'm really looking forward to seeing even a semi working program that can decipher the audio

luksy

  • *
  • Posts: 375
    • View Profile
Re: FF7 PSX sound effects format
« Reply #18 on: 2018-04-25 20:08:11 »
Thanks, i'll release the first prototype soon !

Please guys give me some motivation to keep working on this :) It has been very fun to guess most of the format but now it's taking a lot of time to get it right (especially, reverse engineering all the LFO patterns, frequency ramps and such is exhausting). Would you be interested in a program allowing to modify the sounds and re-exporting the EFFECT.ALL file ?

Being able to export the pure, unadulterated ps1 effects would be great, I'm not sure if you're aware but the PCM sfx used in the PC version are often terrible. DLPB has been working on replacing them but I can't remember how far he got.

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: FF7 PSX sound effects format
« Reply #19 on: 2018-04-25 20:28:43 »
I've recorded all of them from Mednafen... and looped any that need looping.  The audio replacement mod is done.  But Aali's driver code - and not having direct mode set up with my new mod method - is holding Reunion up.

I want to make The Reunion integrated properly  with direct file mode.  Since Aali has done the leg work... it should be fairly simple for me to change his code.  I'm going to have a go.

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #20 on: 2018-04-26 01:31:17 »
Here is the current version : http://sdamo.io/ff7/ff7psxsfx.zip

There is still a lot of inaccuracies in the playback, don't mind  :D



You get a list of the commands being played, and the nice thing is I managed to read the samples directly from INSTR.ALL and INSTR.DAT. I've found that even the simpliest waveforms are sampled. PSound didn't extracted them when I scanned INSTR.ALL, that's why I initially thought those waveform were generated. Probably they were too short to trigger its detection system.

Kaldarasha

  • *
  • Posts: 2449
  • Prince of Model Editing
    • View Profile
Re: FF7 PSX sound effects format
« Reply #21 on: 2018-04-26 04:11:44 »
C2 could be the pan or the marker if it is a stereo or mono effect. At last I can't see in the list that this is handled somewhere.

Phanoo

  • *
  • Posts: 35
  • Musician & Programmer
    • View Profile
    • FM Composer
Re: FF7 PSX sound effects format
« Reply #22 on: 2018-04-26 08:54:27 »
From what I understood, all sounds are mono, but the engine can play two of them at the same time, achieving a stereo effect. The sound themselves don't seem to know about their panning

Some currently unknown commands :
C2, C6, B6, BA, D0, D1, DA, DC, DD, DF
Most of them seems to have no parameters, I even tried to remove C2 from some sounds and it didn't make any hearable difference  :?

If you want to try things with the sounds you can edit the FF7 disc1 iso, set the bytes from 0x1066c8 to 0x1066d2 to 0xA0, and write the sound data you want at 0x1066d3 (no size limit). It will play your sound when you move the cursor on the title screen. To do fast edits/tests, make a savestate before the Squaresoft logo shows up (it loads the sound data here), and toggle FPS limit with F4 (on ePSXe) to go faster.

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: FF7 PSX sound effects format
« Reply #23 on: 2018-04-26 12:20:56 »
i have a good audio setup. if u want to run by me 2 versions with different codes i can tell you if i hear a difference

DLPB_

  • Banned
  • *
  • Posts: 11006
    • View Profile
Re: FF7 PSX sound effects format
« Reply #24 on: 2018-04-26 22:25:52 »
This is seriously impressive....  you can make out exactly what sounds they are.  It's taken over 20 years for someone to tackle it.

Kudos!

Should be noted that some effects should loop indefinitely and perhaps thats what one of the missing opcodes does?
« Last Edit: 2018-04-26 22:39:10 by DLPB »