Error creating the secondary buffer DirectSound

154 views Asked by At

I don't know if I should reply to my previous question or I should to ask a new one

For a project for my university I have to create a client/server application where the client and the server communicate through sockets and the client streams WAV files from the server using DirectSound API

the server and the client communication through sockets its working but when I am choose which file to play I am getting this message: enter image description here

 // Initialize DirectSound
    IDirectSound8* dsound;
    if (DirectSoundCreate8(NULL, &dsound, NULL) != DS_OK) {
        std::cout << "Error initializing DirectSound" << std::endl;
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }

    if (dsound->SetCooperativeLevel(GetDesktopWindow(), DSSCL_PRIORITY) != DS_OK) {
        std::cout << "Error setting cooperative level" << std::endl;
        dsound->Release();
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }

    // Create primary buffer
    DSBUFFERDESC primary_buffer_desc;
    memset(&primary_buffer_desc, 0, sizeof(primary_buffer_desc));
    primary_buffer_desc.dwSize = sizeof(primary_buffer_desc);
    primary_buffer_desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
    IDirectSoundBuffer* primary_buffer;
    if (dsound->CreateSoundBuffer(&primary_buffer_desc, &primary_buffer, NULL) != DS_OK) {
        std::cout << "Error creating primary buffer" << std::endl;
        dsound->Release();
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }

    // Set primary buffer format
    WAVEFORMATEX primary_buffer_format;
    memset(&primary_buffer_format, 0, sizeof(primary_buffer_format));
    primary_buffer_format.wFormatTag = WAVE_FORMAT_PCM;
    primary_buffer_format.nChannels = 2;
    primary_buffer_format.nSamplesPerSec = 44100;
    primary_buffer_format.wBitsPerSample = 16;
    primary_buffer_format.nBlockAlign = primary_buffer_format.nChannels * primary_buffer_format.wBitsPerSample / 8;
    primary_buffer_format.nAvgBytesPerSec = primary_buffer_format.nSamplesPerSec * primary_buffer_format.nBlockAlign;
    if (primary_buffer->SetFormat(&primary_buffer_format) != DS_OK) {
        std::cout << "Error setting primary buffer format" << std::endl;
        primary_buffer->Release();
        dsound->Release();
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }

    // Create secondary buffer
    DSBUFFERDESC secondary_buffer_desc;
    memset(&secondary_buffer_desc, 0, sizeof(secondary_buffer_desc));
    secondary_buffer_desc.dwSize = sizeof(secondary_buffer_desc);
    secondary_buffer_desc.dwFlags = 0;
    secondary_buffer_desc.dwBufferBytes = file_size;
    secondary_buffer_desc.lpwfxFormat = &primary_buffer_format;

    IDirectSoundBuffer* secondary_buffer;
    if (dsound->CreateSoundBuffer(&secondary_buffer_desc, &secondary_buffer, NULL) != DS_OK) {
        std::cout << "Error creating secondary buffer" << std::endl;
        primary_buffer->Release();
        dsound->Release();
        closesocket(socket_client);
        WSACleanup();
        return 1;
    }

DirectSound documentation is confusing me and I can't find why is not working properly

0

There are 0 answers