File: fmfconv_au.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 (105 lines) | stat: -rw-r--r-- 3,030 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
/* fmfconv_au.c: au output routine included into fmfconv.c
   Copyright (c) 2004-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"


static int
snd_write_auheader( void )
{
  const char str[] =
    "ZX Spectrum audio created by fmfconv: http://fuse-emulator.sourceforge.net/";
  size_t len = sizeof(str); /* Includes the null terminator */
  int padding = len % 8;
  libspectrum_dword buff[6];

#ifdef WORDS_BIGENDIAN
  buff[0] = 0x2e736e64UL;		/* magic id */
  buff[1] = 24 + 64;
  buff[2] = ~0U;
  buff[3] = snd_enc == TYPE_PCM ? 3 : ( snd_enc == TYPE_ULW ? 1 : 27 );
  buff[4] = out_rte;
  buff[5] = snd_chn;
#else
  buff[0] = swap_endian_dword( 0x2e736e64UL );		/* magic id */
  buff[1] = swap_endian_dword( 24 + 64 );
  buff[2] = swap_endian_dword( ~0U );
  buff[3] = swap_endian_dword( snd_enc == TYPE_PCM ? 3 : ( snd_enc == TYPE_ULW ? 1 : 27 ) );
  buff[4] = swap_endian_dword( out_rte );
  buff[5] = swap_endian_dword( snd_chn );
#endif
  fwrite( buff, 24, 1, snd );

  fwrite( str, len, 1, snd );

  /* Pad with zeroes until the next multiple of 8 */
  if( padding ) {
    const char zeros[7] = { 0 };
    fwrite( zeros, 8 - padding, 1, snd );
  }

  snd_header_ok = 1;
  printi( 1, "snd_write_auheader(): %dHz %c encoded %s\n", out_rte, snd_enc,
		 snd_chn == 2 ? "stereo" : "mono" );
  return 0;
}

int
snd_write_au( void )
{
  int err;

  if( !snd_header_ok && ( err = snd_write_auheader() ) ) 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_au(): %d samples (%d bytes) sound\n", snd_len/snd_fsz, snd_len );

  return 0;
}

void
snd_finalize_au( void )
{
  libspectrum_dword len;
  long pos = ftell( snd );

  if( fseek( snd, 8, SEEK_SET ) == -1 ) {
    printi( 2, "snd_finalize_au(): cannot finalize sound output file (not seekable).\n" );
  } else {
#ifdef WORDS_BIGENDIAN
    len = pos - 24 - 64;
#else
    len = swap_endian_dword( pos - 24 - 64 );
#endif
    fwrite( &len, 4, 1, snd );			/* data length */
    printi( 1, "snd_finalize_au(): DATA size: %ldbyte\n", pos - 24 - 64 );
  }
}