21
22
#include <string.h>
23
24
#include "common.h"
26
** Allocate and initialize a broadcast info structure.
30
broadcast_info_alloc (void)
31
{ SF_BROADCAST_INFO* bext ;
33
if ((bext = calloc (1, sizeof (SF_BROADCAST_INFO))) == NULL)
37
} /* broadcast_info_alloc */
26
static void strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
27
static int gen_coding_history (char * added_history, int added_history_max, const SF_INFO * psfinfo) ;
30
bc_min_size (const SF_BROADCAST_INFO* info)
34
return offsetof (SF_BROADCAST_INFO, coding_history) + info->coding_history_size ;
35
} /* broadcast_size */
39
bc_var_coding_hist_size (const SF_BROADCAST_VAR* var)
40
{ return var->size - offsetof (SF_BROADCAST_VAR, binfo.coding_history) ;
41
} /* broadcast_size */
44
broadcast_var_alloc (size_t datasize)
45
{ SF_BROADCAST_VAR * data ;
47
if ((data = calloc (1, datasize)) != NULL)
48
data->size = datasize ;
51
} /* broadcast_var_alloc */
40
broadcast_info_copy (SF_BROADCAST_INFO* dst, SF_BROADCAST_INFO* src)
41
{ memcpy (dst, src, sizeof (SF_BROADCAST_INFO)) ;
55
broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datasize)
56
{ char added_history [256] ;
57
int added_history_len, len ;
62
if (bc_min_size (info) > datasize)
63
{ psf->error = SFE_BAD_BROADCAST_INFO_SIZE ;
67
added_history_len = gen_coding_history (added_history, sizeof (added_history), &(psf->sf)) ;
69
if (psf->broadcast_var != NULL)
70
{ size_t coding_hist_offset = offsetof (SF_BROADCAST_INFO, coding_history) ;
72
if (psf->broadcast_var->binfo.coding_history_size + added_history_len < datasize - coding_hist_offset)
73
{ free (psf->broadcast_var) ;
74
psf->broadcast_var = NULL ;
78
if (psf->broadcast_var == NULL)
79
{ int size = datasize + added_history_len + 512 ;
81
psf->broadcast_var = calloc (1, size) ;
82
psf->broadcast_var->size = size ;
85
memcpy (&(psf->broadcast_var->binfo), info, offsetof (SF_BROADCAST_INFO, coding_history)) ;
87
strncpy_crlf (psf->broadcast_var->binfo.coding_history, info->coding_history, bc_var_coding_hist_size (psf->broadcast_var), info->coding_history_size) ;
88
len = strlen (psf->broadcast_var->binfo.coding_history) ;
90
if (len > 0 && psf->broadcast_var->binfo.coding_history [len] != '\n')
91
strncat (psf->broadcast_var->binfo.coding_history, "\r\n", 2) ;
93
if (psf->mode == SFM_WRITE)
94
strncat (psf->broadcast_var->binfo.coding_history, added_history, strlen (added_history)) ;
96
psf->broadcast_var->binfo.coding_history_size = strlen (psf->broadcast_var->binfo.coding_history) ;
98
/* Fore coding_history_size to be even. */
99
psf->broadcast_var->binfo.coding_history_size += (psf->broadcast_var->binfo.coding_history_size & 1) ? 1 : 0 ;
43
101
/* Currently writing this version. */
102
psf->broadcast_var->binfo.version = 1 ;
47
} /* broadcast_info_copy */
105
} /* broadcast_var_set */
50
broadcast_add_coding_history (SF_BROADCAST_INFO* bext, unsigned int channels, unsigned int samplerate)
109
broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize)
112
if (psf->broadcast_var == NULL)
115
size = SF_MIN (datasize, bc_min_size (&(psf->broadcast_var->binfo))) ;
117
memcpy (data, &(psf->broadcast_var->binfo), size) ;
120
} /* broadcast_var_set */
122
/*------------------------------------------------------------------------------
123
** Strncpy which converts all line endings to CR/LF.
127
strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
128
{ char * destend = dest + destmax - 1 ;
129
const char * srcend = src + srcmax ;
131
while (dest < destend && src < srcend)
132
{ if ((src [0] == '\r' && src [1] == '\n') || (src [0] == '\n' && src [1] == '\r'))
156
/* Make sure dest is terminated. */
161
gen_coding_history (char * added_history, int added_history_max, const SF_INFO * psfinfo)
51
162
{ char chnstr [16] ;
166
** From : http://www.sr.se/utveckling/tu/bwf/docs/codhist2.htm
168
** Parameter Variable string <allowed option> Unit
169
** ==========================================================================================
170
** Coding Algorithm A=<ANALOGUE, PCM, MPEG1L1, MPEG1L2, MPEG1L3,
171
** MPEG2L1, MPEG2L2, MPEG2L3>
172
** Sampling frequency F=<11000,22050,24000,32000,44100,48000> [Hz]
173
** Bit-rate B=<any bit-rate allowed in MPEG 2 (ISO/IEC [kbit/s per channel]
175
** Word Length W=<8, 12, 14, 16, 18, 20, 22, 24> [bits]
176
** Mode M=<mono, stereo, dual-mono, joint-stereo>
177
** Text, free string T=<a free ASCII-text string for in house use.
178
** This string should contain no commas (ASCII
179
** 2Chex). Examples of the contents: ID-No; codec
183
switch (psfinfo->channels)
63
192
strncpy (chnstr, "stereo", sizeof (chnstr)) ;
67
LSF_SNPRINTF (chnstr, sizeof (chnstr), "%uchn", channels) ;
71
count = LSF_SNPRINTF (bext->coding_history, sizeof (bext->coding_history), "F=%u,A=PCM,M=%s,W=24,T=%s-%s", samplerate, chnstr, PACKAGE, VERSION) ;
73
if (count >= SIGNED_SIZEOF (bext->coding_history))
74
bext->coding_history_size = sizeof (bext->coding_history) ;
76
{ count += count & 1 ;
77
bext->coding_history_size = count ;
81
} /* broadcast_add_coding_history */
84
** Do not edit or modify anything in this comment block.
85
** The following line is a file identity tag for the GNU Arch
86
** revision control system.
88
** arch-tag: 4b3b69c7-d710-4424-9da0-5048534a0beb
196
LSF_SNPRINTF (chnstr, sizeof (chnstr), "%uchn", psfinfo->channels) ;
200
switch (SF_CODEC (psfinfo->format))
201
{ case SF_FORMAT_PCM_U8 :
202
case SF_FORMAT_PCM_S8 :
205
case SF_FORMAT_PCM_16 :
208
case SF_FORMAT_PCM_24 :
211
case SF_FORMAT_PCM_32 :
214
case SF_FORMAT_FLOAT :
215
width = 24 ; /* Bits in the mantissa + 1 */
217
case SF_FORMAT_DOUBLE :
218
width = 53 ; /* Bits in the mantissa + 1 */
220
case SF_FORMAT_ULAW :
221
case SF_FORMAT_ALAW :
229
count = LSF_SNPRINTF (added_history, added_history_max,
230
"A=PCM,F=%u,W=%hu,M=%s,T=%s-%s\r\n",
231
psfinfo->samplerate, width, chnstr, PACKAGE, VERSION) ;
233
if (count >= added_history_max)
237
} /* gen_coding_history */