Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If real-time parsing LTC is implemented, #70

Open
conducive2 opened this issue May 8, 2024 · 0 comments
Open

If real-time parsing LTC is implemented, #70

conducive2 opened this issue May 8, 2024 · 0 comments

Comments

@conducive2
Copy link

If real-time parsing LTC is implemented, I tried it. One frame will always be lost from transmission to parsing and printing, and there is a delay of about one second. This has troubled me for a long time and I don’t know how to solve it.

`
void Myltc(ltcsnd_sample_t* sound, int bufferSize) {
LTCDecoder* decoder = ltc_decoder_create(1920, 32);
LTCFrameExt frame;
ltcsnd_sample_t* soundCopy = (ltcsnd_sample_t*)malloc(bufferSize * sizeof(ltcsnd_sample_t));
if (soundCopy == NULL) {
fprintf(stderr, "Failed to allocate memory for sound copy.\n");
return;
}
memcpy(soundCopy, sound, bufferSize * sizeof(ltcsnd_sample_t));
ltc_decoder_write(decoder, soundCopy, bufferSize, 0);
while (ltc_decoder_read(decoder, &frame)) {
SMPTETimecode stime;
ltc_frame_to_time(&stime, &frame.ltc, 1);
printf("%02d:%02d:%02d%c%02d\n", stime.hours,
stime.mins, stime.secs, (frame.ltc.dfbit) ? '.' : ':',
stime.frame);
}
free(soundCopy);
ltc_decoder_free(decoder);
}
void CALLBACK waveInProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) {
if (uMsg == WIM_DATA) {
paData* data = (paData*)dwInstance;
WAVEHDR* waveHeader = (WAVEHDR*)dwParam1;
if (waveHeader->dwBytesRecorded > 0) {
Myltc((ltcsnd_sample_t*)waveHeader->lpData, waveHeader->dwBytesRecorded / sizeof(ltcsnd_sample_t));
}
waveInAddBuffer(hwi, waveHeader, sizeof(WAVEHDR));
}
}
DWORD WINAPI RecordingThread(LPVOID lpParameter) {
paData* data = (paData*)lpParameter;
HWAVEIN hWaveIn;
WAVEHDR waveHeader;
MMRESULT result;
WAVEFORMATEX waveFormat;

waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nChannels = NUM_CHANNELS;
waveFormat.nSamplesPerSec = SAMPLE_RATE;
waveFormat.nAvgBytesPerSec = SAMPLE_RATE * NUM_CHANNELS;
waveFormat.nBlockAlign = NUM_CHANNELS;
waveFormat.wBitsPerSample = 8;
waveFormat.cbSize = 0;
 
result = waveInOpen(&hWaveIn, WAVE_MAPPER, &waveFormat, (DWORD_PTR)waveInProc, (DWORD_PTR)&data, CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
    printf("Failed to open audio input device.\n");
    return 1;
}

int numSamples = SAMPLE_RATE * NUM_CHANNELS;
data->maxFrameIndex = numSamples;
data->frameIndex = 0;
data->recordedSamples = (float*)malloc(numSamples * sizeof(float));
if (data->recordedSamples == NULL) {
    printf("Failed to allocate memory for recording.\n");
    waveInClose(hWaveIn);
    return 1;
}

waveHeader.lpData = (LPSTR)malloc(numSamples);
waveHeader.dwBufferLength = numSamples;
waveHeader.dwFlags = 0;
waveHeader.dwLoops = 1;
waveHeader.dwUser = 0;
waveHeader.lpNext = NULL;
waveInPrepareHeader(hWaveIn, &waveHeader, sizeof(WAVEHDR));
waveInAddBuffer(hWaveIn, &waveHeader, sizeof(WAVEHDR));

result = waveInStart(hWaveIn);
if (result != MMSYSERR_NOERROR) {
    printf("Failed to start recording.\n");
    free(data->recordedSamples);
    free(waveHeader.lpData);
    waveInClose(hWaveIn);
    return 1;
}

printf("Recording...\n");
printf("Press Enter to stop recording.\n");
getchar();

waveInStop(hWaveIn);
waveInReset(hWaveIn);
waveInUnprepareHeader(hWaveIn, &waveHeader, sizeof(WAVEHDR));
free(waveHeader.lpData);
waveInClose(hWaveIn);
free(data->recordedSamples);

return 0;

}

int main() {
paData data;
HANDLE hRecordingThread;
DWORD dwThreadId;

hRecordingThread = CreateThread(NULL, 0, RecordingThread, &data, 0, &dwThreadId);
if (hRecordingThread == NULL) {
    printf("Failed to create recording thread.\n");
    return 1;
}
 
WaitForSingleObject(hRecordingThread, INFINITE);
CloseHandle(hRecordingThread);

return 0;

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant