forked from elastomania/across
-
Notifications
You must be signed in to change notification settings - Fork 0
/
H_WAV.CPP
204 lines (181 loc) · 4.36 KB
/
H_WAV.CPP
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "all.h"
void wav::lefoglal( void ) {
if( size > 1000000 )
hiba( "wav::alloc-ban size > 1000000!" );
{ tomb = NEW(signed short*) signed short[size]; }
if( !tomb )
uzenet( "wav::alloc-ban memory!" );
}
wav::wav( unsigned sizepar ) {
tomb = NULL;
size = sizepar;
lefoglal();
}
static void nevwavtest( char* nev ) {
int i = strlen( nev ) - 1;
while( i > 0 ) {
if( nev[i] == '.' ) {
// Most ponton all:
if( strcmpi( ".wav", &nev[i] ) == 0 )
return;
else
hiba( "nevrawtest-ben nincs .raw!" );
}
i--;
}
hiba( "nevwavtest-ben nincs .wav!" );
}
struct wavheader {
char riff[4];
long hossz1;
char wave[4];
// format:
char fmt[4];
long miez1;
short mod; // 1 -> 8bit uncompressed
short channel; // 1 -> mono, 2 -> stereo
unsigned short frekvencia;
short miez2, miez3, miez4;
long nemkene;
// audio data:
char data[4];
long hossz;
};
wav::wav( char* nev, double maxamplitudo, int start, int ucsoutani ) {
tomb = NULL;
nevwavtest( nev );
// Lemeri file hosszat:
FILE* h = qopen( nev, "rb" );
if( !h )
hiba( "Nem tudta megnyitni file-t: ", nev );
wavheader header;
int negyvennegy = 44;
if( sizeof( wavheader ) != negyvennegy )
hiba( "sizeof( wavheader ) != 44!" );
if( fread( &header, 1, 44, h ) != 44 )
hiba( "Nem sikerult beolvasni wav-ot!" );
long hossz = header.hossz;
int bit16 = 1;
// Reszlet eldontese:
int reszlet = 1;
if( ucsoutani <= 0 )
reszlet = 0;
int reszhossz = ucsoutani-start;
if( !reszlet ) {
start = 0;
if( bit16 )
reszhossz = hossz/2;
else
reszhossz = hossz;
}
if( header.channel != 1 )
hiba( "Wav file nem mono!: ", nev );
if( bit16 ) {
// 16 bites uncompressed:
if( size%2 )
hiba( "16 bites wav-nal nem paros byteszam!" );
size = reszhossz;
if( start+reszhossz > hossz/2 )
hiba( "Reszlet kilog wav-bol!" );
lefoglal();
qseek( h, start*2, SEEK_CUR );
if( fread( tomb, 1, size*2, h ) != size*2 )
hiba( "Nem sikerult beolvasni wav-ot!: ", nev );
}
else {
// 8 bites uncompressed:
if( reszlet )
hiba( "8 bitre meg nincs megirva reszlet!" );
size = hossz;
lefoglal();
if( fread( tomb, 1, hossz, h ) != hossz )
hiba( "Nem sikerult beolvasni wav-ot!: ", nev );
unsigned char* bytetomb = (unsigned char*)tomb;
for( int i = size-1; i >= 0; i-- ) {
tomb[i] = short((((short)(bytetomb[i]))-128)<<8);
}
}
qclose( h );
// Megkeresi maximalis amplitudot:
int maxamp = 1;
for( int i = 0; i < size; i++ ) {
if( tomb[i] > 0 ) {
if( tomb[i] > maxamp )
maxamp = tomb[i];
}
else {
if( -tomb[i] > maxamp )
maxamp = -tomb[i];
}
}
// Kiszamolja szorzot:
double szorzo = 32000.0*maxamplitudo/double(maxamp);
for( i = 0; i < size; i++ ) {
tomb[i] *= szorzo;
}
}
void wav::save( char* nev ) {
nevwavtest( nev );
// Lemeri file hosszat:
FILE* h = fopen( nev, "wb" );
if( !h )
hiba( "Nem tudta megnyitni file-t: ", nev );
if( fwrite( tomb, 1, size*2, h ) != size*2 )
hiba( "Nem sikerult irni wav-ot wav::save-ben!" );
fclose( h );
}
// Elejere tesz egy loopot a vegebol, amit aztan le is vag:
void wav::loopol( int n ) {
if( n >= size )
hiba( "n >= size!" );
for( int i = 0; i < n; i++ ) {
double arany = ((double)i)/n;
tomb[i] = arany*tomb[i] + (1-arany)*tomb[size-n+i];
}
size -= n;
}
// pwav elejet rateszi vegere:
void wav::vegereilleszt( wav* pwav, int n ) {
if( n >= size )
hiba( "n >= size!" );
if( n >= pwav->size )
hiba( "n >= pwav->size!" );
for( int i = 0; i < n; i++ ) {
double arany = ((double)i)/n;
tomb[size-n+i] = (1-arany)*tomb[i] + arany*pwav->tomb[i];
}
}
// WAV2 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//short* tomb, mtomb;
wav2::wav2( wav* pwav ) {
mtomb = NULL;
tomb = pwav->tomb;
#ifndef WATCOM
hiba( "wav2 csak Watcom alatt mukodik!" );
#endif
if( pwav->size > 64000 )
hiba( "wav2-ben size > 64000!" );
{ mtomb = NEW(short*) short[pwav->size]; }
if( !mtomb )
uzenet( "memory" );
for( int i = 0; i < pwav->size-1; i++ )
mtomb[i] = tomb[i+1]-tomb[i];
mtomb[pwav->size-1] = tomb[0]-tomb[pwav->size-1];
ulsize = ((unsigned long)pwav->size)*65536;
ulfuto = 0;
plow = (unsigned short*)&ulfuto;
phigh = plow+1;
}
void wav2::reset( int fazis ) {
if( fazis < 0 )
ulfuto = 0;
else
ulfuto = fazis*65536;
}
// dt 65536 szoros:
short wav2::getnextsample( long dt ) {
ulfuto += dt;
if( ulfuto >= ulsize )
ulfuto -= ulsize;
return tomb[*phigh] + ((mtomb[*phigh]*(*plow))>>16);
}