summaryrefslogtreecommitdiff
path: root/gst/speed/filter.func
blob: 6802b20c52be4fc957bb801446dff845297c7db7 (plain)
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
/* -*- Mode: c; c-basic-offset: 2 -*- */ 
    _FORMAT *in_data, *out_data;
    static gint64 offset = 0, timestamp = 0;

    /* get a buffer here so that we can have something to interpolate
     * against for the first few samples if speed < 0.5 */
    in_data = (_FORMAT*) GST_BUFFER_DATA(in);
    nin = GST_BUFFER_SIZE(in)/sizeof(_FORMAT);
    lower = in_data[0];
    i_float = 0.5 * (speed - 1.0);
    i = i_float + 1.0; /* ciel(i_float) for ints */
    
    do {
      speed = filter->speed; /* update this, it might have changed */
      
      out = gst_buffer_new();
      GST_BUFFER_DATA(out) = (gchar*) g_new(_FORMAT,SPEED_BUFSIZE/sizeof(_FORMAT));
      GST_BUFFER_SIZE(out) = SPEED_BUFSIZE;
      out_data = (_FORMAT*) GST_BUFFER_DATA(out);
      nout = GST_BUFFER_SIZE(out) / sizeof(_FORMAT);
      GST_BUFFER_TIMESTAMP (out) = timestamp;
      offset += nout;
      timestamp = offset * GST_SECOND / filter->rate;
      
      for (j=0; j<nout; j++) {
        /* index of upper bounds of interpolation for
         * new sample, got it by trial&error on the chalkboard */
        i_float += speed;
        i = i_float + 1.0; /* ciel(i_float) for ints */
        
        while (i >= nin) {
          i = i % nin;
          i_float = i_float - nin;
          lower = in_data[nin-1];
          gst_buffer_unref(in);
          in = GST_BUFFER (gst_pad_pull (filter->sinkpad));
          
          while (GST_IS_EVENT (in)) {
            gst_pad_event_default (filter->srcpad, GST_EVENT (in));
            in = GST_BUFFER (gst_pad_pull (filter->sinkpad));
          }
          
          in_data = (_FORMAT*) GST_BUFFER_DATA(in);
          nin = GST_BUFFER_SIZE(in) / sizeof(_FORMAT);
        } 
        
        if (i>0)
          lower = in_data[i-1];
        
        interp = i_float - floor(i_float);
        
        out_data[j] = lower*(1-interp) + in_data[i]*interp;
        
        lower = in_data[i];
      }
      
      gst_pad_push(filter->srcpad, GST_DATA (out));

      gst_element_yield (element);
    } while (TRUE);