0170f7ccfb9546734b56b3f213be0f4301ae1da1
[Faustine.git] / interpretor / libsndfile-1.0.25 / src / mpc2k.c
1 /*
2 ** Copyright (C) 2008-2011 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 #define HEADER_LENGTH 42 /* Sum of above data fields. */
47 #define HEADER_NAME_LEN 17 /* Length of name string. */
48
49 #define SFE_MPC_NO_MARKER 666
50
51 /*------------------------------------------------------------------------------
52 ** Private static functions.
53 */
54
55 static int mpc2k_close (SF_PRIVATE *psf) ;
56
57 static int mpc2k_write_header (SF_PRIVATE *psf, int calc_length) ;
58 static int mpc2k_read_header (SF_PRIVATE *psf) ;
59
60 /*------------------------------------------------------------------------------
61 ** Public function.
62 */
63
64 int
65 mpc2k_open (SF_PRIVATE *psf)
66 { int error = 0 ;
67
68 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
69 { if ((error = mpc2k_read_header (psf)))
70 return error ;
71 } ;
72
73 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_MPC2K)
74 return SFE_BAD_OPEN_FORMAT ;
75
76 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
77 { if (mpc2k_write_header (psf, SF_FALSE))
78 return psf->error ;
79
80 psf->write_header = mpc2k_write_header ;
81 } ;
82
83 psf->container_close = mpc2k_close ;
84
85 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
86
87 error = pcm_init (psf) ;
88
89 return error ;
90 } /* mpc2k_open */
91
92 /*------------------------------------------------------------------------------
93 */
94
95 static int
96 mpc2k_close (SF_PRIVATE *psf)
97 {
98 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
99 mpc2k_write_header (psf, SF_TRUE) ;
100
101 return 0 ;
102 } /* mpc2k_close */
103
104 static int
105 mpc2k_write_header (SF_PRIVATE *psf, int calc_length)
106 { char sample_name [HEADER_NAME_LEN + 1] ;
107 sf_count_t current ;
108
109 if (psf->pipeoffset > 0)
110 return 0 ;
111
112 current = psf_ftell (psf) ;
113
114 if (calc_length)
115 { psf->filelength = psf_get_filelen (psf) ;
116
117 psf->dataoffset = HEADER_LENGTH ;
118 psf->datalength = psf->filelength - psf->dataoffset ;
119
120 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
121 } ;
122
123 /* Reset the current header length to zero. */
124 psf->header [0] = 0 ;
125 psf->headindex = 0 ;
126
127 /*
128 ** Only attempt to seek if we are not writng to a pipe. If we are
129 ** writing to a pipe we shouldn't be here anyway.
130 */
131 if (psf->is_pipe == SF_FALSE)
132 psf_fseek (psf, 0, SEEK_SET) ;
133
134 snprintf (sample_name, sizeof (sample_name), "%s ", psf->file.name.c) ;
135
136 psf_binheader_writef (psf, "e11b", 1, 4, sample_name, make_size_t (HEADER_NAME_LEN)) ;
137 psf_binheader_writef (psf, "e111", 100, 0, (psf->sf.channels - 1) & 1) ;
138 psf_binheader_writef (psf, "et4888", 0, psf->sf.frames, psf->sf.frames, psf->sf.frames) ;
139 psf_binheader_writef (psf, "e112", 0, 1, (uint16_t) psf->sf.samplerate) ;
140
141 /* Always 16 bit little endian data. */
142 psf->bytewidth = 2 ;
143 psf->endian = SF_ENDIAN_LITTLE ;
144
145 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
146
147 if (psf->error)
148 return psf->error ;
149
150 psf->dataoffset = psf->headindex ;
151
152 if (current > 0)
153 psf_fseek (psf, current, SEEK_SET) ;
154
155 return psf->error ;
156 } /* mpc2k_write_header */
157
158 static int
159 mpc2k_read_header (SF_PRIVATE *psf)
160 { char sample_name [HEADER_NAME_LEN + 1] ;
161 unsigned char bytes [4] ;
162 uint32_t sample_start, loop_end, sample_frames, loop_length ;
163 uint16_t sample_rate ;
164
165 psf_binheader_readf (psf, "pebb", 0, bytes, 2, sample_name, make_size_t (HEADER_NAME_LEN)) ;
166
167 if (bytes [0] != 1 || bytes [1] != 4)
168 return SFE_MPC_NO_MARKER ;
169
170 sample_name [HEADER_NAME_LEN] = 0 ;
171
172 psf_log_printf (psf, "MPC2000\n Name : %s\n", sample_name) ;
173
174 psf_binheader_readf (psf, "eb4444", bytes, 3, &sample_start, &loop_end, &sample_frames, &loop_length) ;
175
176 psf->sf.channels = bytes [2] ? 2 : 1 ;
177
178 psf_log_printf (psf, " Level : %d\n Tune : %d\n Stereo : %s\n", bytes [0], bytes [1], bytes [2] ? "Yes" : "No") ;
179
180 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) ;
181
182 psf_binheader_readf (psf, "eb2", bytes, 2, &sample_rate) ;
183
184 psf_log_printf (psf, " Loop mode : %s\n Beats : %d\n Sample rate : %d\nEnd\n", bytes [0] ? "None" : "Fwd", bytes [1], sample_rate) ;
185
186 psf->sf.samplerate = sample_rate ;
187
188 psf->sf.format = SF_FORMAT_MPC2K | SF_FORMAT_PCM_16 ;
189
190 psf->dataoffset = psf_ftell (psf) ;
191
192 /* Always 16 bit little endian data. */
193 psf->bytewidth = 2 ;
194 psf->endian = SF_ENDIAN_LITTLE ;
195
196 psf->datalength = psf->filelength - psf->dataoffset ;
197 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
198 psf->sf.frames = psf->datalength / psf->blockwidth ;
199
200 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
201
202 return 0 ;
203 } /* mpc2k_read_header */
204