sox-devel@lists.sourceforge.net unofficial mirror
 help / color / mirror / code / Atom feed
blob e9f128c5378127299e0aa8023404ee1a2adddd8b 5067 bytes (raw)
name: src/nsp.c 	 # note: path name is non-authoritative(*)

  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
174
175
176
177
178
 
/* libSoX CSL NSP format.
 * http://web.archive.org/web/20160525045942/http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/CSL/CSL.html
 *
 * Copyright 2017 Jim O'Regan
 *
 * based on aiff.c
 * Copyright 1991-2007 Guido van Rossum And Sundry Contributors
 *
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained.
 * Guido van Rossum And Sundry Contributors are not responsible for
 * the consequences of using this software.
 */

#include "sox_i.h"

#include <time.h>      /* for time stamping comments */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>

int lsx_nspstartread(sox_format_t * ft);

int lsx_nspstartread(sox_format_t * ft)
{
  char buf[5];
  uint32_t hchunksize;
  uint32_t chunksize;
  unsigned short channels = 0;
  sox_encoding_t enc = SOX_ENCODING_SIGN2;
  unsigned short bits = 16;
  double rate = 0.0;
  uint64_t seekto = 0;
  int i;
  size_t ssndsize = 0;

  char date[20];
  char *comment;
  uint16_t maxabschan[8];
  uint32_t datalength;
  uint32_t samplerate;
  int numchannels;

  uint8_t trash8;

  /* FORM chunk */
  if (lsx_reads(ft, buf, (size_t)8) == SOX_EOF || strncmp(buf, "FORMDS16", (size_t)8) != 0) {
    lsx_fail_errno(ft,SOX_EHDR,"NSP header does not begin with magic word `FORMDS16'");
    return(SOX_EOF);
  }
  lsx_readdw(ft, &hchunksize);

  while (1) {
    if (lsx_reads(ft, buf, (size_t)4) == SOX_EOF) {
      if (ssndsize > 0)
        break;
      else {
        lsx_fail_errno(ft,SOX_EHDR,"Missing SDA_ chunk in NSP file");
        return(SOX_EOF);
      }
    }
    if (strncmp(buf, "HEDR", (size_t)4) == 0) {
      /* HEDR chunk */
      lsx_readdw(ft, &chunksize);
      lsx_reads(ft, date, (size_t)20);
      lsx_readdw(ft, &samplerate);
      rate = (double)samplerate;
      lsx_readdw(ft, &datalength);
      lsx_readw(ft, &maxabschan[0]);
      lsx_readw(ft, &maxabschan[1]);

      /* Most likely there will only be 1 channel, but there can be 2 here */
      if (maxabschan[0] == 0xffff && maxabschan[1] == 0xffff) {
        lsx_fail_errno(ft,SOX_EHDR,"Channels A and B undefined");
      } else if (maxabschan[0] == 0xffff || maxabschan[1] == 0xffff) {
        ft->signal.channels = 1;
      } else {
        ft->signal.channels = 2;
      }
    } else if (strncmp(buf, "HDR8", (size_t)4) == 0) {
      /* HDR8 chunk */
      lsx_readdw(ft, &chunksize);
      lsx_reads(ft, date, (size_t)20);
      lsx_readdw(ft, &samplerate);
      rate = (double)samplerate;
      lsx_readdw(ft, &datalength);
      lsx_readw(ft, &maxabschan[0]);
      lsx_readw(ft, &maxabschan[1]);
      lsx_readw(ft, &maxabschan[2]);
      lsx_readw(ft, &maxabschan[3]);
      lsx_readw(ft, &maxabschan[4]);
      lsx_readw(ft, &maxabschan[5]);
      lsx_readw(ft, &maxabschan[6]);
      lsx_readw(ft, &maxabschan[7]);

      /* Can be up to 8 channels */
      numchannels = 0;
      for (i = 0; i < 7; i++) {
        if (maxabschan[i] != 0xffff) {
          numchannels++;
        }
      }
      if (numchannels == 0) {
        lsx_fail_errno(ft,SOX_EHDR,"No channels defined");
      }
      ft->signal.channels = numchannels;
    } else if (strncmp(buf, "NOTE", (size_t)4) == 0) {
      unsigned char nullc = 0;
      /* NOTE chunk */
      lsx_readdw(ft, &chunksize);
      comment = lsx_malloc(chunksize * sizeof(char*));
      lsx_reads(ft, comment, (size_t)chunksize);
      if(strlen(comment) != 0)
        lsx_debug("NSP comment: %s %d", comment);
      free(comment);
      lsx_readb(ft, &nullc);
    } else if (strncmp(buf, "SDA_", (size_t)4) == 0) {
      lsx_readdw(ft, &chunksize);
      ssndsize = chunksize;
      /* if can't seek, just do sound now */
      if (!ft->seekable)
        break;
      /* else, seek to end of sound and hunt for more */
      seekto = lsx_tell(ft);
      lsx_seeki(ft, (off_t)chunksize, SEEK_CUR);
    } else {
      if (lsx_eof(ft))
        break;
      buf[4] = 0;
      lsx_debug("NSPstartread: ignoring `%s' chunk", buf);
      lsx_readdw(ft, &chunksize);
      if (lsx_eof(ft))
        break;
      /* Skip the chunk using lsx_readb() so we may read
         from a pipe */
      while (chunksize-- > 0) {
        if (lsx_readb(ft, &trash8) == SOX_EOF)
          break;
      }
    }
    if (lsx_eof(ft))
      break;
  }

  if (ft->seekable) {
    if (seekto > 0)
      lsx_seeki(ft, seekto, SEEK_SET);
    else {
      lsx_fail_errno(ft,SOX_EOF,"NSP: no sound data on input file");
      return(SOX_EOF);
    }
  }

  return lsx_check_read_params(
      ft, channels, rate, enc, bits, (uint64_t)ssndsize/2, sox_false);
}

