1 | /* |
---|
2 | Audio File Library |
---|
3 | Copyright (C) 1998-2000, Michael Pruett <michael@68k.org> |
---|
4 | |
---|
5 | This library is free software; you can redistribute it and/or |
---|
6 | modify it under the terms of the GNU Library General Public |
---|
7 | License as published by the Free Software Foundation; either |
---|
8 | version 2 of the License, or (at your option) any later version. |
---|
9 | |
---|
10 | This library is distributed in the hope that it will be useful, |
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | Library General Public License for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU Library General Public |
---|
16 | License along with this library; if not, write to the |
---|
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
18 | Boston, MA 02111-1307 USA. |
---|
19 | */ |
---|
20 | |
---|
21 | /* |
---|
22 | nextwrite.c |
---|
23 | |
---|
24 | This file contains routines for writing NeXT/Sun format sound files. |
---|
25 | */ |
---|
26 | |
---|
27 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <string.h> |
---|
30 | #include <assert.h> |
---|
31 | |
---|
32 | #include "audiofile.h" |
---|
33 | #include "afinternal.h" |
---|
34 | #include "next.h" |
---|
35 | #include "byteorder.h" |
---|
36 | #include "util.h" |
---|
37 | #include "setup.h" |
---|
38 | |
---|
39 | status _af_next_update (AFfilehandle file); |
---|
40 | |
---|
41 | static u_int32_t nextencodingtype (_AudioFormat *format); |
---|
42 | static status next_write_header (AFfilehandle file); |
---|
43 | |
---|
44 | /* A return value of zero indicates successful synchronisation. */ |
---|
45 | status _af_next_update (AFfilehandle file) |
---|
46 | { |
---|
47 | next_write_header(file); |
---|
48 | return AF_SUCCEED; |
---|
49 | } |
---|
50 | |
---|
51 | static status next_write_header (AFfilehandle file) |
---|
52 | { |
---|
53 | _Track *track; |
---|
54 | int frameSize; |
---|
55 | u_int32_t offset, length, encoding, sampleRate, channelCount; |
---|
56 | |
---|
57 | track = _af_filehandle_get_track(file, AF_DEFAULT_TRACK); |
---|
58 | |
---|
59 | frameSize = _af_format_frame_size(&track->f, AF_FALSE); |
---|
60 | |
---|
61 | offset = HOST_TO_BENDIAN_INT32(track->fpos_first_frame); |
---|
62 | length = HOST_TO_BENDIAN_INT32(track->totalfframes * frameSize); |
---|
63 | encoding = HOST_TO_BENDIAN_INT32(nextencodingtype(&track->f)); |
---|
64 | sampleRate = HOST_TO_BENDIAN_INT32(track->f.sampleRate); |
---|
65 | channelCount = HOST_TO_BENDIAN_INT32(track->f.channelCount); |
---|
66 | |
---|
67 | if (af_fseek(file->fh, 0, SEEK_SET) != 0) |
---|
68 | _af_error(AF_BAD_LSEEK, "bad seek"); |
---|
69 | |
---|
70 | af_fwrite(".snd", 4, 1, file->fh); |
---|
71 | af_fwrite(&offset, 4, 1, file->fh); |
---|
72 | af_fwrite(&length, 4, 1, file->fh); |
---|
73 | af_fwrite(&encoding, 4, 1, file->fh); |
---|
74 | af_fwrite(&sampleRate, 4, 1, file->fh); |
---|
75 | af_fwrite(&channelCount, 4, 1, file->fh); |
---|
76 | |
---|
77 | return AF_SUCCEED; |
---|
78 | } |
---|
79 | |
---|
80 | static u_int32_t nextencodingtype (_AudioFormat *format) |
---|
81 | { |
---|
82 | u_int32_t encoding = 0; |
---|
83 | |
---|
84 | if (format->compressionType != AF_COMPRESSION_NONE) |
---|
85 | { |
---|
86 | if (format->compressionType == AF_COMPRESSION_G711_ULAW) |
---|
87 | encoding = _AU_FORMAT_MULAW_8; |
---|
88 | else if (format->compressionType == AF_COMPRESSION_G711_ALAW) |
---|
89 | encoding = _AU_FORMAT_ALAW_8; |
---|
90 | } |
---|
91 | else if (format->sampleFormat == AF_SAMPFMT_TWOSCOMP) |
---|
92 | { |
---|
93 | if (format->sampleWidth == 8) |
---|
94 | encoding = _AU_FORMAT_LINEAR_8; |
---|
95 | else if (format->sampleWidth == 16) |
---|
96 | encoding = _AU_FORMAT_LINEAR_16; |
---|
97 | else if (format->sampleWidth == 24) |
---|
98 | encoding = _AU_FORMAT_LINEAR_24; |
---|
99 | else if (format->sampleWidth == 32) |
---|
100 | encoding = _AU_FORMAT_LINEAR_32; |
---|
101 | } |
---|
102 | else if (format->sampleFormat == AF_SAMPFMT_FLOAT) |
---|
103 | encoding = _AU_FORMAT_FLOAT; |
---|
104 | else if (format->sampleFormat == AF_SAMPFMT_DOUBLE) |
---|
105 | encoding = _AU_FORMAT_DOUBLE; |
---|
106 | |
---|
107 | return encoding; |
---|
108 | } |
---|
109 | |
---|
110 | status _af_next_write_init (AFfilesetup setup, AFfilehandle filehandle) |
---|
111 | { |
---|
112 | _Track *track; |
---|
113 | |
---|
114 | if (_af_filesetup_make_handle(setup, filehandle) == AF_FAIL) |
---|
115 | return AF_FAIL; |
---|
116 | |
---|
117 | filehandle->formatSpecific = NULL; |
---|
118 | |
---|
119 | if (filehandle->miscellaneousCount > 0) |
---|
120 | { |
---|
121 | _af_error(AF_BAD_NUMMISC, "NeXT format supports no miscellaneous chunks"); |
---|
122 | return AF_FAIL; |
---|
123 | } |
---|
124 | |
---|
125 | next_write_header(filehandle); |
---|
126 | |
---|
127 | track = _af_filehandle_get_track(filehandle, AF_DEFAULT_TRACK); |
---|
128 | track->fpos_first_frame = 28; |
---|
129 | |
---|
130 | track->f.byteOrder = AF_BYTEORDER_BIGENDIAN; |
---|
131 | |
---|
132 | return AF_SUCCEED; |
---|
133 | } |
---|