~ubuntu-branches/ubuntu/breezy/muse/breezy

« back to all changes in this revision

Viewing changes to synti/stklib/AifWvOut.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2004-02-07 15:18:22 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040207151822-es27xxkzbcxkebjm
Tags: 0.6.3-1
* New upstream version.
* Added patches:
  + [10_alsa_init_fix] New, from upstream CVS.
    Initialize direction variable when setting Alsa parameters.
  + [10_canvas_translation_fix] New, from upstream CVS.
    Do not translate tooltips twice in canvas popup.
  + [10_checkbox_fix] New, from upstream CVS.
    Use proper set/test methods on metronome checkboxes.
  + [10_html_doc_cleanup] New.
    Fix links and HTML errors in documentation.
  + [20_allow_system_timer] New.
    The new upstream version fails by default if the real-time clock
    could not be accessed (usually the case when not running suid-root).
    This patch reverts the old behaviour of falling back to the more
    inaccurate system timer.
* Updated patches:
  + [11_PIC_fixes_fixup] Rediffed.
* Removed patches:
  + [20_no_atomic_asm] Merged upstream.
* debian/compat: Splice out debhelper compatibility level from rules file.
* debian/control: Build-depend on latest jack release by default.
  Closes: #228788
* debian/control: Bump standards version.
* debian/control: Use auto-generated debconf dependency via misc:Depends.
* debian/control: Minor tweaks to the long description.
* debian/control: Tighten fluidsynth build dependency to sane version.
* debian/muse.doc-base: New. Register HTML documentation with doc-base.
* debian/templates: Tiny rewording, and typo fix.
* debian/templates, debian/po/*: Switch to po-debconf for translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************/
2
 
/*
3
 
  AifWvOut Output Class,
4
 
  by Gary P. Scavone, 2000
5
 
 
6
 
  This object inherits from WvOut and is
7
 
  used to write Audio Interchange File
8
 
  Format files with 16-bit data (signed
9
 
  integer).
10
 
 
11
 
  .aif files are always bif-endian.
12
 
*/
13
 
/*******************************************/
14
 
 
15
 
#include "AifWvOut.h"
16
 
#ifdef __LITTLE_ENDIAN__
17
 
  #include "ByteSwap.h"
18
 
#endif
19
 
 
20
 
/********  Aiff Soundfile Header Struct   *******/
21
 
struct aiffhdr {
22
 
  char form[4];                  // "FORM"
23
 
  INT32 form_size;               // in bytes
24
 
  char aiff[4];                  // "AIFF"
25
 
  char comm[4];                  // "COMM"
26
 
  INT32 comm_size;               // "COMM" chunk size (should be 18)
27
 
  INT16 num_chans;               // number of channels
28
 
  unsigned long sample_frames;    // sample frames of audio data
29
 
  INT16 sample_size;             // always 16 for STK
30
 
  unsigned char srate[10];        // IEEE 754 floating point format
31
 
  char  ssnd[4];                 // "SSND"
32
 
  INT32 ssnd_size;               // "SSND" chunk size
33
 
  unsigned long offset;           // data offset in data block (should be 0)
34
 
  unsigned long block_size;       // not used by STK (should be 0)
35
 
};
36
 
 
37
 
FILE *openAifFile(int chans, char *fileName)    {
38
 
  struct aiffhdr hdr = {"FOR",46,"AIF","COM",18,0,0,16,"0","SSN",8,0,0};
39
 
  char tempName[128];
40
 
  FILE *fd;
41
 
  char msg[256];
42
 
  INT16 i;
43
 
  unsigned long exp;
44
 
  unsigned long rate = (unsigned long) SRATE;
45
 
 
46
 
  hdr.form[3] = 'M';
47
 
  hdr.aiff[3] = 'F';
48
 
  hdr.comm[3] = 'M';
49
 
  hdr.ssnd[3] = 'D';
50
 
 
51
 
  hdr.num_chans = chans;
52
 
 
53
 
  /*
54
 
   * For AIFF files, the sample reate is stored in a 10-byte, IEEE Standard
55
 
   * 754 floating point number, so we need to convert to that.
56
 
   */
57
 
  memset(hdr.srate, 0, 10);
58
 
  exp = rate;
59
 
  for (i=0; i<32; i++) {
60
 
    exp >>= 1;
61
 
    if (!exp) break;
62
 
  }
63
 
  i += 16383;
64
 
#ifdef __LITTLE_ENDIAN__
65
 
  swap16((unsigned char *)&i);
66
 
#endif
67
 
  *(INT16 *)(hdr.srate) = (INT16) i;
68
 
 
69
 
  for (i=32; i; i--) {
70
 
    if (rate & 0x80000000) break;
71
 
    rate <<= 1;
72
 
  }
73
 
 
74
 
#ifdef __LITTLE_ENDIAN__
75
 
  swap32((unsigned char *)&rate);
76
 
#endif
77
 
  *(unsigned long *)(hdr.srate+2) = (unsigned long) rate;
78
 
    
79
 
  strcpy(tempName,fileName);
80
 
  if (strstr(tempName,".aif") == NULL) strcat(tempName,".aif");
81
 
  fd = fopen(tempName,"wb");
82
 
  if (!fd) {
83
 
    sprintf(msg, "AifWvOut: Could not create soundfile: %s\n", tempName);
84
 
    throw StkError(msg, StkError::FILE_ERROR);
85
 
  }
86
 
 
87
 
#ifdef __LITTLE_ENDIAN__
88
 
  swap32((unsigned char *)&hdr.form_size);
89
 
  swap32((unsigned char *)&hdr.comm_size);
90
 
  swap16((unsigned char *)&hdr.num_chans);
91
 
  swap16((unsigned char *)&hdr.sample_size);
92
 
  swap32((unsigned char *)&hdr.ssnd_size);
93
 
  swap32((unsigned char *)&hdr.offset);
94
 
  swap32((unsigned char *)&hdr.block_size);
95
 
#endif
96
 
 
97
 
  printf("\nCreating soundfile: %s\n", tempName);
98
 
 
99
 
  /* I found it necessary to break the fwrite() calls as
100
 
   * follows ... a single write of 54 bytes didn't work.
101
 
   */
102
 
  fwrite(&hdr,4,5,fd);
103
 
  fwrite(&hdr.num_chans,2,1,fd);
104
 
  fwrite(&hdr.sample_frames,4,1,fd);
105
 
  fwrite(&hdr.sample_size,2,1,fd);
106
 
  fwrite(&hdr.srate,10,1,fd);
107
 
  fwrite(&hdr.ssnd,4,4,fd);
108
 
 
109
 
  return fd;
110
 
}
111
 
 
112
 
