Advertisement

error in recording sounds with sdl

Started by April 12, 2020 11:35 AM
1 comment, last by fleabay 4 years, 7 months ago

I write a program for learning english and I want to record sounds and my program didn't work at all I don't know why, this is the code:

#include <SDL.h>

#include <stdio.h>

#include <string>

#include <sstream>

//Recieved audio spec

SDL_AudioSpec gReceivedRecordingSpec;

SDL_AudioSpec gReceivedPlaybackSpec;

//Maximum recording time

const int MAX_RECORDING_SECONDS = 5;

//Maximum recording time plus padding

const int RECORDING_BUFFER_SECONDS = MAX_RECORDING_SECONDS + 1;

//Recording data buffer

Uint8* gRecordingBuffer = NULL;

//Size of data buffer

Uint32 gBufferByteSize = 0;

//Position in data buffer

Uint32 gBufferBytePosition = 0;

//Maximum position in data buffer for recording

Uint32 gBufferByteMaxPosition = 0;

void audioRecordingCallback(void* userdata, Uint8* stream, int len)

{

//Copy audio from stream

memcpy(&gRecordingBuffer[gBufferBytePosition], stream, len);

//Move along buffer

gBufferBytePosition += len;

}

void audioPlaybackCallback(void* userdata, Uint8* stream, int len)

{

//Copy audio to stream

memcpy(stream, &gRecordingBuffer[gBufferBytePosition], len);

//Move along buffer

gBufferBytePosition += len;

}

int main(int argv, char* argc[])

{

int ui = 0;

SDL_Init(SDL_INIT_AUDIO);

//Main loop flag

bool quit = false;

printf("0 for record and 1 for playback");

//Event handler

SDL_Event e;

SDL_AudioSpec recordingDeviceId;

SDL_AudioSpec playbackDeviceId;

while (!quit)

{

//Handle events on queue

while (SDL_PollEvent(&e) != 0)

{

//On key press

if (e.type == SDL_KEYDOWN)

{

SDL_AudioSpec desiredRecordingSpec;

SDL_zero(desiredRecordingSpec);

desiredRecordingSpec.freq = 44100;

desiredRecordingSpec.format = AUDIO_F32;

desiredRecordingSpec.channels = 2;

desiredRecordingSpec.samples = 4096;

desiredRecordingSpec.callback = audioRecordingCallback;

//Open recording device

SDL_AudioDeviceID recordingDeviceId = SDL_OpenAudioDevice(NULL, SDL_TRUE, &desiredRecordingSpec, &gReceivedRecordingSpec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);

//Device failed to open

if (recordingDeviceId == 0)

{

SDL_Log("Failed to open recording device!SDL Error : %s", SDL_GetError());

//Report error

}

//Device opened successfully

else

{

//Default audio spec

SDL_AudioSpec desiredPlaybackSpec;

SDL_zero(desiredPlaybackSpec);

desiredPlaybackSpec.freq = 44100;

desiredPlaybackSpec.format = AUDIO_F32;

desiredPlaybackSpec.channels = 2;

desiredPlaybackSpec.samples = 4096;

desiredPlaybackSpec.callback = audioPlaybackCallback;

//Open playback device

SDL_AudioDeviceID playbackDeviceId = SDL_OpenAudioDevice(NULL, SDL_FALSE, &desiredPlaybackSpec, &gReceivedPlaybackSpec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);

//Device failed to open

if (playbackDeviceId == 0)

{

//Report error

SDL_Log("Failed to open playback device! SDL Error: %s", SDL_GetError());

}

else

{

//Calculate per sample bytes

int bytesPerSample = gReceivedRecordingSpec.channels * (SDL_AUDIO_BITSIZE(gReceivedRecordingSpec.format) / 8);

//Calculate bytes per second

int bytesPerSecond = gReceivedRecordingSpec.freq * bytesPerSample;

//Calculate buffer size

gBufferByteSize = RECORDING_BUFFER_SECONDS * bytesPerSecond;

//Calculate max buffer use

gBufferByteMaxPosition = MAX_RECORDING_SECONDS * bytesPerSecond;

//Allocate and initialize byte buffer

gRecordingBuffer = new Uint8[gBufferByteSize];

memset(gRecordingBuffer, 0, gBufferByteSize);

//Start recording

if (e.key.keysym.sym == SDLK_0)

{

//Go back to beginning of buffer

gBufferBytePosition = 0;

//Start recording

SDL_PauseAudioDevice(recordingDeviceId, SDL_FALSE);

ui = 1;

}

//Start playback

if (e.key.keysym.sym == SDLK_1)

{

//Go back to beginning of buffer

gBufferBytePosition = 0;

//Start playback

SDL_PauseAudioDevice(playbackDeviceId, SDL_FALSE);

ui = 2;

}

}

//Updating recording

if (ui == 1)

{

//Lock callback

SDL_LockAudioDevice(recordingDeviceId);

//Finished recording

if (gBufferBytePosition > gBufferByteMaxPosition)

{

//Stop recording audio

SDL_PauseAudioDevice(recordingDeviceId, SDL_TRUE);

}

//Unlock callback

SDL_UnlockAudioDevice(recordingDeviceId);

}

//Updating playback

else if (ui == 2)

{

//Lock callback

SDL_LockAudioDevice(playbackDeviceId);

//Finished playback

if (gBufferBytePosition > gBufferByteMaxPosition)

{

//Stop playing audio

SDL_PauseAudioDevice(playbackDeviceId, SDL_TRUE);

}

//Unlock callback

SDL_UnlockAudioDevice(playbackDeviceId);

}

}

}

}

}

return 0;

}

None

tkialwan said:
I write a program

Do you know C++? Does it compile?

This is a butchered lazyfoo tutorial. Did you have the original working? There is a zip file at the bottom of that (lazyfoo.net audio recording) page with a working implementation. (I assume)

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

This topic is closed to new replies.

Advertisement