File: Sequencer2.C

package info (click to toggle)
gmod 3.1-2
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 1,348 kB
  • ctags: 808
  • sloc: cpp: 7,755; makefile: 82
file content (188 lines) | stat: -rw-r--r-- 4,311 bytes parent folder | download | duplicates (7)
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
179
180
181
182
183
184
185
186
187
188
// -*-C++-*-
// This file is part of the gmod package
// Copyright (C) 1997 by Andrew J. Robinson

#include <stdlib.h>
#include <string.h>
#include <asm/errno.h>

#ifdef USE_LOCAL
#include "soundcard.h"
#else
#include <sys/soundcard.h>
#endif

#include "Sample.h"
#include "Sequencer.h"

int
Sequencer::patchLoad(FILE * modFd, int sampleNum, Sample &samp,
		     int &bytesRead, int cutFactor) const
{
  int xBytesRead;
  int len;
  unsigned short loopFlags;
  int loopStart;
  int loopEnd;
  int i, rc;
  struct patch_info tpatch;
  struct patch_info *patch;

  memset(&tpatch, 0, sizeof(patch_info));
  samp.getPatchInfo(tpatch);
  patch = (struct patch_info *)malloc(sizeof(struct patch_info) +
				      tpatch.len + 2);

  memcpy(patch, &tpatch, sizeof(struct patch_info));
  patch->key = GUS_PATCH;
  patch->low_note = 0;
  patch->high_note = 0x7fffffff;
  patch->volume = 0;
  patch->panning = -112;
  patch->device_no = gusDev_;
  patch->instr_no = sampleNum;

  len = patch->len;

  /* fix loopEnd if necessary */

  if (patch->loop_end > len)
    patch->loop_end = len;

  loopStart = patch->loop_start;
  loopEnd = patch->loop_end;

  if (loopStart >= loopEnd)
    patch->mode &= ~WAVE_LOOPING;

  loopFlags = patch->mode;

  if ((bytesRead = fread(patch->data, 1, len, modFd)) != len)
    {
      if (((loopFlags & WAVE_16_BITS) && (bytesRead > 1)) ||
	  (!(loopFlags & WAVE_16_BITS) && (bytesRead > 0)))
	{
	  /* Warning: Short sample */

	  xBytesRead = bytesRead;

	  if ((loopFlags & WAVE_16_BITS) && ((bytesRead % 2) != 0))
	    xBytesRead--;
	  
	  i = xBytesRead - (len - bytesRead);

	  if (i < 0)
	    i = 0;

	  if ((loopFlags & WAVE_16_BITS) && ((i % 2) != 0))
	    i--;

	  memcpy(patch->data + xBytesRead, patch->data + i, len - xBytesRead);
	}
      else
	{
	  free(patch);
	  return -ENODATA;
	}
    }

  /* "decode" sample if necessary */
  samp.decode(patch->data);

  /* convert 16 bits to 8 if necessary */

  if ((loopFlags & WAVE_16_BITS) && ((len > 256*1024) || (cutFactor > 1)))
    {
      for (i = 0; i < len - 1; i+= 2)
	patch->data[i / 2] = patch->data[i + 1];
      
      len = (patch->len /= 2);
      loopStart = (patch->loop_start /= 2);
      loopEnd = (patch->loop_end /= 2);
      loopFlags &= ~WAVE_16_BITS;
      patch->base_freq /= 2;
    }

  /* extend looped samples, since length might equal loop end */

  if (loopFlags & WAVE_LOOPING)
    {
      if (loopFlags & WAVE_16_BITS)
	{
	  if (len >= 2)
	    {
	      patch->data[len] = patch->data[len - 2];
	      patch->data[len + 1] = patch->data[len - 1];
	      len += 2;
	      patch->len += 2;
	    }
	}
      else if (len >= 1)
	{
	  patch->data[len] = patch->data[len - 1];
	  len++;
	  (patch->len)++;
	}
    }

  /* Shorten samples to fit in memory.  Do not shorten a looped sample if */
  /* the start and end are not evenly divisible by cutFactor; otherwise, */
  /* the sample may go out of tune. */

  if ((cutFactor == 2) && (loopFlags & WAVE_LOOPING) && (loopStart % 2) &&
      (loopEnd % 2))
    {
      loopStart = (patch->loop_start -= 1);
      loopEnd = (patch->loop_end -= 1);
      i = 2;
    }
  else
    i = 1;

  if ((cutFactor > 1) && !((loopFlags & WAVE_LOOPING) &&
			   ((loopStart % cutFactor) ||
			    (loopEnd % cutFactor))))
    {
      samp.cutFactor(i * cutFactor);

      for (i = 0; i < len / cutFactor; i++)
	patch->data[i] = patch->data[i * cutFactor];
      
      len = (patch->len /= cutFactor);
      loopStart = (patch->loop_start /= cutFactor);
      loopEnd = (patch->loop_end /= cutFactor);
      patch->base_freq /= cutFactor;
    }
  else
    samp.cutFactor(i);

  /* try to remove loop clicking */

  if ((loopFlags & WAVE_LOOPING) && (loopEnd >= 2) &&
      !(loopFlags & WAVE_16_BITS))
    {
      patch->data[loopEnd] = patch->data[loopStart];

      if (loopFlags & WAVE_UNSIGNED)
	patch->data[loopEnd - 1] =
	  ((unsigned char)(patch->data[loopEnd - 2]) +
	   (unsigned char)(patch->data[loopEnd])) / 2;
      else
	patch->data[loopEnd - 1] =
	  ((signed char)(patch->data[loopEnd - 2]) +
	   (signed char)(patch->data[loopEnd])) / 2;
    }

  if (memory() < len)
    rc = -ENOSPC;
  else
    {
#define write ::write
#define seqfd seqfd_
      rc = SEQ_WRPATCH2(patch, sizeof(struct patch_info) + len);
#undef write
    }

  free(patch);
  return rc;
}