File: SampleVision.cpp

package info (click to toggle)
audiofile 0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,192 kB
  • sloc: cpp: 31,691; sh: 11,006; ansic: 3,773; makefile: 271
file content (313 lines) | stat: -rw-r--r-- 7,604 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
	Audio File Library
	Copyright (C) 2012, Michael Pruett <michael@68k.org>

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Library General Public
	License as published by the Free Software Foundation; either
	version 2 of the License, or (at your option) any later version.

	This library 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
	Library General Public License for more details.

	You should have received a copy of the GNU Library General Public
	License along with this library; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place - Suite 330,
	Boston, MA  02111-1307  USA.
*/

#include "config.h"
#include "SampleVision.h"

#include "File.h"
#include "Setup.h"
#include "Track.h"
#include "afinternal.h"
#include "util.h"
#include <string.h>

static const char *kSMPMagic = "SOUND SAMPLE DATA ";
static const unsigned kSMPMagicLength = 18;
static const char *kSMPVersion = "2.1 ";
static const unsigned kSMPVersionLength = 4;
static const unsigned kSMPNameLength = 30;
static const unsigned kSMPCommentLength = 60;
static const unsigned kSMPMarkerNameLength = 10;
static const uint32_t kSMPInvalidSamplePosition = 0xffffffffu;

static const uint8_t kSMPMIDIUnityPlaybackNote = 60;

static _AFfilesetup sSampleVisionDefaultFileSetup =
{
	_AF_VALID_FILESETUP,
	AF_FILE_SAMPLEVISION,
	true,	// trackSet
	true,	// instrumentSet
	true,	// miscellaneousSet
	1,		// trackCount
	NULL,	// tracks
	1,		// instrumentCount
	NULL,	// instruments
	0,		// miscellaneousCount
	NULL	// miscellaneous
};

static void trimTrailingSpaces(char *s)
{
	int n = strlen(s);
	if (!n)
		return;
	while (--n > 0 && s[n] == ' ')
		;
	s[n+1] = '\0';
}

SampleVisionFile::SampleVisionFile() :
	m_frameCountOffset(-1)
{
	setFormatByteOrder(AF_BYTEORDER_LITTLEENDIAN);
}

SampleVisionFile::~SampleVisionFile()
{
}

bool SampleVisionFile::recognize(File *fh)
{
	fh->seek(0, File::SeekFromBeginning);
	char magic[kSMPMagicLength];
	if (fh->read(magic, kSMPMagicLength) != kSMPMagicLength)
		return false;
	return !strncmp(magic, kSMPMagic, kSMPMagicLength);
}

AFfilesetup SampleVisionFile::completeSetup(AFfilesetup setup)
{
	if (setup->trackSet && setup->trackCount != 1)
	{
		_af_error(AF_BAD_NUMTRACKS, "SampleVision file must have 1 track");
		return AF_NULL_FILESETUP;
	}

	TrackSetup *track = &setup->tracks[0];
	if (track->sampleFormatSet)
	{
		if (!track->f.isSigned() || track->f.sampleWidth != 16)
		{
			_af_error(AF_BAD_SAMPFMT,
				"SampleVision format supports only 16-bit signed integer audio data");
			return AF_NULL_FILESETUP;
		}
	}
	else
		_af_set_sample_format(&track->f, AF_SAMPFMT_TWOSCOMP,
			track->f.sampleWidth);

	if (track->byteOrderSet && track->f.byteOrder != AF_BYTEORDER_LITTLEENDIAN)
	{
		// Treat error as correctable.
		_af_error(AF_BAD_BYTEORDER, "SampleVision supports only little-endian data");
	}

	track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN;

	return _af_filesetup_copy(setup, &sSampleVisionDefaultFileSetup, true);
}

status SampleVisionFile::readInit(AFfilesetup)
{
	fh->seek(0, File::SeekFromBeginning);

	char magic[kSMPMagicLength];
	if (fh->read(magic, kSMPMagicLength) != kSMPMagicLength)
		return AF_FAIL;
	if (strncmp(magic, kSMPMagic, kSMPMagicLength) != 0)
		return AF_FAIL;

	char version[kSMPVersionLength];
	if (fh->read(version, kSMPVersionLength) != kSMPVersionLength)
		return AF_FAIL;
	if (strncmp(version, kSMPVersion, kSMPVersionLength) != 0)
		return AF_FAIL;

	Track *track = allocateTrack();

	char name[kSMPNameLength + 1];
	fh->read(name, kSMPNameLength);
	name[kSMPNameLength] = '\0';
	trimTrailingSpaces(name);
	if (strlen(name) > 0)
		addMiscellaneous(AF_MISC_NAME, name);

	char comment[kSMPCommentLength + 1];
	fh->read(comment, kSMPCommentLength);
	comment[kSMPCommentLength] = '\0';
	trimTrailingSpaces(comment);
	if (strlen(comment) > 0)
		addMiscellaneous(AF_MISC_COMMENT, comment);

	uint32_t frameCount;
	readU32(&frameCount);
	track->totalfframes = frameCount;
	track->fpos_first_frame = fh->tell();
	track->data_size = 2 * frameCount;

	fh->seek(track->data_size, File::SeekFromCurrent);

	uint16_t reserved;
	readU16(&reserved);

	parseLoops();
	parseMarkers();

	uint8_t midiNote;
	uint32_t sampleRate;
	uint32_t smpteOffset;
	uint32_t cycleLength;

	readU8(&midiNote);
	readU32(&sampleRate);
	readU32(&smpteOffset);
	readU32(&cycleLength);

	_af_set_sample_format(&track->f, AF_SAMPFMT_TWOSCOMP, 16);
	track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN;
	track->f.sampleRate = sampleRate;
	track->f.channelCount = 1;
	track->f.compressionType = AF_COMPRESSION_NONE;

	return AF_SUCCEED;
}

