File: audiomixerreader.cpp

package info (click to toggle)
audacity 1.3.12-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 41,528 kB
  • ctags: 30,395
  • sloc: cpp: 166,518; ansic: 105,310; sh: 24,447; lisp: 7,842; makefile: 1,701; python: 240; perl: 31; xml: 8
file content (78 lines) | stat: -rw-r--r-- 2,243 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* audiomixerreader.cpp -- implements a class to read samples from Audacity
*
* 17-Jun-08  RBD created based on audiofilereader.cpp
*/
#include "assert.h"
#include "stdlib.h"
#include "audioreader.h"
#include "allegro.h"
#include "scorealign-glue.h"
#include "audiomixerreader.h"

double Audio_mixer_reader::get_sample_rate()
{
    return sample_rate;
}


Audio_mixer_reader::Audio_mixer_reader(void *mixer_, 
        mixer_process_fn fn_ptr, int chans, 
        double srate, double end_time)
{
    mixer = mixer_; // store in member variable
    mixer_process = fn_ptr;
    buffer = NULL;
    buffer_len = 0;
    index = 0;
    channels = chans;
    sample_rate = srate;
    total_frames = end_time * srate + 0.5 /* for rounding */;
}


long Audio_mixer_reader::get_frames()
{
    // precondition: mixer is valid and no samples have been read
    return total_frames;
}


long Audio_mixer_reader::read(float *data, long n)
{
    for (int i = 0; i < n; i++) { // fill data
        float sum = 0;
        // note: assume mixer returns stereo (interleaved)
        for (int chan = 0; chan < channels; chan++) {
            // sum over channels within a frame
            if (index >= buffer_len * channels) {
                buffer_len = 
                   (*mixer_process) (mixer, &buffer, AMR_BUFFER_FRAMES);
                // frame_count = mixer->Process(AMR_BUFFER_FRAMES);
                // buffer = (float *) mixer->GetBuffer();
                index = 0;
                if (buffer_len == 0) { // no more samples to read
                    // but we processed i
                    return i;
                }
            }
            sum += buffer[index++];
        }
        data[i] = sum;
    }
    return n; // when end is reached, n will be 0, but the caller shouldn't
              // be asking for samples beyond the end because the caller knows
              // how many samples to ask for.
}

void Audio_mixer_reader::close()
{
    // mixer is deleted by the creator of this object, so don't delete here
    buffer = NULL; 
}

void Audio_mixer_reader::print_info()
{
    printf("   Audacity mixer at @ %p\n", mixer);
    printf("   sample rate %g\n", get_sample_rate());
    printf("   total frames %d\n", get_frames());
}