File: fmfconv_aiff.c

package info (click to toggle)
fuse-emulator-utils 1.3.1-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,480 kB
  • ctags: 1,058
  • sloc: sh: 11,498; ansic: 10,101; cpp: 1,116; makefile: 158
file content (184 lines) | stat: -rw-r--r-- 5,627 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
/* fmfconv_aiff.c: aiff output routine included into fmfconv.c
   Copyright (c) 2010-2015 Gergely Szasz

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: szaszg@hu.inter.net

*/
#include <config.h>

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

#include "libspectrum.h"
#include "fmfconv.h"


/*
Byte order: Big-endian

Offset   Length   Contents
  0      4 bytes  "FORM"
  4      4 bytes  <File length - 8>
  8      4 bytes  "AIFF"

 12      4 bytes  "COMM"
 16      4 bytes  <Length of the COMM data> // (=18)
 20      2 bytes  <Channels>     // Channels: 1 = mono, 2 = stereo
 22      4 bytes  <No of frames>
 26      2 bytes  <bits/sample>  // 8 or 16
 36     10 bytes  <Frame rate>  // Frameses per second: e.g., 44100
.................................................... PCM
 38      4 bytes  "SSND"
 42      4 bytes  <No of bytes - 46>
 46      4 bytes  <Offset>	// (=0)
 50      4 bytes  <Block>	// (=0)
 54     (n)bytes  <Sample data>
..................................................... Law!!!


AIFF 80bit FLOAT

if exp = 16383 --> *2^0
[sign1][exp15][1][mant63]
0 exp-16383
*/
#define AIFF_POS_FILELEN  4L
#define AIFF_POS_NOFRAME 22L
#define AIFF_POS_SSNDLEN 42L

#define AIFF_POS_SSNCLEN 62L

int force_aifc = 0;			/* record aifc file even PCM sound */
static int aifc = 0;

/* save float to head[0..9] */
static void
aiff_int_2_80bitfloat( libspectrum_byte head[], libspectrum_word num )
{
  uint64_t frac = num;
  uint64_t max = (uint64_t)1 << 63;
  uint16_t exp = 16382;

  if( num < 1 ) return;

  while( frac < max ) {
    frac <<= 1;
  }
  while( num ) {
    exp++;
    num >>= 1;
  }
  head[0] = exp >> 8;              head[1] = exp & 0xff;
  head[2] = frac >> 56;            head[3] = ( frac >> 48 ) & 0xff;
  head[4] = ( frac >> 40 ) & 0xff; head[5] = ( frac >> 32 ) & 0xff;
  head[6] = ( frac >> 24 ) & 0xff; head[7] = ( frac >> 16 ) & 0xff;
  head[8] = ( frac >>  8 ) & 0xff; head[9] = frac & 0xff;
}

static int
snd_write_aiffheader( void )
{
  libspectrum_byte buff[10];

  if( force_aifc || snd_enc != TYPE_PCM ) aifc = 1;		/* AIFF or AIFC */

  if( aifc )
    fwrite( "FORM\377\377\377\377AIFCCOMM\000\000\000\046", 20, 1, snd );
  else
    fwrite( "FORM\377\377\377\377AIFFCOMM\000\000\000\022", 20, 1, snd );

  if( snd_chn == 2 )
    fwrite( "\000\002", 2, 1, snd );			/* Stereo */
  else
    fwrite( "\000\001", 2, 1, snd );			/* Mono */

  fwrite( "\377\377\377\377", 4, 1, snd );			/* Stereo */
  if( snd_enc == TYPE_PCM )
    fwrite( "\000\020", 2, 1, snd );/* 16 bit/ sample */
  else
    fwrite( "\000\010", 2, 1, snd );/* 8 bit/ sample */

  aiff_int_2_80bitfloat( buff, out_rte );
  fwrite( buff, 10, 1, snd );			/* sampling rate in Hz */

/* NONE not compressed.
   ulaw uLaw 2:1.......
   alaw aLaw 2:1....... */
  if( aifc ) {
    if( snd_enc == TYPE_PCM )
      fwrite( "NONE\020not compressed ", 20, 1, snd );	/* PCM */
    else if( snd_enc == TYPE_ALW )
      fwrite( "alaw\020aLaw 2:1       ", 20, 1, snd );	/* A-Law */
    else
      fwrite( "ulaw\020uLaw 2:1       ", 20, 1, snd );	/* u-Law */
  }

  fwrite( "SSND\377\377\377\377\000\000\000\000\000\000\000\000", 16, 1, snd );/* data Chunk header */
  snd_header_ok = 1;
  printi( 1, "snd_write_aif%cheader(): %dHz %c encoded %s\n", aifc ? 'c' : 'f',
          out_rte, snd_enc, snd_chn == 2 ? "stereo" : "mono" );
  return 0;
}

int
snd_write_aiff( void )
{
  int err;

  if( !snd_header_ok && ( err = snd_write_aiffheader() ) ) return err;
  if( snd_enc == TYPE_PCM && snd_little_endian ) {	/* we have to swap all sample */
    pcm_swap_endian();
  }

  if( fwrite( sound8, snd_len, 1, snd ) != 1 ) return ERR_WRITE_SND;
  printi( 2, "snd_write_aif%c(): %d samples (%d bytes) sound\n", aifc ? 'c' : 'f', snd_len/snd_fsz, snd_len );

  return 0;
}

void
snd_finalize_aiff( void )
{
  libspectrum_byte buff[4];
  long pos = ftell( snd );
  long dsz = pos;

  if( fseek( snd, AIFF_POS_FILELEN, SEEK_SET ) == -1 ) {
    printw( "snd_finalize_aif%c(): cannot finalize sound output file (not seekable).\n", aifc ? 'c' : 'f' );
  } else {
    pos -= 8;
    buff[3] = pos & 0xff;           buff[2] = ( pos >> 8 ) & 0xff;
    buff[1] = ( pos >> 16 ) & 0xff; buff[0] = pos >> 24;
    fwrite( buff, 4, 1, snd );			/* set file length - 8 */

    fseek( snd, aifc ? AIFF_POS_SSNCLEN : AIFF_POS_SSNDLEN, SEEK_SET );
    pos -= aifc ? 52 : 32;
    buff[3] = pos & 0xff;           buff[2] = ( pos >> 8 ) & 0xff;
    buff[1] = ( pos >> 16 ) & 0xff; buff[0] = pos >> 24;
    fwrite( buff, 4, 1, snd );			/* set file length - 8 */

    fseek( snd, AIFF_POS_NOFRAME, SEEK_SET );
    dsz = pos - 8; dsz /= snd_fsz;
    buff[3] = dsz & 0xff;           buff[2] = ( dsz >> 8 ) & 0xff;
    buff[1] = ( dsz >> 16 ) & 0xff; buff[0] = dsz >> 24;
    fwrite( buff, 4, 1, snd );			/* set file length - 8 */

    printi( 1, "snd_finalize_aif%c(): FORM size: %ldbyte COMM frames: %ldbyte SSND size: %ld\n", aifc ? 'c' : 'f', pos + 32, dsz, pos );
  }
}