-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_func.c
146 lines (120 loc) · 3.12 KB
/
io_func.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "wave_io.h"
int write_wave(short *wave, unsigned int n_wave, int freq, int bits, char *out_name,
struct Waveheader *header)
{
FILE *outfile;
unsigned int i;
char byte;
/*update header*/
header->sample_fq=freq;
header->data_length=(n_wave*header->byte_p_spl)/header->modus;
header->length=header->data_length+sizeof(struct Waveheader)-8;
fprintf(stderr,"filelength %lu\nsamples %lu\nrate %lu\nbits %u\nbytes per sample %u\nchannels %u\n",
header->length, n_wave, header->sample_fq, header->bit_p_spl, header->byte_p_spl, header->modus);
outfile = fopen(out_name,"wb");
if (outfile == NULL)
{
fprintf(stderr,"Couldn't open %s\n",out_name);
exit(0);
}
fwrite(header,sizeof(struct Waveheader),1,outfile);
for (i=0; i < n_wave; i++)
{
/*check for 16 bit data*/
if (header->bit_p_spl==16)
fwrite(&(wave[i]), sizeof(short),1,outfile);
else
{
byte=(char)wave[i];
fwrite(&byte, sizeof(char),1,outfile);
}
}
fclose(outfile);
return(1);
}
int read_wave(short **wave, unsigned int *n_wave, int *freq, int *bits, char *in_name,
struct Waveheader *header)
{
FILE *infile;
unsigned int i, index=0;
int j;
char ch, byte;
u_long n_samples;
infile = fopen(in_name,"rb");
if (infile == NULL)
{
fprintf(stderr,"Couldn't open %s\n",in_name);
exit(0);
}
/*read header*/
fread(header,sizeof(struct Waveheader),1,infile);
fseek(infile,0,SEEK_SET);
/*find data chunk*/
j=0;
for(i=0; i < 100; i++)
{
fread(&ch,sizeof(char),1,infile);
if (ch=='d')
{
j++;
index=i;
}
if (ch=='a')
{
if ((index==(i-1)) && ((j==1)||(j==3)) )
{
j++;
index=i;
}
else
j=0;
}
if (ch=='t')
{
if ((index==(i-1)) && (j==2))
{
j++;
index=i;
}
else
j=0;
}
/*found tag "data"*/
if (j==4)
break;
}
/*read length of data in bytes*/
fread(&n_samples,sizeof(u_long),1,infile);
fprintf(stderr,"filelength %lu\nsamples %lu\nrate %lu\nbits %u\nbytes per sample %u\nchannels %u\n",
header->length, header->modus*n_samples/header->byte_p_spl, header->sample_fq, header->bit_p_spl, header->byte_p_spl, header->modus);
/*change to uniform header length*/
header->sc_len=16;
sprintf(header->data_chunk,"data");
header->data_length=n_samples;
*freq = (int)header->sample_fq;
*bits = header->bit_p_spl;
/*get number of samples*/
*n_wave =header->modus*header->data_length/header->byte_p_spl;
/*allocate memory for samples*/
*wave = (short*)malloc(sizeof(short)*(*n_wave));
if (*wave == NULL)
return(-1);
for (i=0; i < *n_wave; i++)
{
/*if 16 bit data, read short values*/
if (*bits == 16)
fread(&((*wave)[i]), sizeof(short),1,infile);
/*otherwise read char and convert to short*/
else
{
fread(&byte, sizeof(char),1,infile);
(*wave)[i]=(short)byte;
}
}
fclose(infile);
return(1);
}