davs igen... Lang tid siden jeg har haft tid til at programerer.. Men nu har jeg rodet lidt rundt og er igang med at lave en lyd test funktion som skal blive ved med at spille dem samme tone igen og igen indtil man ændre den.... jeg har følgende kode:
/* The original version of this program can be found at http://damb.dk */
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MY_PI 3.141593
#define SAMPLE_RATE 22050
#define A_NODE 440
#define As_NODE 466
#define B_NODE 494
#define C_NODE 523
#define Cs_NODE 554
#define D_NODE 587
#define Ds_Node 622
#define E_NODE 659
#define F_NODE 698
#define Fs_NODE 740
#define G_NODE 784
#define Gs_NODE 831
HWAVEOUT WaveHandle;
WAVEFORMATEX WaveFormat;
WAVEHDR WaveHeader;
MMRESULT MMResult;
/* 440.000 466.163 493.883 523.251 554.365 587.329 622.253 659.255 698.456 739.988 783.991 830.609 */
void CALLBACK CallBackProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
if(uMsg == WOM_DONE)
{
MMRESULT MMResult;
WAVEHDR *WaveHeader = (WAVEHDR *)dwParam1;
if((MMResult = waveOutUnprepareHeader(hwo, WaveHeader, sizeof(WAVEHDR))) != MMSYSERR_NOERROR)
{
char Text[256];
waveOutGetErrorText(MMResult, Text, sizeof(Text));
printf("Failed to unprepare: %sn", Text);
}
free(WaveHeader->lpData);
}
}
int x;
char *buffer;
int time;
void create(int freq, int duration)
{
time = duration;
buffer = (char *)malloc(SAMPLE_RATE*duration/1000);
for (x = 0; x < SAMPLE_RATE*duration/1000; x++)
buffer[x] = (char )(128.0 + (127.0 * sin(2.0 * MY_PI * freq * x / SAMPLE_RATE)));
WaveHeader.lpData = buffer;
WaveHeader.dwBufferLength = time*SAMPLE_RATE/1110;
WaveHeader.dwBytesRecorded = time*SAMPLE_RATE/1110;
WaveHeader.dwLoops = 1;
WaveHeader.dwFlags = WHDR_BEGINLOOP | WHDR_ENDLOOP;
}
DWORD WINAPI PlayTone(LPVOID parameter)
//bool PlayTone()
{
while(true)
{
if(waveOutPrepareHeader(WaveHandle, &WaveHeader, sizeof(WaveHeader)) != MMSYSERR_NOERROR)
{
char Text[256];
waveOutGetErrorText(MMResult, Text, sizeof(Text));
printf("Failed to prepare header: %s \n",Text);
return false;
}
if((MMResult = waveOutWrite(WaveHandle, &WaveHeader, sizeof(WaveHeader))) != MMSYSERR_NOERROR)
{
char Text[256];
waveOutGetErrorText(MMResult, Text, sizeof(Text));
printf("Failed to write: %sn", Text);
return false;
}
while(!(WaveHeader.dwFlags & WHDR_DONE))
{ Sleep(1);
}
//Sleep(time);
}
return true;
}
int main(void)
{
WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nChannels = 1;
WaveFormat.nSamplesPerSec = SAMPLE_RATE;
WaveFormat.nAvgBytesPerSec = SAMPLE_RATE;
WaveFormat.nBlockAlign = 1;
WaveFormat.wBitsPerSample = 8;
WaveFormat.cbSize = 0;
if(waveOutOpen(&WaveHandle, WAVE_MAPPER, &WaveFormat, (DWORD )CallBackProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
{
printf("Failed to open Wave!n");
return false;
}
memset(&WaveHeader, 0, sizeof(WaveHeader));
create(1000, 1000);
CreateThread(NULL,0,PlayTone,NULL,0,NULL);
int hz = 10;
while(true)
{
printf("Indtast tone: ");
scanf("%d",&hz);
create(hz, 1000);
}
while(!(WaveHeader.dwFlags & WHDR_DONE))
Sleep(10);
if((MMResult = waveOutClose(WaveHandle)) != MMSYSERR_NOERROR)
{
char Text[256];
waveOutGetErrorText(MMResult, Text, sizeof(Text));
printf("Failed to close: %sn", Text);
return false;
}
/* PlayTone(A_NODE, 1000);
PlayTone(As_NODE, 1000);
PlayTone(B_NODE, 1000);
PlayTone(C_NODE, 1000);
PlayTone(Cs_NODE, 1000);
PlayTone(D_NODE, 1000);
PlayTone(Ds_Node, 1000);
PlayTone(E_NODE, 1000);
PlayTone(F_NODE, 1000);
PlayTone(Fs_NODE, 1000);
PlayTone(G_NODE, 1000);
PlayTone(Gs_NODE, 1000);*/
return 0;
}
Det er så meningen at den skal gøre det..... Men jeg laver så et loop der bliver ved med at spille dem samme tone igen... Men jeg kan ikke inde ud af at sætte lyd blokke i kø...
Den kode jeg har nu lagger rimlig meget som du kan hører... Hvad skal jeg gøre for at sætte lydblokkene i kø?