5.1 PCM Sound to 5.1 Dolby Digital в Windows C ++ для воспроизведения SPDIFC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 5.1 PCM Sound to 5.1 Dolby Digital в Windows C ++ для воспроизведения SPDIF

Сообщение Anonymous »

Я пытаюсь написать программу, которая может преобразовать 2.1 PCM Sound в 5.1 Dolby Digital Compressed Audio для использования с моим интерфейсом Optical Toslink/SPDIF USB. По причинам отладки я использую синусоидальный сигнал PCM на LF и RF-каналах, который впоследствии будет заменен схваткой на аудио-рендеринг/раковина Windows. Проблема: после сжатия с помощью LibavCodec буфер подходит для Windows (и USB -интерфейса) для обработки. Таким образом, Memcpy в петле бросает переполнение буфера. Вот код: < /p>
#include
#include
#include

#include
#include
#include
#include
#include
#include
#include

extern "C" {
#include
#include
#include
#include
#include
#include
}
// UUID defintion by the videolan vlc player project
#define DEFINE_VLC_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID DECLSPEC_SELECTANY name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }

#define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092 /* Sonic Foundry */
static const GUID __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF = { WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71} };

// Function to get the default audio endpoint
IMMDevice* GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role) {
HRESULT hr;
IMMDeviceEnumerator* enumerator = nullptr;
IMMDevice* device = nullptr;

hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&enumerator);
if (FAILED(hr)) {
std::cerr Release();
if (FAILED(hr)) {
std::cerr Format.nChannels = 2; // Stereo for S/PDIF
wf->Format.nSamplesPerSec = 48000;
wf->Format.wBitsPerSample = 16;
wf->Format.nBlockAlign = 6* 4; // wf->Format.wBitsPerSample / 8 * wf->Format.nChannels
wf->Format.nAvgBytesPerSec = wf->Format.nSamplesPerSec * wf->Format.nBlockAlign;
wf->Format.cbSize = sizeof(*wf) - sizeof(wf->Format);
wf->Samples.wValidBitsPerSample = wf->Format.wBitsPerSample;
wf->dwChannelMask = KSAUDIO_SPEAKER_5POINT1;
wf->SubFormat = subtype;
}

int main() {
// Sine wave parameters
double frequency = 220.0;
int sample_rate = 48000;
AVSampleFormat input_sample_fmt = AV_SAMPLE_FMT_S16;
int input_channels = 2;

// Output AC-3 parameters
int output_sample_rate = 44100;
AVSampleFormat output_sample_fmt = AV_SAMPLE_FMT_FLTP;
int output_channels = 6; // 5.1
int output_bit_rate = 64000;

// Initialize libavcodec
const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_AC3);
AVCodecContext* c = avcodec_alloc_context3(codec);

// Set output buffer size from IAudioClient::GetBufferSize UINT32 bufferFrameCount; audioClient->GetBufferSize(&bufferFrameCount); UINT32 bufferSizeInBit = bufferFrameCount /*Number of Frames*/ * waveFormat.Format.nBlockAlign /*Size of a Frame in Byte*/ * 8 /*Bits*/; c->bit_rate = bufferSizeInBit; c->sample_rate = 44100; c->sample_fmt = AV_SAMPLE_FMT_FLTP; c->ch_layout = AV_CHANNEL_LAYOUT_5POINT1; avcodec_open2(c, codec, nullptr);

c->bit_rate = 384000;
c->sample_rate = output_sample_rate;
c->sample_fmt = output_sample_fmt;
c->ch_layout = AV_CHANNEL_LAYOUT_5POINT1;

avcodec_open2(c, codec, nullptr);

AVFrame* frame = av_frame_alloc();
frame->nb_samples = c->frame_size;
frame->format = c->sample_fmt;
frame->ch_layout = c->ch_layout;
av_frame_get_buffer(frame, 0);

AVPacket& pkt = *av_packet_alloc();

// Initialize COM and WASAPI
CoInitialize(nullptr);

IMMDevice* renderDevice = GetDefaultAudioEndpoint(eRender, eMultimedia);
IAudioClient* audioClient;
renderDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void**)&audioClient);
WAVEFORMATEXTENSIBLE waveFormat;
WAVEFORMATEX* closestMatch = nullptr;
GUID ac3Subtype = __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF; // AC-3 GUID
configureSpdifWaveFormat(&waveFormat, output_sample_fmt, output_sample_rate, output_channels, ac3Subtype);
HRESULT hr = audioClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, (WAVEFORMATEX*)&waveFormat, &closestMatch);

