Switch to unified view

a b/src/wav.cpp
1
/* Copyright (C) 2014 J.F.Dockes
2
 *     This program is free software; you can redistribute it and/or modify
3
 *     it under the terms of the GNU General Public License as published by
4
 *     the Free Software Foundation; either version 2 of the License, or
5
 *     (at your option) any later version.
6
 *
7
 *     This program is distributed in the hope that it will be useful,
8
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *     GNU General Public License for more details.
11
 *
12
 *     You should have received a copy of the GNU General Public License
13
 *     along with this program; if not, write to the
14
 *     Free Software Foundation, Inc.,
15
 *     59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
18
#include "config.h"
19
20
#include <string.h>
21
22
#include "wav.h"
23
24
inline int inttoichar4(unsigned char *cdb, unsigned int addr)
25
{
26
    cdb[3] = (addr & 0xff000000) >> 24;
27
    cdb[2] = (addr & 0x00ff0000) >> 16;
28
    cdb[1] = (addr & 0x0000ff00) >> 8;
29
    cdb[0] =  addr & 0x000000ff;
30
    return 4;
31
}
32
33
inline int inttoichar2(unsigned char *cdb, unsigned int cnt)
34
{
35
    cdb[1] = (cnt & 0x0000ff00) >> 8;
36
    cdb[0] =  cnt & 0x000000ff;
37
    return 2;
38
}
39
40
41
#if 0
42
// For reference: definition of a wav header
43
// Les valeurs en commentaires sont donnees pour du son 44100/16/2
44
struct wav_header {
45
    /*0 */char  riff[4];     /* = 'RIFF' */
46
    /*4 */int32 rifflen;     /* longueur des infos qui suivent= datalen+36 */
47
    /*8 */char  wave[4];     /* = 'WAVE' */
48
49
    /*12*/char  fmt[4];      /* = 'fmt ' */
50
    /*16*/int32 fmtlen;      /* = 16 */
51
    /*20*/int16 formtag;     /* = 1 : PCM */
52
    /*22*/int16 nchan;       /* = 2 : nombre de canaux */
53
    /*24*/int32 sampspersec; /* = 44100 : Nbr d'echantillons par seconde */
54
    /*28*/int32 avgbytpersec;/* = 176400 : Nbr moyen octets par seconde */
55
    /*32*/int16 blockalign;  /* = 4 : nombre d'octets par echantillon */
56
    /*34*/int16 bitspersamp; /* = 16 : bits par echantillon */
57
58
    /*36*/char  data[4];     /* = 'data' */
59
    /*40*/int32 datalen;     /* Nombre d'octets de son qui suivent */
60
    /*44*/char data[];
61
};
62
#endif /* if 0 */
63
64
#define WAVHSIZE 44
65
#define RIFFTOWAVCNT 36
66
67
// Format header. Note the use of intel format integers. Input buffer must 
68
// be of size >= 44
69
int makewavheader(char *buf, int maxsize, int freq, int bits, 
70
                  int chans, unsigned int databytecnt)
71
{
72
    if (maxsize < WAVHSIZE)
73
        return -1;
74
75
    unsigned char *cp = (unsigned char *)buf;
76
    memcpy(cp, "RIFF", 4);
77
    cp += 4;
78
    inttoichar4(cp, databytecnt + RIFFTOWAVCNT);
79
    cp += 4;
80
    memcpy(cp, "WAVE", 4);
81
    cp += 4;
82
83
    memcpy(cp, "fmt ", 4);
84
    cp += 4;
85
    inttoichar4(cp, 16);
86
    cp += 4;
87
    inttoichar2(cp, 1);
88
    cp += 2;
89
    inttoichar2(cp, chans);
90
    cp += 2;
91
    inttoichar4(cp, freq);
92
    cp += 4;
93
    inttoichar4(cp, freq * chans * (bits / 8));
94
    cp += 4;
95
    inttoichar2(cp, chans * bits / 8);
96
    cp += 2;
97
    inttoichar2(cp, bits);
98
    cp += 2;
99
100
    memcpy(cp, "data", 4);
101
    cp += 4;
102
    inttoichar4(cp, databytecnt);
103
    cp += 4;
104
105
    return WAVHSIZE;
106
}