パソコンから20Hzの矩形波をライン出力してみました。LEDが8個並んでいるのはいずれシリアルパラレル変換するためです。今回は並列に接続しているので同じように動作します。下記のコードは4CH出力していますが、動画のLEDのボードに接続しているのはデバイス0のLeftです。
#include <iostream> #include <string> #include <windows.h> #include <MMSystem.h> #pragma comment (lib, "winmm.lib") int main() { WAVEFORMATEX wfe; static HWAVEOUT hWaveOut; static WAVEHDR whdr; static LPBYTE lpWave; WAVEFORMATEX wfe2; static HWAVEOUT hWaveOut2; static WAVEHDR whdr2; static LPBYTE lpWave2; int i, len, start, end; int frequency = 20; int terms = 10; int sampling = 192000; wfe.wFormatTag = WAVE_FORMAT_PCM; wfe.nChannels = 2; wfe.wBitsPerSample = 8; wfe.nBlockAlign = wfe.nChannels * wfe.wBitsPerSample / 8; wfe.nSamplesPerSec = sampling; wfe.nAvgBytesPerSec = wfe.nSamplesPerSec * wfe.nBlockAlign; waveOutOpen(&hWaveOut, 0, &wfe, 0, 0, CALLBACK_NULL); lpWave = (LPBYTE)calloc(wfe.nAvgBytesPerSec, terms * wfe.nChannels); wfe2.wFormatTag = WAVE_FORMAT_PCM; wfe2.nChannels = 2; wfe2.wBitsPerSample = 8; wfe2.nBlockAlign = wfe2.nChannels * wfe2.wBitsPerSample / 8; wfe2.nSamplesPerSec = sampling; wfe2.nAvgBytesPerSec = wfe2.nSamplesPerSec * wfe2.nBlockAlign; waveOutOpen(&hWaveOut2, 2, &wfe2, 0, 0, CALLBACK_NULL); lpWave2 = (LPBYTE)calloc(wfe2.nAvgBytesPerSec, terms * wfe2.nChannels); // 立ち上がりは出力を0にする for (i = 0; i < sampling * terms * wfe.nChannels; i++) { lpWave[i] = 128; lpWave2[i] = 128; } len = sampling / frequency; // 最初の8サイクルは出力しない start = len * 8 * wfe.nChannels; start = 0; // 8サイクル波形を出力する end = start + len * 32 * wfe.nChannels; printf("start=%d, end=%d\n", start, end); for (i = start; i < end; i++) { if ((i % 2) == 0) { if ((i/2) % len < len / 2) { lpWave[i] = 228; } else { lpWave[i] = 28; }}} len /= 2; for (i = start; i < end; i++) { if ((i % 2) == 1) { if ((i / 2) % len < len / 2) { lpWave[i] = 228; } else { lpWave[i] = 28; }}} len *= 2; for (i = start; i < end; i++) { if ((i % 2) == 1) { if ((i / 2) % len < len / 2) { lpWave2[i] = 228; } else { lpWave2[i] = 28; } } } len /= 2; for (i = start; i < end; i++) { if ((i % 2) == 0) { if ((i / 2) % len < len / 2) { lpWave2[i] = 228; } else { lpWave2[i] = 28; } } } whdr.lpData = (LPSTR)lpWave; whdr.dwBufferLength = wfe.nAvgBytesPerSec * terms; whdr.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP; whdr.dwLoops = 1; waveOutPrepareHeader(hWaveOut, &whdr, sizeof(WAVEHDR)); waveOutWrite(hWaveOut, &whdr, sizeof(WAVEHDR)); whdr2.lpData = (LPSTR)lpWave2; whdr2.dwBufferLength = wfe.nAvgBytesPerSec * terms; whdr2.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP; whdr2.dwLoops = 1; waveOutPrepareHeader(hWaveOut2, &whdr2, sizeof(WAVEHDR)); waveOutWrite(hWaveOut2, &whdr2, sizeof(WAVEHDR)); char str[128]; std::cout << "hello, world\n"; std::cin >> str;