if (FAILED(hr)) {
std::cerr
Обновленный код: < /p>
#include
#include
#include

#include
#include
#include
#include
#include
#include
#include

extern "C" {
#include
#include
#include
#include
#include
#include
}

#define DEFINE_VLC_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
EXTERN_C const GUID DECLSPEC_SELECTANY name \
= { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
#define BIT_TO_BYTE / 8

#define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092 /* Sonic Foundry */
static const GUID __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF = { WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71} };

// Function to get the default audio endpoint
IMMDevice* GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role) {
HRESULT hr;
IMMDeviceEnumerator* enumerator = nullptr;
IMMDevice* device = nullptr;

hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&enumerator);
if (FAILED(hr)) {
std::cerr Release();
if (FAILED(hr)) {
std::cerr Format.nChannels = 2; // Stereo for S/PDIF
wf->Format.nSamplesPerSec = 48000;
wf->Format.wBitsPerSample = 16;
wf->Format.nBlockAlign = wf->Format.wBitsPerSample / 8 * wf->Format.nChannels;
wf->Format.nAvgBytesPerSec = wf->Format.nSamplesPerSec * wf->Format.nBlockAlign;
wf->Format.cbSize = sizeof(*wf) - sizeof(wf->Format);
wf->Samples.wValidBitsPerSample = wf->Format.wBitsPerSample;
wf->dwChannelMask = KSAUDIO_SPEAKER_5POINT1;
wf->SubFormat = subtype;
}

int main() {
// Sine wave parameters
double frequency = 220.0;
int sample_rate = 48000;
AVSampleFormat input_sample_fmt = AV_SAMPLE_FMT_S16;
int input_channels = 2;

// Output AC-3 parameters
int output_sample_rate = 44100;
AVSampleFormat output_sample_fmt = AV_SAMPLE_FMT_FLTP;
int output_channels = 6; // 5.1

// Initialize COM and WASAPI
CoInitialize(nullptr);

IMMDevice* renderDevice = GetDefaultAudioEndpoint(eRender, eMultimedia);
IAudioClient* audioClient;
renderDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void**)&audioClient);
WAVEFORMATEXTENSIBLE waveFormat;
WAVEFORMATEX* closestMatch = nullptr;
GUID ac3Subtype = __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF; // AC-3 GUID
configureSpdifWaveFormat(&waveFormat, output_sample_fmt, output_sample_rate, output_channels, ac3Subtype);
HRESULT hr = audioClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, (WAVEFORMATEX*)&waveFormat, &closestMatch);

if (FAILED(hr)) {
std::cerr nb_samples = c->frame_size;
frame->format = c->sample_fmt;
frame->ch_layout = c->ch_layout;
av_frame_get_buffer(frame, 0);

std::vector output_buffer(c->frame_size * output_channels);

AVPacket& pkt = *av_packet_alloc();

double time = 0.0;
while (true) {
for (int i = 0; i < c->frame_size; ++i) {
double sample = sin(2.0 * M_PI * frequency * time);
int16_t sample_int = static_cast(sample * 32767.0);

output_buffer[i * 6 + 0] = sample_int; // Front L
output_buffer[i * 6 + 1] = sample_int; // Front R
output_buffer[i * 6 + 2] = 0; // Center
output_buffer[i * 6 + 3] = 0; // LFE
output_buffer[i * 6 + 4] = 0; // Rear L
output_buffer[i * 6 + 5] = 0; // Rear R

time += 1.0 / sample_rate;
}
memcpy(frame->data[0], output_buffer.data(), output_buffer.size() * sizeof(int16_t));

int ret = avcodec_send_frame(c, frame);
if (ret < 0) break;

while (ret >= 0) {
ret = avcodec_receive_packet(c, &pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
if (ret < 0) break;

BYTE* renderData;
HRESULT hj = renderClient->GetBuffer(pkt.size, &renderData);
std::cout

Подробнее здесь: https://stackoverflow.com/questions/795 ... f-playback
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»