Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / icedax / sun.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)sun.c    1.4 01/10/27 Copyright 1998,1999 Heiko Eissfeldt */
14 /***
15  * CopyPolicy: GNU Public License 2 applies
16  * Copyright (C) by Heiko Eissfeldt
17  *
18  *
19  * ---------------------------------------------------------------------
20  *  definitions for sun pcm output
21  * ---------------------------------------------------------------------
22  */
23
24 #include "config.h"
25 #include "mytype.h"
26 #include <stdio.h>
27 #include <unixstd.h>
28 #include "byteorder.h"
29 #include "sndfile.h"
30
31 typedef struct SUNHDR {
32   unsigned int magic;                   /* dns. a la .snd */
33   unsigned int data_location;           /* offset to data */
34   unsigned int size;                    /* # of data bytes */
35   unsigned int format;                  /* format code */
36   unsigned int sample_rate;             /* in Hertz */
37   unsigned int channelcount;            /* 1 for mono, 2 for stereo */
38   char info[8];                         /* comments */
39 } SUNHDR;
40
41 static SUNHDR sunHdr;
42
43 static int InitSound(int audio, long channels, unsigned long rate, 
44                                                         long nBitsPerSample, unsigned long expected_bytes);
45
46 static int InitSound(int audio, long channels, unsigned long rate, 
47                                                         long nBitsPerSample, unsigned long expected_bytes)
48 {
49   unsigned long format = nBitsPerSample > 8 ? 0x03 : 0x02;
50
51   sunHdr.magic         = cpu_to_le32(UINT4_C(0x646e732e));
52   sunHdr.data_location = cpu_to_be32(0x20);
53   sunHdr.size          = cpu_to_be32(expected_bytes);
54   sunHdr.format        = cpu_to_be32(format);
55   sunHdr.sample_rate   = cpu_to_be32(rate);
56   sunHdr.channelcount  = cpu_to_be32(channels);
57
58   return write (audio, &sunHdr, sizeof (sunHdr));
59 }
60
61 static int ExitSound(int audio, unsigned long nBytesDone);
62
63 static int ExitSound(int audio, unsigned long nBytesDone)
64 {
65   sunHdr.size = cpu_to_be32(nBytesDone);
66
67   /* goto beginning */
68   if (lseek(audio, 0L, SEEK_SET) == -1) {
69     return 0;
70   }
71   return write (audio, &sunHdr, sizeof (sunHdr));
72 }
73
74 static unsigned long GetHdrSize(void);
75
76 static unsigned long GetHdrSize()
77 {
78   return sizeof( sunHdr );
79 }
80
81 static unsigned long InSizeToOutSize(unsigned long BytesToDo);
82
83 static unsigned long InSizeToOutSize(unsigned long BytesToDo)
84 {
85         return BytesToDo;
86 }
87
88 struct soundfile sunsound =
89 {
90         InitSound,              /* init header method */
91         ExitSound,              /* exit header method */
92         GetHdrSize,             /* report header size method */
93         /* get sound samples out */
94         (int (*)(int audio, unsigned char *buf, unsigned long BytesToDo)) write,
95         InSizeToOutSize,        /* compressed? output file size */
96         1                       /* needs big endian samples */
97 };
98
99