Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 821cea93 authored by Glenn Kasten's avatar Glenn Kasten Committed by Android (Google) Code Review
Browse files

Merge "Add NBAIO support for more sample rates"

parents efced1d8 b64497eb
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -45,17 +45,15 @@ enum {
// Negotiation of format is based on the data provider and data sink, or the data consumer and
// data source, exchanging prioritized arrays of offers and counter-offers until a single offer is
// mutually agreed upon.  Each offer is an NBAIO_Format.  For simplicity and performance,
// NBAIO_Format is an enum that ties together the most important combinations of the various
// NBAIO_Format is a typedef that ties together the most important combinations of the various
// attributes, rather than a struct with separate fields for format, sample rate, channel count,
// interleave, packing, alignment, etc.  The reason is that NBAIO_Format tries to abstract out only
// the combinations that are actually needed within AudioFligner.  If the list of combinations grows
// the combinations that are actually needed within AudioFlinger.  If the list of combinations grows
// too large, then this decision should be re-visited.
enum NBAIO_Format {
    Format_Invalid,
    Format_SR44_1_C2_I16,   // 44.1 kHz PCM stereo interleaved 16-bit signed
    Format_SR48_C2_I16,     // 48 kHz PCM stereo interleaved 16-bit signed
    Format_SR44_1_C1_I16,   // 44.1 kHz PCM mono interleaved 16-bit signed
    Format_SR48_C1_I16,     // 48 kHz PCM mono interleaved 16-bit signed
// Sample rate and channel count are explicit, PCM interleaved 16-bit is assumed.
typedef unsigned NBAIO_Format;
enum {
    Format_Invalid
};

// Return the frame size of an NBAIO_Format in bytes
+85 −39
Original line number Diff line number Diff line
@@ -24,44 +24,55 @@ namespace android {

size_t Format_frameSize(NBAIO_Format format)
{
    switch (format) {
    case Format_SR44_1_C2_I16:
    case Format_SR48_C2_I16:
        return 2 * sizeof(short);
    case Format_SR44_1_C1_I16:
    case Format_SR48_C1_I16:
        return 1 * sizeof(short);
    case Format_Invalid:
    default:
        return 0;
    }
    return Format_channelCount(format) * sizeof(short);
}

size_t Format_frameBitShift(NBAIO_Format format)
{
    switch (format) {
    case Format_SR44_1_C2_I16:
    case Format_SR48_C2_I16:
        return 2;   // 1 << 2 == 2 * sizeof(short)
    case Format_SR44_1_C1_I16:
    case Format_SR48_C1_I16:
        return 1;   // 1 << 1 == 1 * sizeof(short)
    case Format_Invalid:
    default:
        return 0;
    }
    // sizeof(short) == 2, so frame size == 1 << channels
    return Format_channelCount(format);
}

enum {
    Format_SR_8000,
    Format_SR_11025,
    Format_SR_16000,
    Format_SR_22050,
    Format_SR_24000,
    Format_SR_32000,
    Format_SR_44100,
    Format_SR_48000,
    Format_SR_Mask = 7
};

enum {
    Format_C_1 = 0x08,
    Format_C_2 = 0x10,
    Format_C_Mask = 0x18
};

unsigned Format_sampleRate(NBAIO_Format format)
{
    switch (format) {
    case Format_SR44_1_C1_I16:
    case Format_SR44_1_C2_I16:
    if (format == Format_Invalid) {
        return 0;
    }
    switch (format & Format_SR_Mask) {
    case Format_SR_8000:
        return 8000;
    case Format_SR_11025:
        return 11025;
    case Format_SR_16000:
        return 16000;
    case Format_SR_22050:
        return 22050;
    case Format_SR_24000:
        return 24000;
    case Format_SR_32000:
        return 32000;
    case Format_SR_44100:
        return 44100;
    case Format_SR48_C1_I16:
    case Format_SR48_C2_I16:
    case Format_SR_48000:
        return 48000;
    case Format_Invalid:
    default:
        return 0;
    }
@@ -69,14 +80,14 @@ unsigned Format_sampleRate(NBAIO_Format format)

unsigned Format_channelCount(NBAIO_Format format)
{
    switch (format) {
    case Format_SR44_1_C1_I16:
    case Format_SR48_C1_I16:
    if (format == Format_Invalid) {
        return 0;
    }
    switch (format & Format_C_Mask) {
    case Format_C_1:
        return 1;
    case Format_SR44_1_C2_I16:
    case Format_SR48_C2_I16:
    case Format_C_2:
        return 2;
    case Format_Invalid:
    default:
        return 0;
    }
@@ -84,12 +95,47 @@ unsigned Format_channelCount(NBAIO_Format format)

NBAIO_Format Format_from_SR_C(unsigned sampleRate, unsigned channelCount)
{
    if (sampleRate == 44100 && channelCount == 2) return Format_SR44_1_C2_I16;
    if (sampleRate == 48000 && channelCount == 2) return Format_SR48_C2_I16;
    if (sampleRate == 44100 && channelCount == 1) return Format_SR44_1_C1_I16;
    if (sampleRate == 48000 && channelCount == 1) return Format_SR48_C1_I16;
    NBAIO_Format format;
    switch (sampleRate) {
    case 8000:
        format = Format_SR_8000;
        break;
    case 11025:
        format = Format_SR_11025;
        break;
    case 16000:
        format = Format_SR_16000;
        break;
    case 22050:
        format = Format_SR_22050;
        break;
    case 24000:
        format = Format_SR_24000;
        break;
    case 32000:
        format = Format_SR_32000;
        break;
    case 44100:
        format = Format_SR_44100;
        break;
    case 48000:
        format = Format_SR_48000;
        break;
    default:
        return Format_Invalid;
    }
    switch (channelCount) {
    case 1:
        format |= Format_C_1;
        break;
    case 2:
        format |= Format_C_2;
        break;
    default:
        return Format_Invalid;
    }
    return format;
}

// This is a default implementation; it is expected that subclasses will optimize this.
ssize_t NBAIO_Sink::writeVia(writeVia_t via, size_t total, void *user, size_t block)