File: samples.cpp

package info (click to toggle)
nestopia 1.49-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,424 kB
  • sloc: cpp: 86,935; xml: 26,956; makefile: 1,515; ansic: 772; sh: 19
file content (151 lines) | stat: -rw-r--r-- 4,446 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Nestopia UE
 * 
 * Copyright (C) 2012-2018 R. Danbrook
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#ifndef _MINGW
#include <archive.h>
#include <archive_entry.h>
#endif

#include "nstcommon.h"
#include "samples.h"

static uint8_t* wavfile;

extern nstpaths_t nstpaths;

int nst_sample_load_file(const char* filepath) {
	// Load a sample file into memory
	FILE *file;
	long filesize; // File size in bytes
	size_t result;
	
	file = fopen(filepath, "rb");
	
	if (!file) { return 0; }
	
	fseek(file, 0, SEEK_END);
	filesize = ftell(file);
	fseek(file, 0, SEEK_SET);
	
	wavfile = (uint8_t*)malloc(filesize * sizeof(uint8_t));
	
	if (wavfile == NULL) { return 0; }
	
	result = fread(wavfile, sizeof(uint8_t), filesize, file);
	if (result != filesize) { return 0; }
	
	fclose(file);
	return 1;
}

int nst_sample_load_archive(const char* filename, const char* reqfile) {
#ifndef _MINGW
	struct archive *a;
	struct archive_entry *entry;
	int r;
	int64_t entrysize;
	
	a = archive_read_new();
	archive_read_support_filter_all(a);
	archive_read_support_format_all(a);
	r = archive_read_open_filename(a, filename, 10240);
	
	// Test if it's actually an archive
	if (r != ARCHIVE_OK) {
		r = archive_read_free(a);
		return 0;
	}
	
	// Scan through the archive for files
	while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
		const char *currentfile = archive_entry_pathname(entry);
		int len = strlen(currentfile);
		if ((!strcasecmp(&currentfile[len-4], ".wav"))) {
			// Load the specified file
			if (!strcmp(currentfile, reqfile)) {
				entrysize = archive_entry_size(entry);
				wavfile = (uint8_t*)malloc(entrysize);
				archive_read_data(a, wavfile, entrysize);
				archive_read_data_skip(a);
				r = archive_read_free(a);
				return 1;
			}
		}
	}
#endif
	return 0;
}

int nst_sample_unload_file() {
	// Free the file
	if (wavfile) { free(wavfile); }
	return 1;
}

void nst_sample_setcontent(User::File& file) {
	// Parse the .WAV header and load the sample into the emulator
	
	// Check to see if it has a valid header
	uint8_t fmt[4] = { 0x66, 0x6d, 0x74, 0x20};
	uint8_t subchunk2id[4] = { 0x64, 0x61, 0x74, 0x61};
	if (memcmp(&wavfile[0x00], "RIFF", 4) != 0) { return; }
	if (memcmp(&wavfile[0x08], "WAVE", 4) != 0) { return; }
	if (memcmp(&wavfile[0x0c], &fmt, 4) != 0) { return; }
	if (memcmp(&wavfile[0x24], &subchunk2id, 4) != 0) { return; }
	
	// Load the sample into the emulator
	uint8_t *dataptr = &wavfile[0x2c];
	uint32_t datasize = wavfile[0x2b] << 24 | wavfile[0x2a] << 16 | wavfile[0x29] << 8 | wavfile[0x28];
	uint16_t blockalign = wavfile[0x21] << 8 | wavfile[0x20];
	uint16_t numchannels = wavfile[0x17] << 8 | wavfile[0x16];
	uint16_t bitspersample = wavfile[0x23] << 8 | wavfile[0x22];
	uint32_t samplerate = wavfile[0x1b] << 24 | wavfile[0x1a] << 16 | wavfile[0x19] << 8 | wavfile[0x18];
	file.SetSampleContent(dataptr, datasize / blockalign, numchannels == 2, bitspersample, samplerate);
}

void nst_sample_load_samples(User::File& file, const char* sampgame) {
	// Load samples for the specific game
	char reqfile[16];
	char samppath[576];
	
	// Requested sample .wav file
	snprintf(reqfile, sizeof(reqfile), "%02d.wav", file.GetId());
	
	// Check if there's a MAME-style zip archive
	snprintf(samppath, sizeof(samppath), "%s%s.zip", nstpaths.sampdir, sampgame);
	if (nst_sample_load_archive(samppath, reqfile)) {
		nst_sample_setcontent(file);
		nst_sample_unload_file();
	}
	else { // Otherwise load .wav files from an extracted directory
		snprintf(samppath, sizeof(samppath), "%s%s/%s", nstpaths.sampdir, sampgame, reqfile);
		if (nst_sample_load_file(samppath)) {
			nst_sample_setcontent(file);
			nst_sample_unload_file();
		}
	}
}