mirror of
https://github.com/ScrelliCopter/VGM-Tools
synced 2025-02-21 04:09:25 +11:00
Refactor most of the opening and scanning between sources
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "neoadpcmextract.h"
|
#include "neoadpcmextract.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -12,6 +13,7 @@ int main(int argc, char** argv)
|
|||||||
if (!file)
|
if (!file)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
// Error on VGZ's for now.
|
||||||
if (fgetc(file) == 0x1F && fgetc(file) == 0x8B)
|
if (fgetc(file) == 0x1F && fgetc(file) == 0x8B)
|
||||||
{
|
{
|
||||||
printf("I'm a little gzip short and stout\n");
|
printf("I'm a little gzip short and stout\n");
|
||||||
@@ -20,7 +22,42 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
int err = vgmExtractSamples(file);
|
Buffer smpbuf = {NULL, 0, 0};
|
||||||
fclose(file);
|
char name[32];
|
||||||
return err;
|
int smpaCount = 0, smpbCount = 0;
|
||||||
|
|
||||||
|
// Find ADCPM samples.
|
||||||
|
int scanType;
|
||||||
|
while ((scanType = vgmScanSample(file)))
|
||||||
|
{
|
||||||
|
if (scanType != 'A' && scanType != 'B')
|
||||||
|
continue;
|
||||||
|
fprintf(stderr, "ADPCM-%c data found at 0x%08lX\n", scanType, ftell(file));
|
||||||
|
|
||||||
|
if (vgmReadSample(file, &smpbuf) || smpbuf.size == 0)
|
||||||
|
continue;
|
||||||
|
if (scanType == 'A')
|
||||||
|
{
|
||||||
|
// decode
|
||||||
|
snprintf(name, sizeof(name), "smpa_%02x.pcm", smpaCount++);
|
||||||
|
printf("./adpcm \"%s\" \"$WAVDIR/%s.wav\"\n", name, name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// decode
|
||||||
|
snprintf(name, sizeof(name), "smpb_%02x.pcm", smpbCount++);
|
||||||
|
printf("./adpcmb -d \"%s\" \"$WAVDIR/%s.wav\"\n", name, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write adpcm sample.
|
||||||
|
FILE* fout = fopen(name, "wb");
|
||||||
|
if (!fout)
|
||||||
|
continue;
|
||||||
|
fwrite(smpbuf.data, sizeof(uint8_t), smpbuf.size, fout);
|
||||||
|
fclose(fout);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(smpbuf.data);
|
||||||
|
fclose(file);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/* neoadpcmextract.cpp
|
/* neoadpcmextract.c
|
||||||
Copyright (C) 2017, 2019 Nicholas Curtis
|
Copyright (C) 2017, 2019, 2020 Nicholas Curtis
|
||||||
|
|
||||||
This software is provided 'as-is', without any express or implied
|
This software is provided 'as-is', without any express or implied
|
||||||
warranty. In no event will the authors be held liable for any damages
|
warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -18,14 +18,11 @@
|
|||||||
3. This notice may not be removed or altered from any source distribution.
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include "neoadpcmextract.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct { uint8_t* data; size_t size; } Buffer;
|
int vgmReadSample(FILE* fin, Buffer* buf)
|
||||||
|
|
||||||
int DecodeSample(FILE* fin, const char* name, Buffer* buf)
|
|
||||||
{
|
{
|
||||||
// Get sample data length.
|
// Get sample data length.
|
||||||
uint32_t sampLen = 0;
|
uint32_t sampLen = 0;
|
||||||
@@ -35,10 +32,11 @@ int DecodeSample(FILE* fin, const char* name, Buffer* buf)
|
|||||||
sampLen -= sizeof(uint64_t);
|
sampLen -= sizeof(uint64_t);
|
||||||
|
|
||||||
// Resize buffer if needed.
|
// Resize buffer if needed.
|
||||||
if (!buf->data || buf->size < sampLen)
|
buf->size = sampLen;
|
||||||
|
if (!buf->data || buf->reserved < sampLen)
|
||||||
{
|
{
|
||||||
free(buf->data);
|
free(buf->data);
|
||||||
buf->size = sampLen;
|
buf->reserved = sampLen;
|
||||||
buf->data = malloc(sampLen);
|
buf->data = malloc(sampLen);
|
||||||
if (!buf->data)
|
if (!buf->data)
|
||||||
return 1;
|
return 1;
|
||||||
@@ -51,25 +49,17 @@ int DecodeSample(FILE* fin, const char* name, Buffer* buf)
|
|||||||
// Read adpcm data.
|
// Read adpcm data.
|
||||||
fread(buf->data, sizeof(uint8_t), sampLen, fin);
|
fread(buf->data, sizeof(uint8_t), sampLen, fin);
|
||||||
|
|
||||||
// Write adpcm sample.
|
|
||||||
FILE* fout = fopen(name, "wb");
|
|
||||||
if (!fout)
|
|
||||||
return 1;
|
|
||||||
fwrite(buf->data, sizeof(uint8_t), sampLen, fout);
|
|
||||||
fclose(fout);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int vgmExtractSamples(FILE* file)
|
int vgmScanSample(FILE* file)
|
||||||
{
|
{
|
||||||
Buffer smpBytes = {NULL, 0};
|
|
||||||
char namebuf[32];
|
|
||||||
int smpaCount = 0, smpbCount = 0;
|
|
||||||
|
|
||||||
// Scan for pcm headers.
|
// Scan for pcm headers.
|
||||||
while (!feof(file) && !ferror(file))
|
while (1)
|
||||||
{
|
{
|
||||||
|
if (feof(file) || ferror(file))
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Patterns to match (in hex):
|
// Patterns to match (in hex):
|
||||||
// 67 66 82 - ADPCM-A
|
// 67 66 82 - ADPCM-A
|
||||||
// 67 66 83 - ADPCM-B
|
// 67 66 83 - ADPCM-B
|
||||||
@@ -78,21 +68,8 @@ int vgmExtractSamples(FILE* file)
|
|||||||
|
|
||||||
uint8_t byte = fgetc(file);
|
uint8_t byte = fgetc(file);
|
||||||
if (byte == 0x82)
|
if (byte == 0x82)
|
||||||
{
|
return 'A';
|
||||||
printf("ADPCM-A data found at 0x%08lX\n", ftell(file));
|
|
||||||
snprintf(namebuf, sizeof(namebuf), "smpa_%x.pcm", smpaCount++);
|
|
||||||
if (DecodeSample(file, namebuf, &smpBytes))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (byte == 0x83)
|
else if (byte == 0x83)
|
||||||
{
|
return 'B';
|
||||||
printf("ADPCM-B data found at 0x%08lX\n", ftell(file));
|
|
||||||
snprintf(namebuf, sizeof(namebuf), "smpb_%x.pcm", smpbCount++);
|
|
||||||
if (DecodeSample(file, namebuf, &smpBytes))
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(smpBytes.data);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,7 +2,11 @@
|
|||||||
#define __NEOADPCMEXTRACT_H__
|
#define __NEOADPCMEXTRACT_H__
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
int vgmExtractSamples(FILE* file);
|
typedef struct { uint8_t* data; size_t size, reserved; } Buffer;
|
||||||
|
|
||||||
|
int vgmReadSample(FILE* fin, Buffer* buf);
|
||||||
|
int vgmScanSample(FILE* file);
|
||||||
|
|
||||||
#endif//__NEOADPCMEXTRACT_H__
|
#endif//__NEOADPCMEXTRACT_H__
|
||||||
|
|||||||
Reference in New Issue
Block a user