Windows mame has a -wavwrite option to record the sound output directly to a WAv file. details in the mame docs.
Note it records all the bios stuff (usually no sound) too so you will probably have to trim it. This function is used in the mame movie maker.