|
|
|
|
@ -10,7 +10,8 @@ int voice_file_encode(const int16_t* pcm, size_t count, int rate, int channels,
|
|
|
|
|
const char* path, struct attachment_info* info) { |
|
|
|
|
static const int bitrates[] = {16000, 32000, 64000}, complexities[] = {2, 5, 10}; |
|
|
|
|
static const char levels[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
|
|
|
if (!pcm || !info || !path || rate <= 0 || (channels != 1 && channels != 2) || preset < 0 || preset > 2) goto invalid; |
|
|
|
|
if (!pcm || !info || !path || (rate != 8000 && rate != 12000 && rate != 16000 && rate != 24000 && rate != 48000) || |
|
|
|
|
(channels != 1 && channels != 2) || preset < 0 || preset > 2) goto invalid; |
|
|
|
|
size_t frame = (size_t)(rate / 50) * channels, samples = count / frame * frame; |
|
|
|
|
if (samples < (size_t)rate * channels / 2 || samples / channels / rate > 86400) goto invalid; |
|
|
|
|
opus_codec_encoder_t* encoder = opus_codec_encoder_create(rate, channels); |
|
|
|
|
@ -52,7 +53,7 @@ done:
|
|
|
|
|
opus_codec_encoder_destroy(encoder); |
|
|
|
|
if (error) { |
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "voice_file: encoding/write failed path=%s", path); |
|
|
|
|
if (remove(path)) DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "voice_file: partial file cleanup failed path=%s", path); |
|
|
|
|
if (file && remove(path)) DEBUG_WARN(DEBUG_CATEGORY_MEDIA, "voice_file: partial file cleanup failed path=%s", path); |
|
|
|
|
} else DEBUG_INFO(DEBUG_CATEGORY_MEDIA, "voice_file: prepared path=%s duration=%ums rate=%d channels=%d", path, info->duration_ms, rate, channels); |
|
|
|
|
return error; |
|
|
|
|
invalid: |
|
|
|
|
|