2

我找到了一个有助于将 WAV 文件转换为 Flac 的库: https ://github.com/jhurt/wav_to_flac

也成功将 Flac 编译到平台,它工作正常。

在捕获 wav 格式的音频以将其转换为 Flac 然后发送到我的服务器后,我一直在使用这个库。

问题是音频文件可能很长,然后浪费了宝贵的时间。

问题是我想将音频编码为 Flac 格式,并在捕获时而不是在捕获停止后同时将其发送到服务器,所以,我需要一个关于如何做到这一点的帮助(直接从音频中编码 Flac 所以我可以将它发送到我的服务器)...

4

3 回答 3

4

在我的名为 libsprec 的库中,您可以看到录制 WAV 文件(此处)和将其转换为 FLAC(此处)的示例。(致谢:录音部分在很大程度上依赖于 Erica Sadun 的作品。)

现在,如果您想一步完成,您也可以这样做。诀窍是你必须先初始化音频队列FLAC 库,然后“交错”对它们的调用,即当你在音频队列的回调函数中获得一些音频数据时,你立即 FLAC-对其进行编码。

但是,我认为这不会比在两个单独的步骤中记录和编码要快得多。处理的主要部分是编码本身的记录和数学运算,因此重新读取相同的缓冲区(或者我敢说,甚至是文件!)不会增加太多的处理时间。

也就是说,您可能想做这样的事情:

// First, we initialize the Audio Queue

AudioStreamBasicDescription desc;
desc.mFormatID = kAudioFormatLinearPCM;
desc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
desc.mReserved = 0;
desc.mSampleRate = SAMPLE_RATE;
desc.mChannelsPerFrame = 2; // stereo (?)
desc.mBitsPerChannel = BITS_PER_SAMPLE;
desc.mBytesPerFrame = BYTES_PER_FRAME;
desc.mFramesPerPacket = 1;
desc.mBytesPerPacket = desc.mFramesPerPacket * desc.mBytesPerFrame;

AudioQueueRef queue;

status = AudioQueueNewInput(
    &desc,
    audio_queue_callback, // our custom callback function
    NULL,
    NULL,
    NULL,
    0,
    &queue
);

if (status)
    return status;

AudioQueueBufferRef buffers[NUM_BUFFERS];

for (i = 0; i < NUM_BUFFERS; i++) {
    status = AudioQueueAllocateBuffer(
        queue,
        0x5000, // max buffer size
        &buffers[i]
    );
    if (status)
        return status;

    status = AudioQueueEnqueueBuffer(
        queue,
        buffers[i],
        0,
        NULL
    );
    if (status)
        return status;
}

// Then, we initialize the FLAC encoder:
FLAC__StreamEncoder *encoder;
FLAC__StreamEncoderInitStatus status;
FILE *infile;
const char *dataloc;
uint32_t rate;      /* sample rate */
uint32_t total;     /* number of samples in file */
uint32_t channels;  /* number of channels */
uint32_t bps;       /* bits per sample */
uint32_t dataoff;   /* offset of PCM data within the file */
int err;

/*
 * BUFFSIZE samples * 2 bytes per sample * 2 channels
 */
FLAC__byte buffer[BUFSIZE * 2 * 2];

/*
 * BUFFSIZE samples * 2 channels
 */
FLAC__int32 pcm[BUFSIZE * 2];


/*
 * Create and initialize the FLAC encoder
 */
encoder = FLAC__stream_encoder_new();
if (!encoder)
    return -1;


FLAC__stream_encoder_set_verify(encoder, true);
FLAC__stream_encoder_set_compression_level(encoder, 5);
FLAC__stream_encoder_set_channels(encoder, NUM_CHANNELS); // 2 for stereo
FLAC__stream_encoder_set_bits_per_sample(encoder, BITS_PER_SAMPLE); // 32 for stereo 16 bit per channel
FLAC__stream_encoder_set_sample_rate(encoder, SAMPLE_RATE);

status = FLAC__stream_encoder_init_stream(encoder, flac_callback, NULL, NULL, NULL, NULL);
if (status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
    return -1;


// We now start the Audio Queue...
status = AudioQueueStart(queue, NULL);

// And when it's finished, we clean up the FLAC encoder...
FLAC__stream_encoder_finish(encoder);
FLAC__stream_encoder_delete(encoder);

// and the audio queue and its belongings too
AudioQueueFlush(queue);
AudioQueueStop(queue, false);

for (i = 0; i < NUM_BUFFERS; i++)
    AudioQueueFreeBuffer(queue, buffers[i]);

AudioQueueDispose(queue, true);

// In the audio queue callback function, we do the encoding:

void audio_queue_callback(
    void *data,
    AudioQueueRef inAQ,
    AudioQueueBufferRef buffer,
    const AudioTimeStamp *start_time,
    UInt32 num_packets,
    const AudioStreamPacketDescription *desc
)
{
    unsigned char *buf = buffer->mAudioData;

    for (size_t i = 0; i < num_packets * channels; i++) {
        uint16_t msb = *(uint8_t *)(buf + i * 2 + 1);
        uint16_t usample = (msb << 8) | lsb;

        union {
            uint16_t usample;
            int16_t  ssample;
        } u;

        u.usample = usample;
        pcm[i] = u.ssample;
    }

    FLAC__bool succ = FLAC__stream_encoder_process_interleaved(encoder, pcm, num_packets);
    if (!succ)
        // handle_error();
}

// Finally, in the FLAC stream encoder callback:

FLAC__StreamEncoderWriteStatus flac_callback(
    const FLAC__StreamEncoder *encoder,
    const FLAC__byte buffer[],
    size_t bytes,
    unsigned samples,
    unsigned current_frame,
    void *client_data
)
{
    // Here process `buffer' and stuff,
    // then:

    return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
}

不客气。

于 2013-08-07T06:00:05.270 回答
1

您的问题不是很具体,但您需要使用Audio Recording Services,它可以让您以块的形式访问音频数据,然后将您从那里获得的数据移动到 FLAC 编码器的流接口中。您不能使用您链接到的 WAV 到 FLAC 程序,您必须自己访问 FLAC 库。API 文档在这里

关于如何在此处使用回调的示例。

于 2013-07-31T08:09:53.007 回答
0

您不能使用音频队列服务以 wav 录制音频并使用您的 lib 处理输出数据包吗?

苹果开发文档编辑:“编写 AIFF 和 WAV 文件的应用程序必须在录制结束时更新数据头的大小字段——如果在头最终确定之前中断录制,这可能导致文件不可用——或者他们必须更新大小记录每包数据后的字段,效率低下。”

显然,动态编码 wav 文件似乎相当困难

于 2013-08-01T11:27:27.213 回答