-
Notifications
You must be signed in to change notification settings - Fork 1
/
wavplayer.c
173 lines (121 loc) · 4.34 KB
/
wavplayer.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
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
// PLAY EDITOR by Gabriel Gonzalez
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <linux/soundcard.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#define MONO 1
#define STEREO 2
unsigned char chunkID[4];
unsigned char format[4];
unsigned char subChunk1ID[4];
unsigned char subChunk2ID[4];
unsigned int chunkSize;
unsigned int subChunk1IDSize;
unsigned int audioFormat;
unsigned int numChannels;
unsigned int sampleRate;
unsigned int byteRate;
unsigned int blockAlign;
unsigned int bitPerSample;
unsigned int wavFileSize;
int main(int argc, char *argv[]){
FILE *fileSound;
int soundCard;
printf("WavPlayer by Gabriel González\n\n");
soundCard = open("/dev/dsp", O_WRONLY);
if( soundCard < 0){
printf("ERROR to open the sound card! \n");
}else{
printf("Sound Card Opened !\n");
}
if( fileSound = fopen(argv[1],"r")){
printf("Wav file ok! \n");
}
else{
printf("Error opening wav file \n");
}
printf("Description: \n");
fseek(fileSound , 0L, SEEK_SET);
fread(&chunkID,4 , 1 , fileSound);
printf("RIFF: %s \n", chunkID) ;
fseek(fileSound , 4L, SEEK_SET);
fread(&chunkSize , 4, 1 , fileSound);
printf("CHUNCK SIZE: %d\n", ( chunkSize / 1024 ) / 1024 );
fseek(fileSound , 8L , SEEK_SET);
fread(&format, 4, 1, fileSound);
printf("FILE FORMAT: %s\n",format);
fseek(fileSound , 12L , SEEK_SET);
fread(&subChunk1ID , 4, 1 , fileSound);
printf("FMT ID %s\n",subChunk1ID);
fseek(fileSound , 16L , SEEK_SET);
fread(&subChunk1IDSize , 4, 1 , fileSound);
printf("FMT Size: %d\n",subChunk1IDSize);
fseek(fileSound , 20L , SEEK_SET);
fread(&audioFormat , 2, 1 , fileSound);
if(audioFormat == 1)
printf("NO HAY COMPRESION\n");
else
printf("HAY ALGUN TIPO DE COMPRESION\n");
fseek(fileSound , 22L , SEEK_SET);
fread(&numChannels , 2 , 1 , fileSound);
if(numChannels == 1)
printf("Play in MONO:\n" );
else if(numChannels == 2)
printf("Play in STEREO\n",numChannels);
fseek(fileSound , 24L , SEEK_SET);
fread(&sampleRate , 4, 1 , fileSound);
printf("SampleRate %d\n", sampleRate);
fseek(fileSound , 28L , SEEK_SET);
fread(&byteRate , 4 , 1, fileSound);
printf("SAMPLE RATE: %d\n",byteRate);
fseek(fileSound , 32L , SEEK_SET);
fread(&blockAlign , 2 , 1, fileSound);
printf("BLOCK ALIGN %d\n", blockAlign);
fseek(fileSound , 34L , SEEK_SET);
fread(&bitPerSample , 2 , 1, fileSound);
printf("BITS PER SAMPLE %d\n",bitPerSample);
fseek(fileSound , 36L , SEEK_SET);
fread(&subChunk2ID , 4 , 1 , fileSound);
printf("DATA : %s\n",subChunk2ID);
fseek(fileSound , 40L , SEEK_SET);
fread(&wavFileSize , 4 , 1 , fileSound);
printf("File size %d\n",wavFileSize);
fseek(fileSound , 44L , SEEK_SET);
printf("Configurando tarjeta de sonido ...\n");
if (numChannels == 1){
if (ioctl(soundCard, SOUND_PCM_WRITE_CHANNELS, MONO) == -1){
printf("I can't configure channels in MONO\n");
}else{
printf("Playing in MONO\n");
}
}
if (numChannels == 2){
if (ioctl(soundCard, SOUND_PCM_WRITE_CHANNELS, STEREO) == -1)
printf("I can't configure channels in STEREO\n");
else{
printf("Playing in STEREO\n");
}
}
if (ioctl(soundCard, SOUND_PCM_WRITE_RATE, &sampleRate) == -1){
printf("I can't configure sample rate\n");
}
if( ioctl(soundCard , SOUND_PCM_WRITE_BITS , &bitPerSample ) == -1){
printf("I can't send the configuration BITS PER SAMPLE \n");
}else{
printf("BITS configurated!\n");
}
int sample=0;
while(feof(fileSound) == 0)
{
fread(&sample, 1, 1, fileSound);
write(soundCard, &sample, 1);
}
fclose(fileSound);
close(soundCard);
return EXIT_SUCCESS;
}