annotate libao2/ao_pcm.c @ 3309:28f2ebcb5c95

Format plugin now with working switch statements
author anders
date Tue, 04 Dec 2001 12:28:26 +0000
parents 981a9e5118ce
children de4074ab4e5f
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
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
17 char *ao_outputfilename = NULL;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
18 int ao_pcm_waveheader = 1;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
19
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
20 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
21 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
22 #define WAV_ID_FMT 0x20746d66 /* "fmt " */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
23 #define WAV_ID_DATA 0x61746164 /* "data" */
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
24 #define WAV_ID_PCM 0x0001
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
25
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
26 struct WaveHeader
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 unsigned long riff;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
29 unsigned long file_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
30 unsigned long wave;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
31 unsigned long fmt;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
32 unsigned long fmt_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
33 short fmt_tag;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
34 short channels;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
35 unsigned long sample_rate;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
36 unsigned long bytes_per_second;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
37 short block_align;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
38 short bits;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
39 unsigned long data;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
40 unsigned long data_length;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
41 };
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
42
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
43
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
44 static struct WaveHeader wavhdr = {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
45 WAV_ID_RIFF,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
46 0x00000000,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
47 WAV_ID_WAVE,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
48 WAV_ID_FMT,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
49 16,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
50 WAV_ID_PCM,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
51 2,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
52 44100,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
53 192000,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
54 4,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
55 16,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
56 WAV_ID_DATA,
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
57 0x00000000
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
58 };
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
59
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
60 static FILE *fp = NULL;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
61
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
62 // to set/get/query special features/parameters
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
63 static int control(int cmd,int arg){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
64 return -1;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
65 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
66
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
67 // open & setup audio device
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
68 // return: 1=success 0=fail
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
69 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
70 if(!ao_outputfilename) {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
71 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
72 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
73 }
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
74
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
75 wavhdr.channels = channels;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
76 wavhdr.sample_rate = rate;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
77 wavhdr.bytes_per_second = rate * (format / 8) * channels;
1113
atmosfear
parents: 1112
diff changeset
78 wavhdr.bits = format;
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
79
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
80 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
81 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
82 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
83 fp = fopen(ao_outputfilename, "wb");
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
84
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1113
diff changeset
85 ao_data.outburst = 4096;
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
86
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
87
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
88 if(fp) {
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
89 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
90 fseek(fp, sizeof(wavhdr), SEEK_SET);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
91 return 1;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
92 }
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
93 printf("PCM: Failed to open %s for writing!\n", ao_outputfilename);
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
94 return 0;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
95 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
96
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
97 // close audio device
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
98 static void uninit(){
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
99
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
100 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
101 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
102 fseek(fp, 0, SEEK_SET);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
103 fwrite(&wavhdr,sizeof(wavhdr),1,fp);
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
104 }
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
105 fclose(fp);
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
106 free(ao_outputfilename);
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
107 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
108
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
109 // stop playing and empty buffers (for seeking/pause)
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
110 static void reset(){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
111
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
112 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
113
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
114 // stop playing, keep buffers (for pause)
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
115 static void audio_pause()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
116 {
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
117 // for now, just call reset();
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
118 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 // resume playing, after audio_pause()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
122 static void audio_resume()
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
123 {
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
124 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
125
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
126 // return: how many bytes can be played without blocking
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
127 static int get_space(){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
128
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1113
diff changeset
129 return ao_data.outburst;
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
130 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
131
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
132 // plays 'len' bytes of 'data'
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
133 // it should round it down to outburst*n
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
134 // return: number of bytes played
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
135 static int play(void* data,int len,int flags){
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
136
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
137 //printf("PCM: Writing chunk!\n");
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
138 fwrite(data,len,1,fp);
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
139
1112
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
140 if(ao_pcm_waveheader)
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
141 wavhdr.data_length += len;
b1cf1087ec33 Added support for writing wave files and specifying filename to write to.
atmosfear
parents: 1107
diff changeset
142
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
143 return len;
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
144 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
145
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1113
diff changeset
146 // return: delay in seconds between first and last sample in buffer
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1113
diff changeset
147 static float get_delay(){
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
148
3095
981a9e5118ce interface to libao2 changed ao_plugin added
anders
parents: 1113
diff changeset
149 return 0.0;
1107
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
150 }
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
151
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
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
155
9d764880d989 Added raw PCM writer ao driver.
atmosfear
parents:
diff changeset
156