aboutsummaryrefslogtreecommitdiff
path: root/src/flowgraph/resampler/README.md
blob: ea319c7602a57d7d8b33f22800c091bd3ca6ffef (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Sample Rate Converter

This folder contains a sample rate converter, or "resampler".

The converter is based on a sinc function that has been windowed by a hyperbolic cosine.
We found this had fewer artifacts than the more traditional Kaiser window.

## Building the Resampler

It is part of [Oboe](https://github.com/google/oboe) but has no dependencies on Oboe.
So the contents of this folder can be used outside of Oboe.

To build it for use outside of Oboe:

1. Copy the "resampler" folder to a folder in your project that is in the include path.
2. Add all of the \*.cpp files in the resampler folder to your project IDE or Makefile.
3. In ResamplerDefinitions.h, define RESAMPLER_OUTER_NAMESPACE with your own project name. Alternatively, use -DRESAMPLER_OUTER_NAMESPACE=mynamespace when compiling to avoid modifying the resampler code.

## Creating a Resampler

Include the [main header](MultiChannelResampler.h) for the resampler.

    #include "resampler/MultiChannelResampler.h"

Here is an example of creating a stereo resampler that will convert from 44100 to 48000 Hz.
Only do this once, when you open your stream. Then use the sample resampler to process multiple buffers.

    MultiChannelResampler *resampler = MultiChannelResampler::make(
            2, // channel count
            44100, // input sampleRate
            48000, // output sampleRate
            MultiChannelResampler::Quality::Medium); // conversion quality

Possible values for quality include { Fastest, Low, Medium, High, Best }.
Higher quality levels will sound better but consume more CPU because they have more taps in the filter.

## Fractional Frame Counts

Note that the number of output frames generated for a given number of input frames can vary.

For example, suppose you are converting from 44100 Hz to 48000 Hz and using an input buffer with 960 frames. If you calculate the number of output frames you get:

    960 * 48000 * 44100 = 1044.897959...

You cannot generate a fractional number of frames. So the resampler will sometimes generate 1044 frames and sometimes 1045 frames. On average it will generate 1044.897959 frames. The resampler stores the fraction internally and keeps track of when to consume or generate a frame.

You can either use a fixed number of input frames or a fixed number of output frames. The other frame count will vary.

## Calling the Resampler with a fixed number of OUTPUT frames

In this example, suppose we have a fixed number of output frames and a variable number of input frames.

Assume you start with these variables and a method that returns the next input frame:

    float *outputBuffer;     // multi-channel buffer to be filled
    int    numOutputFrames;  // number of frames of output

The resampler has a method isWriteNeeded() that tells you whether to write to or read from the resampler.

    int outputFramesLeft = numOutputFrames;
    while (outputFramesLeft > 0) {
        if(resampler->isWriteNeeded()) {
            const float *frame = getNextInputFrame(); // you provide this
            resampler->writeNextFrame(frame);
        } else {
            resampler->readNextFrame(outputBuffer);
            outputBuffer += channelCount;
            outputFramesLeft--;
        }
    }

## Calling the Resampler with a fixed number of INPUT frames

In this example, suppose we have a fixed number of input frames and a variable number of output frames.

Assume you start with these variables:

    float *inputBuffer;     // multi-channel buffer to be consumed
    float *outputBuffer;    // multi-channel buffer to be filled
    int    numInputFrames;  // number of frames of input
    int    numOutputFrames = 0;
    int    channelCount;    // 1 for mono, 2 for stereo

    int inputFramesLeft = numInputFrames;
    while (inputFramesLeft > 0) {
        if(resampler->isWriteNeeded()) {
            resampler->writeNextFrame(inputBuffer);
            inputBuffer += channelCount;
            inputFramesLeft--;
        } else {
            resampler->readNextFrame(outputBuffer);
            outputBuffer += channelCount;
            numOutputFrames++;
        }
    }

## Deleting the Resampler

When you are done, you should delete the Resampler to avoid a memory leak.

    delete resampler;