File: mode_gen.c

package info (click to toggle)
shntool 3.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,220 kB
  • ctags: 1,118
  • sloc: ansic: 8,515; sh: 3,761; makefile: 36
file content (173 lines) | stat: -rw-r--r-- 4,570 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*  mode_gen.c - gen mode module
 *  Copyright (C) 2000-2009  Jason Jordan <shnutils@freeshell.org>
 *
 *  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.
 */

#include <string.h>
#include "mode.h"

CVSID("$Id: mode_gen.c,v 1.53 2009/03/16 04:46:03 jason Exp $")

static bool gen_main(int,char **);
static void gen_help(void);

mode_module mode_gen = {
  "gen",
  "shngen",
  "Generates CD-quality PCM WAVE data files containing silence",
  CVSIDSTR,
  TRUE,
  gen_main,
  gen_help
};

#define GEN_PREFIX "silence"

static char *gen_size = NULL;

static void gen_help()
{
  st_info("Usage: %s [OPTIONS]\n",st_progname());
  st_info("\n");
  st_info("Mode-specific options:\n");
  st_info("\n");
  st_info("  -h      show this help screen\n");
  st_info("  -l len  generate files containing silence of length len (*)\n");
  st_info("\n");
  st_info("          (*) len must be in bytes, m:ss, m:ss.ff or m:ss.nnn format\n");
  st_info("\n");
}

static void parse(int argc,char **argv)
{
  int c;

  st_ops.output_directory = CURRENT_DIR;
  st_ops.output_prefix = GEN_PREFIX;

  while ((c = st_getopt(argc,argv,"l:")) != -1) {
    switch (c) {
      case 'l':
        if (NULL == optarg)
          st_error("missing silence length");
        gen_size = optarg;
        break;
    }
  }

  if (NULL == gen_size)
    st_error("length must be specified");
}

static bool process()
{
  wave_info *info;
  unsigned char header[CANONICAL_HEADER_SIZE],silence[XFER_SIZE];
  char outfilename[FILENAME_SIZE];
  FILE *output;
  proc_info output_proc;
  wlong gen_bytes,bytes_left,bytes;
  progress_info proginfo;
  bool success;

  success = FALSE;

  if (NULL == (info = new_wave_info(NULL))) {
    st_error("could not allocate memory for WAVE info");
  }

  info->rate = CD_RATE;

  gen_bytes = smrt_parse((unsigned char *)gen_size,info);

  create_output_filename("","",outfilename);

  info->channels = CD_CHANNELS;
  info->samples_per_sec = CD_SAMPLES_PER_SEC;
  info->avg_bytes_per_sec = CD_RATE;
  info->block_align = CD_BLOCK_ALIGN;
  info->bits_per_sample = CD_BITS_PER_SAMPLE;
  info->wave_format = WAVE_FORMAT_PCM;
  info->rate = CD_RATE;

  info->data_size = gen_bytes;
  info->chunk_size = info->data_size + CANONICAL_HEADER_SIZE - 8;
  info->length = info->data_size / (wlong)info->rate;
  info->exact_length = (double)info->data_size / (double)info->rate;

  if (PROB_ODD_SIZED_DATA(info))
    info->chunk_size++;

  make_canonical_header(header,info);

  length_to_str(info);

  bytes_left = gen_bytes;

  memset((void *)silence,0,XFER_SIZE);

  proginfo.initialized = FALSE;
  proginfo.prefix = "Generating";
  proginfo.clause = NULL;
  proginfo.filename1 = NULL;
  proginfo.filedesc1 = NULL;
  proginfo.filename2 = outfilename;
  proginfo.filedesc2 = info->m_ss;
  proginfo.bytes_total = bytes_left + CANONICAL_HEADER_SIZE;

  prog_update(&proginfo);

  if (NULL == (output = open_output_stream(outfilename,&output_proc)))
    st_error("could not open output file: [%s]",outfilename);

  if (write_n_bytes(output,header,CANONICAL_HEADER_SIZE,&proginfo) != CANONICAL_HEADER_SIZE) {
    prog_error(&proginfo);
    st_error("error while writing %d-byte WAVE header",CANONICAL_HEADER_SIZE);
  }

  while (bytes_left > 0) {
    bytes = min(bytes_left,XFER_SIZE);

    if (write_n_bytes(output,silence,bytes,&proginfo) != bytes) {
      prog_error(&proginfo);
      st_error("error while writing %d-byte chunk of silence",bytes);
    }

    bytes_left -= bytes;
  }

  if (PROB_ODD_SIZED_DATA(info) && (1 != write_padding(output,1,&proginfo))) {
    prog_error(&proginfo);
    st_error("error while NULL-padding odd-sized data chunk");
  }

  success = TRUE;

  prog_success(&proginfo);

  if (CLOSE_CHILD_ERROR_OUTPUT == close_output(output,output_proc)) {
    success = FALSE;
  }

  return success;
}

static bool gen_main(int argc,char **argv)
{
  parse(argc,argv);

  return process();
}