status SampleVisionFile::parseLoops()
{
	for (int i=0; i<8; i++)
	{
		uint32_t startFrame, endFrame;
		uint8_t type;
		uint16_t count;
		readU32(&startFrame);
		readU32(&endFrame);
		readU8(&type);
		readU16(&count);
	}
	return AF_SUCCEED;
}

status SampleVisionFile::parseMarkers()
{
	for (int i=0; i<8; i++)
	{
		char name[kSMPMarkerNameLength + 1];
		fh->read(name, kSMPMarkerNameLength);
		name[kSMPMarkerNameLength] = '\0';
		uint32_t position;
		readU32(&position);
	}
	return AF_SUCCEED;
}

void SampleVisionFile::addMiscellaneous(int type, const char *data)
{
	miscellaneousCount++;
	miscellaneous = (Miscellaneous *) _af_realloc(miscellaneous,
		miscellaneousCount * sizeof (Miscellaneous));

	Miscellaneous &m = miscellaneous[miscellaneousCount - 1];
	m.id = miscellaneousCount;
	m.type = type;
	m.size = strlen(data);
	m.position = 0;
	m.buffer = _af_malloc(m.size);
	memcpy(m.buffer, data, m.size);
}

status SampleVisionFile::writeInit(AFfilesetup setup)
{
	if (_af_filesetup_make_handle(setup, this) == AF_FAIL)
		return AF_FAIL;

	fh->write(kSMPMagic, kSMPMagicLength);
	fh->write(kSMPVersion, kSMPVersionLength);

	char name[kSMPNameLength + 1];
	char comment[kSMPCommentLength + 1];
	memset(name, ' ', kSMPNameLength);
	memset(comment, ' ', kSMPCommentLength);
	fh->write(name, kSMPNameLength);
	fh->write(comment, kSMPCommentLength);

	uint32_t frameCount = 0;
	m_frameCountOffset = fh->tell();
	writeU32(&frameCount);

	Track *track = getTrack();
	track->fpos_first_frame = fh->tell();

	return AF_SUCCEED;
}

status SampleVisionFile::update()
{
	fh->seek(m_frameCountOffset, File::SeekFromBeginning);
	Track *track = getTrack();
	uint32_t frameCount = track->totalfframes;
	writeU32(&frameCount);
	writeTrailer();
	return AF_SUCCEED;
}

status SampleVisionFile::writeTrailer()
{
	Track *track = getTrack();

	fh->seek(track->fpos_after_data, File::SeekFromBeginning);

	uint16_t reserved = 0;
	writeU16(&reserved);

	writeLoops();
	writeMarkers();

	uint8_t midiNote = kSMPMIDIUnityPlaybackNote;
	uint32_t sampleRate = track->f.sampleRate;
	uint32_t smpteOffset = 0;
	uint32_t cycleLength = 0;

	writeU8(&midiNote);
	writeU32(&sampleRate);
	writeU32(&smpteOffset);
	writeU32(&cycleLength);
	return AF_SUCCEED;
}

status SampleVisionFile::writeLoops()
{
	for (int i=0; i<8; i++)
	{
		uint32_t startFrame = kSMPInvalidSamplePosition, endFrame = 0;
		uint8_t type = 0;
		uint16_t count = 0;
		writeU32(&startFrame);
		writeU32(&endFrame);
		writeU8(&type);
		writeU16(&count);
	}
	return AF_SUCCEED;
}

status SampleVisionFile::writeMarkers()
{
	for (int i=0; i<8; i++)
	{
		char name[kSMPMarkerNameLength + 1];
		memset(name, ' ', kSMPMarkerNameLength);
		name[kSMPMarkerNameLength] = '\0';
		fh->write(name, kSMPMarkerNameLength);
		uint32_t position = kSMPInvalidSamplePosition;
		writeU32(&position);
	}
	return AF_SUCCEED;
}