File: wav_play.c

package info (click to toggle)
lletters 0.1.95-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,152 kB
  • ctags: 502
  • sloc: sh: 9,985; ansic: 3,225; makefile: 230; sed: 93; perl: 50
file content (136 lines) | stat: -rw-r--r-- 2,380 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#ifdef GSM
#	include <gsm.h>
#endif
#include "wav_lib.h"
#include "version.h"
#include "tellhow.h"
#include "timer.h"

wav_sig *file_info;
long count;

/* GSM stuff */

#ifdef GSM

struct {
	gsm handle;
	gsm_frame buf;
	gsm_signal sample[160];
} gsm_info;

#endif

void tellhow(int argc, char *argv[]){
	printf(BRAG, MAJ, MIN);
	printf(WAVP, argv[0]);
}

void report(){
	printf(BRAG, MAJ, MIN);

 	printf(PLAYING,
	file_info->name,
	file_info->len,
	file_info->srate,
	file_info->bps,
	file_info->depth,
	file_info->chans);

}

int init_dsp(wav_sig *file_info){
	int i, p;

	dsp_fd = open("/dev/dsp", O_WRONLY, 0);

	ioctl(dsp_fd, SNDCTL_DSP_RESET, 0);

	p =  file_info->depth;
	i =  ioctl(dsp_fd, SOUND_PCM_WRITE_BITS, &p);

	p =  file_info->chans;
	i += ioctl(dsp_fd, SOUND_PCM_WRITE_CHANNELS, &p);

	p =  file_info->srate;
	i += ioctl(dsp_fd, SOUND_PCM_WRITE_RATE, &p);

	i += ioctl(dsp_fd, SNDCTL_DSP_GETBLKSIZE, &bsize);
	
	ioctl(dsp_fd, SNDCTL_DSP_SYNC, 0);

	return(i);
}

void play_buffer(){

#ifndef GSM

	if (file_info->len < bsize){
		count = read(file_info->handle, buffer, file_info->len);
		file_info->len -= count;
	} else {
		count = read(file_info->handle, (int *)buffer, bsize);
		file_info->len -= count;
	}
	write(dsp_fd, buffer, count);

#else

	read(file_info->handle, (char *) gsm_info.buf, sizeof(gsm_info.buf));
	gsm_decode(gsm_info.handle, gsm_info.buf, gsm_info.sample);
	count = write(dsp_fd, gsm_info.sample, sizeof(gsm_info.sample));
	file_info->len -= count;
	
	count = 0;
#endif
}
#ifdef SOUND
int play_file (char *filename) {
        long i;
#else 
int main(int argc, char *argv[]) {
        char *filename;
        long i;

	if (argc == 1){
	  tellhow(argc, argv);
	  exit(-1);
	}
	filename = argv[1];
#endif 	
	file_info = (wav_sig *)malloc(sizeof(wav_sig));
	buffer = (int *)malloc(32767);

	if (init_wav_read(filename, file_info) == -1){
		fprintf(stderr,"Error opening file: %s\n", file_info->name);
		exit(0);
	}

	if (i = init_dsp(file_info) != 0){
		fprintf(stderr,"Cannot initialize DSP\n");
#ifdef SOUND
		exit(0);
#endif 
	}
	
	report();

#ifdef GSM	
	gsm_info.handle = gsm_create();
#endif
	while (file_info->len > 0){
		play_buffer();
		update_timer(file_info, 1);
	}
#ifdef GSM
	gsm_destroy(gsm_info.handle);
#endif

	close(dsp_fd);
	close(file_info->handle);
	puts("\n");
}