Download this file

wav.cpp    107 lines (91 with data), 3.1 kB

  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
/* Copyright (C) 2014 J.F.Dockes
* 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <string.h>
#include "wav.h"
inline int inttoichar4(unsigned char *cdb, unsigned int addr)
{
cdb[3] = (addr & 0xff000000) >> 24;
cdb[2] = (addr & 0x00ff0000) >> 16;
cdb[1] = (addr & 0x0000ff00) >> 8;
cdb[0] = addr & 0x000000ff;
return 4;
}
inline int inttoichar2(unsigned char *cdb, unsigned int cnt)
{
cdb[1] = (cnt & 0x0000ff00) >> 8;
cdb[0] = cnt & 0x000000ff;
return 2;
}
#if 0
// For reference: definition of a wav header
// Les valeurs en commentaires sont donnees pour du son 44100/16/2
struct wav_header {
/*0 */char riff[4]; /* = 'RIFF' */
/*4 */int32 rifflen; /* longueur des infos qui suivent= datalen+36 */
/*8 */char wave[4]; /* = 'WAVE' */
/*12*/char fmt[4]; /* = 'fmt ' */
/*16*/int32 fmtlen; /* = 16 */
/*20*/int16 formtag; /* = 1 : PCM */
/*22*/int16 nchan; /* = 2 : nombre de canaux */
/*24*/int32 sampspersec; /* = 44100 : Nbr d'echantillons par seconde */
/*28*/int32 avgbytpersec;/* = 176400 : Nbr moyen octets par seconde */
/*32*/int16 blockalign; /* = 4 : nombre d'octets par echantillon */
/*34*/int16 bitspersamp; /* = 16 : bits par echantillon */
/*36*/char data[4]; /* = 'data' */
/*40*/int32 datalen; /* Nombre d'octets de son qui suivent */
/*44*/char data[];
};
#endif /* if 0 */
#define WAVHSIZE 44
#define RIFFTOWAVCNT 36
// Format header. Note the use of intel format integers. Input buffer must
// be of size >= 44
int makewavheader(char *buf, int maxsize, int freq, int bits,
int chans, unsigned int databytecnt)
{
if (maxsize < WAVHSIZE)
return -1;
unsigned char *cp = (unsigned char *)buf;
memcpy(cp, "RIFF", 4);
cp += 4;
inttoichar4(cp, databytecnt + RIFFTOWAVCNT);
cp += 4;
memcpy(cp, "WAVE", 4);
cp += 4;
memcpy(cp, "fmt ", 4);
cp += 4;
inttoichar4(cp, 16);
cp += 4;
inttoichar2(cp, 1);
cp += 2;
inttoichar2(cp, chans);
cp += 2;
inttoichar4(cp, freq);
cp += 4;
inttoichar4(cp, freq * chans * (bits / 8));
cp += 4;
inttoichar2(cp, chans * bits / 8);
cp += 2;
inttoichar2(cp, bits);
cp += 2;
memcpy(cp, "data", 4);
cp += 4;
inttoichar4(cp, databytecnt);
cp += 4;
return WAVHSIZE;
}