/src/libsndfile/src/mpc2k.c
Line | Count | Source |
1 | | /* |
2 | | ** Copyright (C) 2008-2017 Erik de Castro Lopo <erikd@mega-nerd.com> |
3 | | ** |
4 | | ** This program is free software; you can redistribute it and/or modify |
5 | | ** it under the terms of the GNU Lesser General Public License as published by |
6 | | ** the Free Software Foundation; either version 2.1 of the License, or |
7 | | ** (at your option) any later version. |
8 | | ** |
9 | | ** This program is distributed in the hope that it will be useful, |
10 | | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | ** GNU Lesser General Public License for more details. |
13 | | ** |
14 | | ** You should have received a copy of the GNU Lesser General Public License |
15 | | ** along with this program; if not, write to the Free Software |
16 | | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
17 | | */ |
18 | | |
19 | | #include "sfconfig.h" |
20 | | |
21 | | #include <stdio.h> |
22 | | #include <fcntl.h> |
23 | | #include <string.h> |
24 | | #include <ctype.h> |
25 | | |
26 | | #include "sndfile.h" |
27 | | #include "sfendian.h" |
28 | | #include "common.h" |
29 | | |
30 | | /* |
31 | | ** Info from Olivier Tristan <o.tristan@ultimatesoundbank.com> |
32 | | ** |
33 | | ** HEADER |
34 | | ** 2 magic bytes: 1 and 4. |
35 | | ** 17 char for the name of the sample. |
36 | | ** 3 bytes: level, tune and channels (0 for channels is mono while 1 is stereo) |
37 | | ** 4 uint32: sampleStart, loopEnd, sampleFrames and loopLength |
38 | | ** 1 byte: loopMode (0 no loop, 1 forward looping) |
39 | | ** 1 byte: number of beat in loop |
40 | | ** 1 uint16: sampleRate |
41 | | ** |
42 | | ** DATA |
43 | | ** Data are always non compressed 16 bits interleaved |
44 | | */ |
45 | | |
46 | 0 | #define HEADER_LENGTH 42 /* Sum of above data fields. */ |
47 | 52 | #define HEADER_NAME_LEN 17 /* Length of name string. */ |
48 | | |
49 | | /*------------------------------------------------------------------------------ |
50 | | ** Private static functions. |
51 | | */ |
52 | | |
53 | | static int mpc2k_close (SF_PRIVATE *psf) ; |
54 | | |
55 | | static int mpc2k_write_header (SF_PRIVATE *psf, int calc_length) ; |
56 | | static int mpc2k_read_header (SF_PRIVATE *psf) ; |
57 | | |
58 | | /*------------------------------------------------------------------------------ |
59 | | ** Public function. |
60 | | */ |
61 | | |
62 | | int |
63 | | mpc2k_open (SF_PRIVATE *psf) |
64 | 26 | { int error = 0 ; |
65 | | |
66 | 26 | if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0)) |
67 | 26 | { if ((error = mpc2k_read_header (psf))) |
68 | 0 | return error ; |
69 | 26 | } ; |
70 | | |
71 | 26 | if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_MPC2K) |
72 | 0 | return SFE_BAD_OPEN_FORMAT ; |
73 | | |
74 | 26 | if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR) |
75 | 0 | { if (mpc2k_write_header (psf, SF_FALSE)) |
76 | 0 | return psf->error ; |
77 | | |
78 | 0 | psf->write_header = mpc2k_write_header ; |
79 | 26 | } ; |
80 | | |
81 | 26 | psf->container_close = mpc2k_close ; |
82 | | |
83 | 26 | psf->blockwidth = (sf_count_t) psf->bytewidth * psf->sf.channels ; |
84 | | |
85 | 26 | error = pcm_init (psf) ; |
86 | | |
87 | 26 | return error ; |
88 | 26 | } /* mpc2k_open */ |
89 | | |
90 | | /*------------------------------------------------------------------------------ |
91 | | */ |
92 | | |
93 | | static int |
94 | | mpc2k_close (SF_PRIVATE *psf) |
95 | 26 | { |
96 | 26 | if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR) |
97 | 0 | mpc2k_write_header (psf, SF_TRUE) ; |
98 | | |
99 | 26 | return 0 ; |
100 | 26 | } /* mpc2k_close */ |
101 | | |
102 | | static int |
103 | | mpc2k_write_header (SF_PRIVATE *psf, int calc_length) |
104 | 0 | { char sample_name [HEADER_NAME_LEN + 1] ; |
105 | 0 | sf_count_t current ; |
106 | |
|
107 | 0 | if (psf->pipeoffset > 0) |
108 | 0 | return 0 ; |
109 | | |
110 | 0 | current = psf_ftell (psf) ; |
111 | |
|
112 | 0 | if (calc_length) |
113 | 0 | { psf->filelength = psf_get_filelen (psf) ; |
114 | |
|
115 | 0 | psf->dataoffset = HEADER_LENGTH ; |
116 | 0 | psf->datalength = psf->filelength - psf->dataoffset ; |
117 | |
|
118 | 0 | psf->sf.frames = psf->datalength / ((sf_count_t) psf->bytewidth * psf->sf.channels) ; |
119 | 0 | } ; |
120 | | |
121 | | /* Reset the current header length to zero. */ |
122 | 0 | psf->header.ptr [0] = 0 ; |
123 | 0 | psf->header.indx = 0 ; |
124 | | |
125 | | /* |
126 | | ** Only attempt to seek if we are not writng to a pipe. If we are |
127 | | ** writing to a pipe we shouldn't be here anyway. |
128 | | */ |
129 | 0 | if (psf->is_pipe == SF_FALSE) |
130 | 0 | psf_fseek (psf, 0, SEEK_SET) ; |
131 | |
|
132 | 0 | snprintf (sample_name, sizeof (sample_name), "%-*.*s", HEADER_NAME_LEN, HEADER_NAME_LEN, psf->file.name) ; |
133 | |
|
134 | 0 | psf_binheader_writef (psf, "e11b", BHW1 (1), BHW1 (4), BHWv (sample_name), BHWz (HEADER_NAME_LEN)) ; |
135 | 0 | psf_binheader_writef (psf, "e111", BHW1 (100), BHW1 (0), BHW1 ((psf->sf.channels - 1) & 1)) ; |
136 | 0 | psf_binheader_writef (psf, "et4888", BHW4 (0), BHW8 (psf->sf.frames), BHW8 (psf->sf.frames), BHW8 (psf->sf.frames)) ; |
137 | 0 | psf_binheader_writef (psf, "e112", BHW1 (0), BHW1 (1), BHW2 ((uint16_t) psf->sf.samplerate)) ; |
138 | | |
139 | | /* Always 16 bit little endian data. */ |
140 | 0 | psf->bytewidth = 2 ; |
141 | 0 | psf->endian = SF_ENDIAN_LITTLE ; |
142 | |
|
143 | 0 | psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ; |
144 | |
|
145 | 0 | if (psf->error) |
146 | 0 | return psf->error ; |
147 | | |
148 | 0 | psf->dataoffset = psf->header.indx ; |
149 | |
|
150 | 0 | if (current > 0) |
151 | 0 | psf_fseek (psf, current, SEEK_SET) ; |
152 | |
|
153 | 0 | return psf->error ; |
154 | 0 | } /* mpc2k_write_header */ |
155 | | |
156 | | static int |
157 | | mpc2k_read_header (SF_PRIVATE *psf) |
158 | 26 | { char sample_name [HEADER_NAME_LEN + 1] ; |
159 | 26 | unsigned char bytes [4] ; |
160 | 26 | uint32_t sample_start, loop_end, sample_frames, loop_length ; |
161 | 26 | uint16_t sample_rate ; |
162 | | |
163 | 26 | psf_binheader_readf (psf, "pebb", 0, bytes, 2, sample_name, make_size_t (HEADER_NAME_LEN)) ; |
164 | | |
165 | 26 | if (bytes [0] != 1 || bytes [1] != 4) |
166 | 0 | return SFE_MPC_NO_MARKER ; |
167 | | |
168 | 26 | sample_name [HEADER_NAME_LEN] = 0 ; |
169 | | |
170 | 26 | psf_log_printf (psf, "MPC2000\n Name : %s\n", sample_name) ; |
171 | | |
172 | 26 | psf_binheader_readf (psf, "eb4444", bytes, 3, &sample_start, &loop_end, &sample_frames, &loop_length) ; |
173 | | |
174 | 26 | psf->sf.channels = bytes [2] ? 2 : 1 ; |
175 | | |
176 | 26 | psf_log_printf (psf, " Level : %d\n Tune : %d\n Stereo : %s\n", bytes [0], bytes [1], bytes [2] ? "Yes" : "No") ; |
177 | | |
178 | 26 | psf_log_printf (psf, " Sample start : %d\n Loop end : %d\n Frames : %d\n Length : %d\n", sample_start, loop_end, sample_frames, loop_length) ; |
179 | | |
180 | 26 | psf_binheader_readf (psf, "eb2", bytes, 2, &sample_rate) ; |
181 | | |
182 | 26 | psf_log_printf (psf, " Loop mode : %s\n Beats : %d\n Sample rate : %d\nEnd\n", bytes [0] ? "None" : "Fwd", bytes [1], sample_rate) ; |
183 | | |
184 | 26 | psf->sf.samplerate = sample_rate ; |
185 | | |
186 | 26 | psf->sf.format = SF_FORMAT_MPC2K | SF_FORMAT_PCM_16 ; |
187 | | |
188 | 26 | psf->dataoffset = psf_ftell (psf) ; |
189 | | |
190 | | /* Always 16 bit little endian data. */ |
191 | 26 | psf->bytewidth = 2 ; |
192 | 26 | psf->endian = SF_ENDIAN_LITTLE ; |
193 | | |
194 | 26 | psf->datalength = psf->filelength - psf->dataoffset ; |
195 | 26 | psf->blockwidth = (sf_count_t) psf->sf.channels * psf->bytewidth ; |
196 | 26 | psf->sf.frames = psf->datalength / psf->blockwidth ; |
197 | | |
198 | 26 | psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ; |
199 | | |
200 | 26 | return 0 ; |
201 | 26 | } /* mpc2k_read_header */ |
202 | | |