AifWvOut :: AifWvOut(char *fileName, int chans)
113
 
{
114
 
  char msg[256];
115
 
  if (chans < 1) {
116
 
    sprintf(msg, "AifWvOut: number of channels = %d not supported!\n", chans);
117
 
    throw StkError(msg, StkError::FUNCTION_SYNTAX);
118
 
  }
119
 
  channels = chans;
120
 
  fd = openAifFile(chans,fileName);
121
 
  data_length = FILE_BUFFER_SIZE*channels;
122
 
  data = (INT16 *) new INT16[data_length];
123
 
}
124
 
 
125
 
AifWvOut :: ~AifWvOut()
126
 
{
127
 
  MY_FLOAT time;
128
 
  unsigned long bytes;
129
 
  unsigned long frames;
130
 
 
131
 
  fwrite(data,2,counter,fd);
132
 
  time = (double) totalCount * ONE_OVER_SRATE;
133
 
  printf("%f Seconds Computed\n\n", time);
134
 
 
135
 
  frames = (unsigned long) totalCount;
136
 
#ifdef __LITTLE_ENDIAN__
137
 
  swap32((unsigned char *)&frames);
138
 
#endif
139
 
  fseek(fd,22,SEEK_SET); // jump to "COMM" sample_frames
140
 
  fwrite(&frames,4,1,fd);
141
 
 
142
 
  bytes = totalCount*2*channels + 46;
143
 
#ifdef __LITTLE_ENDIAN__
144
 
  swap32((unsigned char *)&bytes);
145
 
#endif
146
 
  fseek(fd,4,SEEK_SET); // jump to file size
147
 
  fwrite(&bytes,4,1,fd);
148
 
 
149
 
  bytes = totalCount*2*channels + 8;
150
 
#ifdef __LITTLE_ENDIAN__
151
 
  swap32((unsigned char *)&bytes);
152
 
#endif
153
 
  fseek(fd,42,SEEK_SET); // jump to "SSND" chunk size
154
 
  fwrite(&bytes,4,1,fd);
155
 
 
156
 
  fclose(fd);
157
 
}
158
 
 
159
 
void AifWvOut :: tick(MY_FLOAT sample)
160
 
{
161
 
  INT16 isample;
162
 
 
163
 
  isample = (INT16) (sample * 32000.0);
164
 
#ifdef __LITTLE_ENDIAN__
165
 
  swap16((unsigned char *)&isample);
166
 
#endif
167
 
  for (int i=0;i<channels;i++)
168
 
    data[counter++] = isample;
169
 
 
170
 
  totalCount++;
171
 
  if (counter == data_length) {
172
 
    fwrite(data,2,data_length,fd);
173
 
    counter = 0;
174
 
  }
175
 
}
176
 
 
177
 
void AifWvOut :: mtick(MY_MULTI samples)
178
 
{
179
 
  for (int i=0;i<channels;i++) {
180
 
    data[counter] = (INT16) (*samples++ * 32000.0);
181
 
#ifdef __LITTLE_ENDIAN__
182
 
    swap16 ((unsigned char *)&data[counter]);
183
 
#endif
184
 
    counter++;
185
 
  }
186
 
 
187
 
  totalCount++;
188
 
  if (counter == data_length) {
189
 
    fwrite(data,2,data_length,fd);
190
 
    counter = 0;
191
 
  }
192
 
}