3107
|
1 /* This is a null audio out plugin it doesnt't really do anything
|
|
2 useful but serves an example of how audio plugins work. It delays
|
|
3 the output signal by the nuber of samples set by aop_delay n
|
|
4 where n is the number of bytes.
|
|
5 */
|
|
6
|
|
7 #include <stdio.h>
|
|
8 #include <stdlib.h>
|
|
9
|
|
10 #include "audio_out.h"
|
|
11 #include "audio_plugin.h"
|
|
12 #include "audio_plugin_internal.h"
|
|
13 #include "afmt.h"
|
|
14
|
|
15 static ao_info_t info =
|
|
16 {
|
|
17 "Null audio plugin",
|
|
18 "delay",
|
|
19 "Anders",
|
|
20 ""
|
|
21 };
|
|
22
|
|
23 LIBAO_PLUGIN_EXTERN(delay)
|
|
24
|
|
25 // local data
|
|
26 typedef struct pl_delay_s
|
|
27 {
|
|
28 void* data; // local audio data block
|
|
29 void* delay; // data block used for delaying audio signal
|
|
30 int len; // local buffer length
|
|
31 int rate; // local data rate
|
|
32 int channels; // local number of channels
|
|
33 int format; // local format
|
|
34
|
|
35 } pl_delay_t;
|
|
36
|
|
37 static pl_delay_t pl_delay={NULL,NULL,0,0,0,0};
|
|
38
|
|
39 // global data
|
|
40 int pl_delay_len=0; // number of samples to delay sound output set from cmd line
|
|
41
|
|
42 // to set/get/query special features/parameters
|
|
43 static int control(int cmd,int arg){
|
|
44 switch(cmd){
|
|
45 case AOCONTROL_PLUGIN_SET_LEN:
|
|
46 if(pl_delay.data)
|
|
47 uninit();
|
|
48 pl_delay.len = arg;
|
|
49 pl_delay.data=(void*)malloc(arg);
|
|
50 return CONTROL_OK;
|
|
51 }
|
|
52 return -1;
|
|
53 }
|
|
54
|
|
55 // open & setup audio device
|
|
56 // return: 1=success 0=fail
|
|
57 static int init(){
|
|
58 int i=0;
|
|
59 float time_delay; // The number of tsamples this plugin delays the output data
|
|
60 /* if the output format of any of the below parameters differs from
|
|
61 what is give it should be changed. See ao_plugin init() */
|
|
62 pl_delay.rate=ao_plugin_data.rate;
|
|
63 pl_delay.channels=ao_plugin_data.channels+1; //0=mono 1=stereo
|
|
64 pl_delay.format=ao_plugin_data.format;
|
|
65
|
|
66 // Tell ao_plugin how much this plugin adds to the overall time delay
|
|
67 time_delay=-1*(float)pl_delay_len/((float)pl_delay.channels*(float)pl_delay.rate);
|
|
68 if(pl_delay.format != AFMT_U8 && pl_delay.format != AFMT_S8)
|
|
69 time_delay/=2;
|
|
70 ao_plugin_data.delay_fix+=time_delay;
|
|
71
|
|
72 pl_delay.delay=(void*)malloc(pl_delay_len);
|
|
73 if(!pl_delay.delay)
|
|
74 return 0;
|
|
75 for(i=0;i<pl_delay_len;i++)
|
|
76 ((char*)pl_delay.delay)[i]=0;
|
|
77 printf("[pl_delay] Output sound delayed by %i bytes\n",pl_delay_len);
|
|
78 return 1;
|
|
79 }
|
|
80
|
|
81 // close plugin
|
|
82 static void uninit(){
|
|
83 if(pl_delay.delay)
|
|
84 free(pl_delay.delay);
|
|
85 if(pl_delay.data)
|
|
86 free(pl_delay.data);
|
|
87 pl_delay_len=0;
|
|
88 }
|
|
89
|
|
90 // empty buffers
|
|
91 static void reset(){
|
|
92 int i = 0;
|
|
93 for(i=0;i<pl_delay.len;i++)
|
|
94 ((char*)pl_delay.data)[i]=0;
|
|
95 for(i=0;i<pl_delay_len;i++)
|
|
96 ((char*)pl_delay.delay)[i]=0;
|
|
97 }
|
|
98
|
|
99 // processes 'ao_plugin_data.len' bytes of 'data'
|
|
100 // called for every block of data
|
|
101 static int play(){
|
|
102 int i=0;
|
|
103 int j=0;
|
|
104 int k=0;
|
|
105 // Copy end of prev block to begining of buffer
|
|
106 for(i=0;i<pl_delay_len;i++,j++)
|
|
107 ((char*)pl_delay.data)[j]=((char*)pl_delay.delay)[i];
|
|
108 // Copy current block except end
|
|
109 for(i=0;i<ao_plugin_data.len-pl_delay_len;i++,j++,k++)
|
|
110 ((char*)pl_delay.data)[j]=((char*)ao_plugin_data.data)[k];
|
|
111 // Save away end of current block for next call
|
|
112 for(i=0;i<pl_delay_len;i++,k++)
|
|
113 ((char*)pl_delay.delay)[i]=((char*)ao_plugin_data.data)[k];
|
|
114 // Set output data block
|
|
115 ao_plugin_data.data=pl_delay.data;
|
|
116 return 1;
|
|
117 }
|
|
118
|
|
119
|
|
120
|