File: Raw.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 (185 lines) | stat: -rw-r--r-- 4,391 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
/*
	Audio File Library
	Copyright (C) 2000, Silicon Graphics, Inc.

	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.
*/

/*
	Raw.cpp

	This file contains code for reading and writing raw audio
	data files.
*/

#include "config.h"
#include "Raw.h"

#include "File.h"
#include "Setup.h"
#include "Track.h"
#include "util.h"

static _AFfilesetup raw_default_filesetup =
{
	_AF_VALID_FILESETUP,	/* valid */
	AF_FILE_RAWDATA,	/* fileFormat */
	true,			/* trackSet */
	true,			/* instrumentSet */
	true,			/* miscellaneousSet */
	1,			/* trackCount */
	NULL,			/* tracks */
	0,			/* instrumentCount */
	NULL,			/* instruments */
	0,			/* miscellaneousCount */
	NULL			/* miscellaneous */
};

const int _af_raw_compression_types[_AF_RAW_NUM_COMPTYPES] =
{
	AF_COMPRESSION_G711_ULAW,
	AF_COMPRESSION_G711_ALAW
};

bool RawFile::recognize(File *fh)
{
	return false;
}

status RawFile::readInit(AFfilesetup filesetup)
{
	Track	*track;

	if (filesetup == NULL)
	{
		_af_error(AF_BAD_FILEHANDLE, "a valid AFfilesetup is required for reading raw data");
		return AF_FAIL;
	}

	if (_af_filesetup_make_handle(filesetup, this) == AF_FAIL)
		return AF_FAIL;

	track = &tracks[0];

	/* Set the track's data offset. */
	if (filesetup->tracks[0].dataOffsetSet)
		track->fpos_first_frame = filesetup->tracks[0].dataOffset;
	else
		track->fpos_first_frame = 0;

	/* Set the track's frame count. */
	if (filesetup->tracks[0].frameCountSet)
	{
		track->totalfframes = filesetup->tracks[0].frameCount;
	}
	else
	{
		AFfileoffset filesize = fh->length();
		if (filesize == -1)
			track->totalfframes = -1;
		else
		{
			/* Ensure that the data offset is valid. */
			if (track->fpos_first_frame > filesize)
			{
				_af_error(AF_BAD_FILESETUP, "data offset is larger than file size");
				return AF_FAIL;
			}

			filesize -= track->fpos_first_frame;
			track->totalfframes = filesize / (int) _af_format_frame_size(&track->f, false);
		}
		track->data_size = filesize;
	}

	return AF_SUCCEED;
}

status RawFile::writeInit(AFfilesetup filesetup)
{
	Track	*track;

	if (_af_filesetup_make_handle(filesetup, this) == AF_FAIL)
		return AF_FAIL;

	track = &tracks[0];
	track->totalfframes = 0;
	if (filesetup->tracks[0].dataOffsetSet)
		track->fpos_first_frame = filesetup->tracks[0].dataOffset;
	else
		track->fpos_first_frame = 0;

	return AF_SUCCEED;
}

status RawFile::update()
{
	return AF_SUCCEED;
}

AFfilesetup RawFile::completeSetup(AFfilesetup setup)
{
	AFfilesetup	newSetup;

	if (setup->trackSet && setup->trackCount != 1)
	{
		_af_error(AF_BAD_FILESETUP, "raw file must have exactly one track");
		return AF_NULL_FILESETUP;
	}

	TrackSetup *track = setup->getTrack();
	if (!track)
	{
		_af_error(AF_BAD_FILESETUP, "could not access track in file setup");
		return AF_NULL_FILESETUP;
	}

	if (track->aesDataSet)
	{
		_af_error(AF_BAD_FILESETUP, "raw file cannot have AES data");
		return AF_NULL_FILESETUP;
	}

	if (track->markersSet && track->markerCount != 0)
	{
		_af_error(AF_BAD_NUMMARKS, "raw file cannot have markers");
		return AF_NULL_FILESETUP;
	}

	if (setup->instrumentSet && setup->instrumentCount != 0)
	{
		_af_error(AF_BAD_NUMINSTS, "raw file cannot have instruments");
		return AF_NULL_FILESETUP;
	}

	if (setup->miscellaneousSet && setup->miscellaneousCount != 0)
	{
		_af_error(AF_BAD_NUMMISC, "raw file cannot have miscellaneous data");
		return AF_NULL_FILESETUP;
	}

	newSetup = (_AFfilesetup *) _af_malloc(sizeof (_AFfilesetup));
	*newSetup = raw_default_filesetup;

	newSetup->tracks = (TrackSetup *) _af_malloc(sizeof (TrackSetup));
	newSetup->tracks[0] = setup->tracks[0];
	newSetup->tracks[0].f.compressionParams = NULL;

	newSetup->tracks[0].markerCount = 0;
	newSetup->tracks[0].markers = NULL;

	return newSetup;
}