summaryrefslogtreecommitdiff
path: root/libaudiofile/avrwrite.c
blob: 8d8a8966e41a4ce87e039d1d335897b9b9d6521a (plain)
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
/*
	Audio File Library
	Copyright (C) 2004, 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.
*/

/*
	avrwrite.c

	This file contains routines for writing AVR (Audio Visual
	Research) sound files.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

#include "audiofile.h"
#include "afinternal.h"
#include "byteorder.h"
#include "util.h"
#include "setup.h"

#include "avr.h"

status _af_avr_update (AFfilehandle file)
{
	_Track		*track;
	u_int32_t	size, loopStart, loopEnd;

	track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK);

	/* Seek to the position of the size field. */
	af_fseek(file->fh, 26, SEEK_SET);

	size = track->totalfframes;

	/* For the case of no loops, loopStart = 0 and loopEnd = size. */
	loopStart = 0;
	loopEnd = size;

	af_write_uint32_be(&size, file->fh);
	af_write_uint32_be(&loopStart, file->fh);
	af_write_uint32_be(&loopEnd, file->fh);

	return AF_SUCCEED;
}

static char *af_basename (char *filename)
{
	char	*base;
	base = strrchr(filename, '/');
	if (base == NULL)
		return filename;
	else
		return base + 1;
}

status _af_avr_write_init (AFfilesetup setup, AFfilehandle filehandle)
{
	_Track		*track;
	char		name[8];
	u_int16_t	mono, resolution, sign, loop, midi;
	u_int32_t	rate, size, loopStart, loopEnd;
	char		reserved[26];
	char		user[64];

	if (_af_filesetup_make_handle(setup, filehandle) == AF_FAIL)
		return AF_FAIL;

	filehandle->formatSpecific = NULL;

	track = _af_filehandle_get_track(filehandle, AF_DEFAULT_TRACK);

	if (af_fseek(filehandle->fh, 0, SEEK_SET) != 0)
	{
		_af_error(AF_BAD_LSEEK, "bad seek");
		return AF_FAIL;
	}

	af_fwrite("2BIT", 4, 1, filehandle->fh);
	memset(name, 0, 8);
	if (filehandle->fileName != NULL)
		strncpy(name, af_basename(filehandle->fileName), 8);
	af_fwrite(name, 8, 1, filehandle->fh);

	if (track->f.channelCount == 1)
		mono = 0x0;
	else
		mono = 0xffff;
	af_write_uint16_be(&mono, filehandle->fh);

	resolution = track->f.sampleWidth;
	af_write_uint16_be(&resolution, filehandle->fh);

	if (track->f.sampleFormat == AF_SAMPFMT_UNSIGNED)
		sign = 0x0;
	else
		sign = 0xffff;
	af_write_uint16_be(&sign, filehandle->fh);

	/* We do not currently support loops. */
	loop = 0;
	af_write_uint16_be(&loop, filehandle->fh);
	midi = 0xffff;
	af_write_uint16_be(&midi, filehandle->fh);

	rate = track->f.sampleRate;
	/* Set the high-order byte of rate to 0xff. */
	rate |= 0xff000000;
	size = track->totalfframes;
	loopStart = 0;
	loopEnd = size;

	af_write_uint32_be(&rate, filehandle->fh);
	af_write_uint32_be(&size, filehandle->fh);
	af_write_uint32_be(&loopStart, filehandle->fh);
	af_write_uint32_be(&loopEnd, filehandle->fh);

	memset(reserved, 0, 26);
	af_fwrite(reserved, 26, 1, filehandle->fh);

	memset(user, 0, 64);
	af_fwrite(user, 64, 1, filehandle->fh);

	if (track->fpos_first_frame == 0)
		track->fpos_first_frame = af_ftell(filehandle->fh);

	return AF_SUCCEED;
}