static int lsx_nspstopread(sox_format_t * ft)
{
    ft->sox_errno = SOX_SUCCESS;

    return SOX_SUCCESS;
}

LSX_FORMAT_HANDLER(nsp)
{
  static char const * const names[] = {"nsp", NULL };
  static sox_format_handler_t const handler = {SOX_LIB_VERSION_CODE,
    "Computerized Speech Lab NSP file",
    names, SOX_FILE_LIT_END,
    lsx_nspstartread, lsx_rawread, NULL,
    NULL, NULL, NULL,
    NULL, NULL, NULL, 0
  };
  return &handler;
}

debug log:

solving e9f128c5 ...
found e9f128c5 in https://public-inbox.org/sox-devel/CAM6T9X-sgFUgoKjcmmbGz=2F+C5ei_fa_EqfKFbaweRRpcv1_Q@mail.gmail.com/ ||
	https://public-inbox.org/sox-devel/CAM6T9X-cYCvfXGJ7HD5HrBATiNuMSeu5Q3iPn0zATP8KZ9hEbA@mail.gmail.com/

applying [1/1] https://public-inbox.org/sox-devel/CAM6T9X-sgFUgoKjcmmbGz=2F+C5ei_fa_EqfKFbaweRRpcv1_Q@mail.gmail.com/
diff --git a/src/nsp.c b/src/nsp.c
new file mode 100644
index 00000000..e9f128c5

Checking patch src/nsp.c...
Applied patch src/nsp.c cleanly.

skipping https://public-inbox.org/sox-devel/CAM6T9X-cYCvfXGJ7HD5HrBATiNuMSeu5Q3iPn0zATP8KZ9hEbA@mail.gmail.com/ for e9f128c5
index at:
100644 e9f128c5378127299e0aa8023404ee1a2adddd8b	src/nsp.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/sox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).