~ubuntu-branches/ubuntu/oneiric/muse/oneiric

« back to all changes in this revision

Viewing changes to synti/stklib/WavWvOut.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-23 17:28:23 UTC
  • Revision ID: james.westby@ubuntu.com-20020423172823-w8yplzr81a759xa3
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************/
 
2
/*  Wave file Output Class,                */
 
3
/*  by Perry R. Cook, 1995-96              */
 
4
/*  revised by Gary P. Scavone, 1999       */
 
5
/*                                         */
 
6
/*  This Object opens a DOS/Windows .wav   */
 
7
/*  16bit data (signed integers) file, and */
 
8
/*  poke buffers of samples into it.       */
 
9
/*******************************************/
 
10
 
 
11
#include "WavWvOut.h"
 
12
#ifndef __LITTLE_ENDIAN__
 
13
  #include "ByteSwap.h"
 
14
#endif
 
15
 
 
16
/********  Wav Soundfile Header Struct   *******/
 
17
struct wavehdr {
 
18
  char  riff[4];          // "RIFF"
 
19
  INT32  file_size;       // in bytes
 
20
  char  wave[4];          // "WAVE"
 
21
  char  fmt[4];           // "fmt "
 
22
  INT32  block_size;      // in bytes (16 for PCM)
 
23
  INT16 format_tag;       // 1=PCM, 257=Mu-Law, 258=A-Law, 259=ADPCM
 
24
  INT16 num_chans;        // 1=mono, 2=stereo
 
25
  INT32  srate;
 
26
  INT32  bytes_per_sec;
 
27
  INT16 bytes_per_samp;   // 2=16-bit mono, 4=16-bit stereo
 
28
  INT16 bits_per_samp;
 
29
  char  data[4];          // "data"
 
30
  INT32  dlength;         // in bytes
 
31
};
 
32
 
 
33
FILE *openWAVFile(int chans,char *fileName)     {
 
34
  struct wavehdr hdr = {"RIF",44,"WAV","fmt",
 
35
                                                16,1,1,(INT32) SRATE,(INT32) SRATE*2,2,16,"dat",0};
 
36
  char tempName[128];
 
37
  FILE *fd;
 
38
  char msg[256];
 
39
 
 
40
  hdr.riff[3] = 'F';
 
41
  hdr.wave[3] = 'E';
 
42
  hdr.fmt[3]  = ' ';
 
43
  hdr.data[3] = 'a';
 
44
    
 
45
  strcpy(tempName,fileName);
 
46
  if (strstr(tempName,".wav") == NULL) strcat(tempName,".wav");
 
47
  hdr.num_chans = chans;
 
48
  hdr.bytes_per_sec = (long) SRATE*2*chans;
 
49
  hdr.bytes_per_samp = 2*chans;
 
50
  hdr.bits_per_samp = 16;
 
51
  fd = fopen(tempName,"wb");
 
52
  if (!fd) {
 
53
    sprintf(msg, "WavWvOut: Could not create soundfile: %s\n", tempName);
 
54
    throw StkError(msg, StkError::FILE_ERROR);
 
55
  }
 
56
 
 
57
#ifndef __LITTLE_ENDIAN__
 
58
  swap32((unsigned char *)&hdr.file_size);
 
59
  swap32((unsigned char *)&hdr.block_size);
 
60
  swap16((unsigned char *)&hdr.format_tag);
 
61
  swap16((unsigned char *)&hdr.num_chans);
 
62
  swap32((unsigned char *)&hdr.srate);
 
63
  swap32((unsigned char *)&hdr.bytes_per_sec);
 
64
  swap16((unsigned char *)&hdr.bytes_per_samp);
 
65
  swap16((unsigned char *)&hdr.bits_per_samp);
 
66
#endif
 
67
 
 
68
  printf("\nCreating soundfile: %s\n", tempName);
 
69
  fwrite(&hdr,4,11,fd);
 
70
  return fd;
 
71
}
 
72
 
 
73
WavWvOut :: WavWvOut(char *fileName, int chans)
 
74
{
 
75
  char msg[256];
 
76
  if (chans < 1) {
 
77
    sprintf(msg, "WavWvOut: number of channels = %d not supported!\n", chans);
 
78
    throw StkError(msg, StkError::FUNCTION_SYNTAX);
 
79
  }
 
80
  channels = chans;
 
81
  fd = openWAVFile(chans,fileName);
 
82
  data_length = FILE_BUFFER_SIZE*channels;
 
83
  data = (INT16 *) new INT16[data_length];
 
84
}
 
85
 
 
86
WavWvOut :: ~WavWvOut()
 
87
{
 
88
  MY_FLOAT time;
 
89
  INT32 bytes;
 
90
 
 
91
  fwrite(data,2,counter,fd);
 
92
  time = (double) totalCount * ONE_OVER_SRATE;
 
93
  printf("%f Seconds Computed\n\n", time);
 
94
 
 
95
  bytes = totalCount*2*channels;
 
96
#ifndef __LITTLE_ENDIAN__
 
97
  swap32((unsigned char *)&bytes);
 
98
#endif
 
99
  fseek(fd,40,SEEK_SET); // jump to data length
 
100
  fwrite(&bytes,4,1,fd);
 
101
 
 
102
  bytes = totalCount*2*channels + 44;
 
103
#ifndef __LITTLE_ENDIAN__
 
104
  swap32((unsigned char *)&bytes);
 
105
#endif
 
106
  fseek(fd,4,SEEK_SET); // jump to file size
 
107
  fwrite(&bytes,4,1,fd);
 
108
  fclose(fd);
 
109
}
 
110
 
 
111
void WavWvOut :: tick(MY_FLOAT sample)
 
112
{
 
113
  static INT16 isample;
 
114
 
 
115
  isample = (INT16) (sample * 32000.0);
 
116
#ifndef __LITTLE_ENDIAN__
 
117
  swap16 ((unsigned char *)&isample);
 
118
#endif
 
119
  for (int i=0;i<channels;i++)
 
120
    data[counter++] = isample;
 
121
 
 
122
  totalCount++;
 
123
  if (counter == data_length) {
 
124
    fwrite(data,2,data_length,fd);
 
125
    counter = 0;
 
126
  }
 
127
}
 
128
 
 
129
void WavWvOut :: mtick(MY_MULTI samples)
 
130
{
 
131
  for (int i=0;i<channels;i++) {
 
132
    data[counter] = (INT16) (*samples++ * 32000.0);
 
133
#ifndef __LITTLE_ENDIAN__
 
134
    swap16 ((unsigned char *)&data[counter]);
 
135
#endif
 
136
    counter++;
 
137
  }
 
138
 
 
139
  totalCount++;
 
140
  if (counter == data_length) {
 
141
    fwrite(data,2,data_length,fd);
 
142
    counter = 0;
 
143
  }
 
144
}