Skip to content

Instantly share code, notes, and snippets.

@sfan5
Created March 14, 2015 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfan5/8bbde9a0324d3a64d299 to your computer and use it in GitHub Desktop.
Save sfan5/8bbde9a0324d3a64d299 to your computer and use it in GitHub Desktop.
Sound recording directly from DeSmuME (w/o any UI, it just does it)
--- desmume-0.9.10/src/sndsdl.cpp.orig 2013-11-28 01:37:18.797021001 +0100
+++ desmume-0.9.10/src/sndsdl.cpp 2015-03-14 22:24:04.184578448 +0100
@@ -16,6 +16,7 @@
along with the this software. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -57,6 +58,8 @@
static u32 soundlen;
static u32 soundbufsize;
static SDL_AudioSpec audiofmt;
+static FILE *aofile;
+static u32 aofile_size;
//////////////////////////////////////////////////////////////////////////////
#ifdef _XBOX
@@ -137,6 +140,43 @@
CreateThread(0,0,SNDXBOXThread,0,0,0);
#endif
+ aofile = fopen("desmume.wav", "wb");
+ if(!aofile)
+ return -1;
+ struct {
+ char magic_riff[4];
+ u32 filesize;
+ char magic_wave[4];
+ char fmt[4];
+ u32 fmt_len;
+ u16 datafmt;
+ u16 channels;
+ u32 samplerate;
+ u32 bytespersec;
+ u16 blockalign;
+ u16 bitspersample;
+ char data[4];
+ u32 data_len;
+ } wavhdr;
+#define ASSIGN_CHAR4(var, val) *((u32*) (var)) = *((u32*) (val));
+ ASSIGN_CHAR4(wavhdr.magic_riff, "RIFF");
+ wavhdr.filesize = 0xFFFFFFFF; // set in SNDSDLDeInit()
+ ASSIGN_CHAR4(wavhdr.magic_wave, "WAVE");
+ ASSIGN_CHAR4(wavhdr.fmt, "fmt ");
+ wavhdr.fmt_len = 16;
+ wavhdr.datafmt = 0x0001; // PCM
+ wavhdr.channels = 2;
+ wavhdr.samplerate = 44100;
+ wavhdr.bytespersec = 44100 * 4; // sample rate * blockalign
+ wavhdr.blockalign = 4; // channels * (int) ((bitspersample + 7) / 8)
+ wavhdr.bitspersample = 16;
+ ASSIGN_CHAR4(wavhdr.data, "data");
+ wavhdr.data_len = 0xFFFFFFFF; // set in SNDSDLDeInit()
+#undef ASSIGN_CHAR4
+ if(fwrite(&wavhdr, sizeof(wavhdr), 1, aofile) < 1)
+ return -1;
+ aofile_size = sizeof(wavhdr);
+
return 0;
}
@@ -154,6 +194,17 @@
if (stereodata16)
free(stereodata16);
+
+ fflush(aofile);
+ u32 tmp;
+ // Write correct file sizes
+ fseek(aofile, 0x04L, SEEK_SET);
+ tmp = aofile_size - 8;
+ fwrite(&tmp, 4, 1, aofile);
+ fseek(aofile, 0x28L, SEEK_SET);
+ tmp = aofile_size - 44;
+ fwrite(&tmp, 4, 1, aofile);
+ fclose(aofile);
}
//////////////////////////////////////////////////////////////////////////////
@@ -184,6 +235,9 @@
soundoffset += copy1size + copy2size;
soundoffset %= soundbufsize;
+ fwrite(buffer, num_samples * sizeof(s16) * 2, 1, aofile);
+ aofile_size += num_samples * sizeof(s16) * 2;
+
SDL_UnlockAudio();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment