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

Using winmm.lib to decode real time LTC input? #30

Open
antithing opened this issue Feb 5, 2018 · 4 comments
Open

Using winmm.lib to decode real time LTC input? #30

antithing opened this issue Feb 5, 2018 · 4 comments

Comments

@antithing
Copy link

Hi, and thank you very much for this code. I am streaming in LTC from a master clock, and am attempting to pick it up using winmm.lib, which streams the signal into a:

short int waveIn[44100 * 3];

Am i correct in thinking that libltc decoding needs an unsigned char* ?

Can I use something similar to the below code, or do i need a queue of some kind for real time streaming?

The signal is checked and is valid ltc.

Thanks!

void Start()
{
	const int NUMPTS = 44100 * 3;   // 3 seconds
	int sampleRate = 44100;

	HWAVEIN      hWaveIn;
	MMRESULT result;

	WAVEFORMATEX pFormat;
	pFormat.wFormatTag = WAVE_FORMAT_PCM;     // simple, uncompressed format
	pFormat.nChannels = 1;                    //  1=mono, 2=stereo
	pFormat.nSamplesPerSec = sampleRate;      // 44100
	pFormat.nAvgBytesPerSec = sampleRate * 2;   // = nSamplesPerSec * n.Channels *    wBitsPerSample/8
	pFormat.nBlockAlign = 2;                  // = n.Channels * wBitsPerSample/8
	pFormat.wBitsPerSample = 16;              //  16 for high quality, 8 for telephone-grade
	pFormat.cbSize = 0;

	// Specify recording parameters
	result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat,
		0L, 0L, WAVE_FORMAT_DIRECT);

	WAVEHDR      WaveInHdr;
	WaveInHdr.lpData = (LPSTR)waveIn;
	WaveInHdr.dwBufferLength = NUMPTS * 2;
	WaveInHdr.dwBytesRecorded = 0;
	WaveInHdr.dwUser = 0L;
	WaveInHdr.dwFlags = 0L;
	WaveInHdr.dwLoops = 0L;
	waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

	// Insert a wave input buffer
	result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

	// decode stuff
	int apv = 1920;
	ltcsnd_sample_t sound[BUFFER_SIZE];
	size_t n =0;
	long int total =0;
	LTCDecoder *decoder;
	LTCFrameExt frame;
	decoder = ltc_decoder_create(apv, 32);

	// Commence sampling input
	result = waveInStart(hWaveIn);

	int num = 0;
	while (num < 200)
	{
		cout << "recording..." << endl;

		sound[0] = waveIn[0]; // this is wrong. Do I need to convert to unsigned char?

		ltc_decoder_write(decoder, sound, n, total); // do i need to write before i read?
		
                ltc_decoder_read(decoder, &frame);
		SMPTETimecode stime;
		ltc_frame_to_time(&stime, &frame.ltc, 1);

		std::cout << stime.frame << std::endl;		  // prints a strange character	
		
		num += 1;
	}
	
	waveInClose(hWaveIn);
	ltc_decoder_free(decoder);
	PlayRecord();
}
@x42
Copy link
Owner

x42 commented Feb 5, 2018

Perhaps try using 8bit directly (instead of 16)

wBitsPerSample = 16;              //  16 for high quality, 8 for telephone-grade

Note you'll also have to set nAvgBytesPerSec = nSamplesPerSec in that case.

Alternatively sound[0] = waveIn[0] / 256 but I don't know if winmm produces signed or unsigned data.
You'll also have to convert sound[0]... sound[total -1] , not just the first element of the data-buffer.

@antithing
Copy link
Author

Thank you. I will try that.
What is the best way to copy the right amount of data from waveIn to sound?
Sound = waveIn gives an error, do I need to do a for loop for the max buffer size? (waveIn[44100 * 3];)
Thanks again!

@antithing
Copy link
Author

Hi, sorry to bug you again, I am still stuck on this. i have adjusted my main loop above to:

while (num < 200)
	{
		cout << "recording..." << endl;

		for (unsigned int i = 0; i < BUFFER_SIZE; ++i) {
			
			sound[i] = waveIn[i] / 256;
			ltc_decoder_write(decoder, sound, n, total);
		}
				
	    ltc_decoder_read(decoder, &frame);
	    SMPTETimecode stime;


		ltc_frame_to_time(&stime, &frame.ltc, 1);

		std::cout << stime.frame << std::endl;			
		
		num += 1;
	}

But it just prints an empty line. Where am i going wrong? Thanks again.

@conducive2
Copy link

@antithing Hello, have you solved this problem? I also encountered similar problems.

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

3 participants