annotate libaf/af_export.c @ 26427:89ce68bf7b6e

Mark phony targets as such.
author diego
date Fri, 18 Apr 2008 08:13:00 +0000
parents e5e5db68a660
children 72d0b1444141
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
1 /* This audio filter exports the incomming signal to other processes
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
2 using memory mapping. Memory mapped area contains a header:
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
3 int nch,
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
4 int size,
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
5 unsigned long long counter (updated every time the contents of
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
6 the area changes),
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
7 the rest is payload (non-interleaved).
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
8 */
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
9
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
10 #include <stdio.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
11 #include <stdlib.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
12 #include <string.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
13 #include <inttypes.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
14 #include <unistd.h>
16982
a9da2db9eb16 Unify include paths by adding -I.. to CFLAGS.
diego
parents: 14245
diff changeset
15 #include "config.h"
10908
dcca52fe32bd disable af export for systems without mmap
faust3
parents: 10892
diff changeset
16
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
17 #include <sys/types.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
18 #include <sys/mman.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
19 #include <sys/types.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
20 #include <sys/stat.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
21 #include <fcntl.h>
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
22
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
23 #include "af.h"
24242
76f5d8892c04 Clean up the way get_path is handled: Compile get_path.c to an object to link
diego
parents: 22725
diff changeset
24 #include "get_path.h"
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
25
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
26 #define DEF_SZ 512 // default buffer size (in samples)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
27 #define SHARED_FILE "mplayer-af_export" /* default file name
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
28 (relative to ~/.mplayer/ */
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
29
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
30 #define SIZE_HEADER (2 * sizeof(int) + sizeof(unsigned long long))
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
31
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
32 // Data for specific instances of this filter
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
33 typedef struct af_export_s
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
34 {
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
35 unsigned long long count; // Used for sync
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
36 void* buf[AF_NCH]; // Buffers for storing the data before it is exported
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
37 int sz; // Size of buffer in samples
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
38 int wi; // Write index
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
39 int fd; // File descriptor to shared memory area
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
40 char* filename; // File to export data
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
41 void* mmap_area; // MMap shared area
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
42 } af_export_t;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
43
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
44
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
45 /* Initialization and runtime control
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
46 af audio filter instance
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
47 cmd control command
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
48 arg argument
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
49 */
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
50 static int control(struct af_instance_s* af, int cmd, void* arg)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
51 {
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
52 af_export_t* s = af->setup;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
53 switch (cmd){
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
54 case AF_CONTROL_REINIT:{
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
55 int i=0;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
56 int mapsize;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
57
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
58 // Free previous buffers
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
59 if (s->buf && s->buf[0])
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
60 free(s->buf[0]);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
61
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
62 // unmap previous area
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
63 if(s->mmap_area)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
64 munmap(s->mmap_area, SIZE_HEADER + (af->data->bps*s->sz*af->data->nch));
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
65 // close previous file descriptor
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
66 if(s->fd)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
67 close(s->fd);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
68
11529
2f30dad6f490 minor fixes by ed@catmur.co.uk
alex
parents: 10908
diff changeset
69 // Accept only int16_t as input format (which sucks)
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
70 af->data->rate = ((af_data_t*)arg)->rate;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
71 af->data->nch = ((af_data_t*)arg)->nch;
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 11529
diff changeset
72 af->data->format = AF_FORMAT_S16_NE;
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
73 af->data->bps = 2;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
74
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
75 // If buffer length isn't set, set it to the default value
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
76 if(s->sz == 0)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
77 s->sz = DEF_SZ;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
78
11529
2f30dad6f490 minor fixes by ed@catmur.co.uk
alex
parents: 10908
diff changeset
79 // Allocate new buffers (as one continuous block)
2f30dad6f490 minor fixes by ed@catmur.co.uk
alex
parents: 10908
diff changeset
80 s->buf[0] = calloc(s->sz*af->data->nch, af->data->bps);
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
81 if(NULL == s->buf[0])
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
82 af_msg(AF_MSG_FATAL, "[export] Out of memory\n");
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
83 for(i = 1; i < af->data->nch; i++)
11529
2f30dad6f490 minor fixes by ed@catmur.co.uk
alex
parents: 10908
diff changeset
84 s->buf[i] = s->buf[0] + i*s->sz*af->data->bps;
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
85
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
86 // Init memory mapping
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
87 s->fd = open(s->filename, O_RDWR | O_CREAT | O_TRUNC, 0640);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
88 af_msg(AF_MSG_INFO, "[export] Exporting to file: %s\n", s->filename);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
89 if(s->fd < 0)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
90 af_msg(AF_MSG_FATAL, "[export] Could not open/create file: %s\n",
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
91 s->filename);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
92
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
93 // header + buffer
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
94 mapsize = (SIZE_HEADER + (af->data->bps * s->sz * af->data->nch));
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
95
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
96 // grow file to needed size
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
97 for(i = 0; i < mapsize; i++){
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
98 char null = 0;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
99 write(s->fd, (void*) &null, 1);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
100 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
101
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
102 // mmap size
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
103 s->mmap_area = mmap(0, mapsize, PROT_READ|PROT_WRITE,MAP_SHARED, s->fd, 0);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
104 if(s->mmap_area == NULL)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
105 af_msg(AF_MSG_FATAL, "[export] Could not mmap file %s\n", s->filename);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
106 af_msg(AF_MSG_INFO, "[export] Memory mapped to file: %s (%p)\n",
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
107 s->filename, s->mmap_area);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
108
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
109 // Initialize header
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
110 *((int*)s->mmap_area) = af->data->nch;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
111 *((int*)s->mmap_area + 1) = s->sz * af->data->bps * af->data->nch;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
112 msync(s->mmap_area, mapsize, MS_ASYNC);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
113
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
114 // Use test_output to return FALSE if necessary
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
115 return af_test_output(af, (af_data_t*)arg);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
116 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
117 case AF_CONTROL_COMMAND_LINE:{
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
118 int i=0;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
119 char *str = arg;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
120
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
121 if (!str){
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
122 if(s->filename)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
123 free(s->filename);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
124
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
125 s->filename = get_path(SHARED_FILE);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
126 return AF_OK;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
127 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
128
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
129 while((str[i]) && (str[i] != ':'))
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
130 i++;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
131
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
132 if(s->filename)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
133 free(s->filename);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
134
18858
ac17673b640d gets ride of a few more sizeof(char)s
reynaldo
parents: 16982
diff changeset
135 s->filename = calloc(i + 1, 1);
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
136 memcpy(s->filename, str, i);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
137 s->filename[i] = 0;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
138
11529
2f30dad6f490 minor fixes by ed@catmur.co.uk
alex
parents: 10908
diff changeset
139 sscanf(str + i + 1, "%d", &(s->sz));
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
140
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
141 return af->control(af, AF_CONTROL_EXPORT_SZ | AF_CONTROL_SET, &s->sz);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
142 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
143 case AF_CONTROL_EXPORT_SZ | AF_CONTROL_SET:
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
144 s->sz = * (int *) arg;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
145 if((s->sz <= 0) || (s->sz > 2048))
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
146 af_msg( AF_MSG_ERROR, "[export] Buffer size must be between"
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
147 " 1 and 2048\n" );
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
148
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
149 return AF_OK;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
150 case AF_CONTROL_EXPORT_SZ | AF_CONTROL_GET:
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
151 *(int*) arg = s->sz;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
152 return AF_OK;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
153
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
154 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
155 return AF_UNKNOWN;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
156 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
157
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
158 /* Free allocated memory and clean up other stuff too.
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
159 af audio filter instance
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
160 */
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
161 static void uninit( struct af_instance_s* af )
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
162 {
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
163 if (af->data){
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
164 free(af->data);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
165 af->data = NULL;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
166 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
167
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
168 if(af->setup){
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
169 af_export_t* s = af->setup;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
170 if (s->buf && s->buf[0])
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
171 free(s->buf[0]);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
172
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
173 // Free mmaped area
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
174 if(s->mmap_area)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
175 munmap(s->mmap_area, sizeof(af_export_t));
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
176
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
177 if(s->fd > -1)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
178 close(s->fd);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
179
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
180 if(s->filename)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
181 free(s->filename);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
182
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
183 free(af->setup);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
184 af->setup = NULL;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
185 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
186 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
187
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
188 /* Filter data through filter
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
189 af audio filter instance
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
190 data audio data
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
191 */
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
192 static af_data_t* play( struct af_instance_s* af, af_data_t* data )
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
193 {
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
194 af_data_t* c = data; // Current working data
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
195 af_export_t* s = af->setup; // Setup for this instance
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
196 int16_t* a = c->audio; // Incomming sound
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
197 int nch = c->nch; // Number of channels
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
198 int len = c->len/c->bps; // Number of sample in data chunk
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
199 int sz = s->sz; // buffer size (in samples)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
200 int flag = 0; // Set to 1 if buffer is filled
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
201
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
202 int ch, i;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
203
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
204 // Fill all buffers
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
205 for(ch = 0; ch < nch; ch++){
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
206 int wi = s->wi; // Reset write index
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
207 int16_t* b = s->buf[ch]; // Current buffer
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
208
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
209 // Copy data to export buffers
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
210 for(i = ch; i < len; i += nch){
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
211 b[wi++] = a[i];
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
212 if(wi >= sz){ // Don't write outside the end of the buffer
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
213 flag = 1;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
214 break;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
215 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
216 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
217 s->wi = wi % s->sz;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
218 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
219
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
220 // Export buffer to mmaped area
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
221 if(flag){
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
222 // update buffer in mapped area
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
223 memcpy(s->mmap_area + SIZE_HEADER, s->buf[0], sz * c->bps * nch);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
224 s->count++; // increment counter (to sync)
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
225 memcpy(s->mmap_area + SIZE_HEADER - sizeof(s->count),
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
226 &(s->count), sizeof(s->count));
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
227 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
228
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
229 // We don't modify data, just export it
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
230 return data;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
231 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
232
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
233 /* Allocate memory and set function pointers
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
234 af audio filter instance
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
235 returns AF_OK or AF_ERROR
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
236 */
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
237 static int af_open( af_instance_t* af )
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
238 {
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
239 af->control = control;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
240 af->uninit = uninit;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
241 af->play = play;
24888
b2402b4f0afa libaf: change filter input/output ratio calculations
uau
parents: 24242
diff changeset
242 af->mul=1;
10892
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
243 af->data = calloc(1, sizeof(af_data_t));
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
244 af->setup = calloc(1, sizeof(af_export_t));
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
245 if((af->data == NULL) || (af->setup == NULL))
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
246 return AF_ERROR;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
247
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
248 ((af_export_t *)af->setup)->filename = get_path(SHARED_FILE);
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
249
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
250 return AF_OK;
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
251 }
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
252
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
253 // Description of this filter
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
254 af_info_t af_info_export = {
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
255 "Sound export filter",
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
256 "export",
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
257 "Anders; Gustavo Sverzut Barbieri <gustavo.barbieri@ic.unicamp.br>",
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
258 "",
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
259 AF_FLAGS_REENTRANT,
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
260 af_open
2167ac4c1d72 Adding filter for exporting audio data to visual effect applications
anders
parents:
diff changeset
261 };