annotate libao2/ao_pcm.c @ 1428:a90d889eb649

largefile patch by Stephen Davies <steve@daviesfam.org>
author arpi
date Tue, 31 Jul 2001 23:18:16 +0000
parents d92e19f1ae71
children 981a9e5118ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
1 #include <stdio.h>
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
2 #include <stdlib.h>
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
3
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
4 #include "audio_out.h"
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
5 #include "audio_out_internal.h"
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
6
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
7 static ao_info_t info =
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
8 {
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
9 "RAW PCM/WAVE file writer audio output",
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
10 "pcm",
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
11 "Atmosfear",
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
12 ""
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
13 };
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
14
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
15 LIBAO_EXTERN(pcm)
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
16
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
17 // there are some globals:
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
18 // ao_samplerate
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
19 // ao_channels
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
20 // ao_format
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
21 // ao_bps
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
22 // ao_outburst
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
23 // ao_buffersize
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
24
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
25 char *ao_outputfilename = NULL;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
26 int ao_pcm_waveheader = 1;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
27
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
28 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
29 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
30 #define WAV_ID_FMT 0x20746d66 /* "fmt " */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
31 #define WAV_ID_DATA 0x61746164 /* "data" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
32 #define WAV_ID_PCM 0x0001
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
33
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
34 struct WaveHeader
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
35 {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
36 unsigned long riff;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
37 unsigned long file_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
38 unsigned long wave;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
39 unsigned long fmt;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
40 unsigned long fmt_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
41 short fmt_tag;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
42 short channels;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
43 unsigned long sample_rate;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
44 unsigned long bytes_per_second;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
45 short block_align;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
46 short bits;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
47 unsigned long data;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
48 unsigned long data_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
49 };
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
50
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
51
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
52 static struct WaveHeader wavhdr = {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
53 WAV_ID_RIFF,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
54 0x00000000,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
55 WAV_ID_WAVE,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
56 WAV_ID_FMT,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
57 16,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
58 WAV_ID_PCM,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
59 2,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
60 44100,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
61 192000,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
62 4,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
63 16,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
64 WAV_ID_DATA,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
65 0x00000000
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
66 };
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
67
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
68 static FILE *fp = NULL;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
69
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
70 // to set/get/query special features/parameters
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
71 static int control(int cmd,int arg){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
72 return -1;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
73 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
74
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
75 // open & setup audio device
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
76 // return: 1=success 0=fail
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
77 static int init(int rate,int channels,int format,int flags){
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
78 if(!ao_outputfilename) {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
79 ao_outputfilename = (char *) malloc(sizeof(char) * 14);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
80 strcpy(ao_outputfilename,(ao_pcm_waveheader ? "audiodump.wav" : "audiodump.pcm"));
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
81 }
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
82
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
83 wavhdr.channels = channels;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
84 wavhdr.sample_rate = rate;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
85 wavhdr.bytes_per_second = rate * (format / 8) * channels;
1113
atmosfear
parents: 1112
diff changeset
86 wavhdr.bits = format;
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
87
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
88 printf("PCM: File: %s (%s) Samplerate: %iHz Channels: %s Format %s\n", ao_outputfilename, (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate, (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format));
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
89 printf("PCM: Info - fastest dumping is achieved with -vo null -hardframedrop.\n");
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
90 printf("PCM: Info - to write WAVE files use -waveheader (default), for RAW PCM -nowaveheader.\n");
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
91 fp = fopen(ao_outputfilename, "wb");
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
92
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
93 ao_outburst = 4096;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
94
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
95
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
96 if(fp) {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
97 if(ao_pcm_waveheader) /* Reserve space for wave header */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
98 fseek(fp, sizeof(wavhdr), SEEK_SET);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
99 return 1;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
100 }
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
101 printf("PCM: Failed to open %s for writing!\n", ao_outputfilename);
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
102 return 0;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
103 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
104
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
105 // close audio device
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
106 static void uninit(){
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
107
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
108 if(ao_pcm_waveheader){ /* Write wave header */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
109 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
110 fseek(fp, 0, SEEK_SET);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
111 fwrite(&wavhdr,sizeof(wavhdr),1,fp);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
112 }
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
113 fclose(fp);
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
114 free(ao_outputfilename);
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
115 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
116
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
117 // stop playing and empty buffers (for seeking/pause)
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
118 static void reset(){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
119
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
120 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
121
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
122 // stop playing, keep buffers (for pause)
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
123 static void audio_pause()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
124 {
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
125 // for now, just call reset();
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
126 reset();
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
127 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
128
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
129 // resume playing, after audio_pause()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
130 static void audio_resume()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
131 {
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
132 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
133
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
134 // return: how many bytes can be played without blocking
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
135 static int get_space(){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
136
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
137 return ao_outburst;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
138 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
139
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
140 // plays 'len' bytes of 'data'
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
141 // it should round it down to outburst*n
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
142 // return: number of bytes played
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
143 static int play(void* data,int len,int flags){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
144
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
145 //printf("PCM: Writing chunk!\n");
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
146 fwrite(data,len,1,fp);
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
147
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
148 if(ao_pcm_waveheader)
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
149 wavhdr.data_length += len;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
150
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
151 return len;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
152 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
153
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
154 // return: how many unplayed bytes are in the buffer
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
155 static int get_delay(){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
156
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
157 return 0;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
158 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
159
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
160
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
161
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
162
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
163
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
164