#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#pragma comment(lib, "winmm.lib")
#define ID_RECORD 101
#define ID_STOP 102
#define ID_PLAY 103
HINSTANCE hInst;
int main()
{
static TCHAR szFileName[] = TEXT ("record.wav") ;
static WORD wDeviceID ;
DWORD dwError ;
MCI_GENERIC_PARMS mciGeneric ;
MCI_OPEN_PARMS mciOpen ;
MCI_RECORD_PARMS mciRecord ;
MCI_SAVE_PARMS mciSave ;
// Open waveform audio
mciOpen.dwCallback = 0 ;
mciOpen.wDeviceID = 0 ;
mciOpen.lpstrDeviceType = TEXT ("waveaudio") ;
mciOpen.lpstrElementName = TEXT ("") ;
mciOpen.lpstrAlias = NULL ;
dwError = mciSendCommand (0, MCI_OPEN,
MCI_WAIT | MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD_PTR) &mciOpen) ;
if (dwError != 0)
{
return TRUE ;
}
wDeviceID = mciOpen.wDeviceID ;
// Begin recording
//mciRecord.dwCallback =(DWORD_PTR)hWnd ;
mciRecord.dwFrom = 0 ;
mciRecord.dwTo = 0 ;
mciSendCommand (wDeviceID, MCI_RECORD, MCI_NOTIFY,(DWORD_PTR) &mciRecord) ;
Sleep(5000);
mciGeneric.dwCallback = 0 ;
mciSendCommand (wDeviceID, MCI_STOP, MCI_WAIT,(DWORD_PTR) &mciGeneric) ;
// Save the file
mciSave.dwCallback = 0 ;
mciSave.lpfilename = szFileName ;
mciSendCommand (wDeviceID, MCI_SAVE, MCI_WAIT | MCI_SAVE_FILE,
(DWORD_PTR) &mciSave) ;
// Close the waveform device
mciSendCommand (wDeviceID, MCI_CLOSE, MCI_WAIT,
(DWORD_PTR) &mciGeneric) ;
mciOpen.dwCallback = 0 ;
mciOpen.wDeviceID = 0 ;
mciOpen.lpstrDeviceType = NULL ;
mciOpen.lpstrElementName = szFileName ;
mciOpen.lpstrAlias = NULL ;
dwError = mciSendCommand (0, MCI_OPEN,MCI_WAIT | MCI_OPEN_ELEMENT,(DWORD_PTR) &mciOpen) ;
if (dwError != 0)
{
return TRUE ;
}
return 0;
}
https://www.oschina.net/code/snippet_1768500_39013
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* M_PI is declared in math.h */
#define PI M_PI
typedef unsigned int UI;
typedef unsigned long int UL;
typedef unsigned short int US;
typedef unsigned char UC;
typedef signed int SI;
typedef signed long int SL;
typedef signed short int SS;
typedef signed char SC;
#define attr(a) __attribute__((a))
#define packed attr(packed)
/* WAV header, 44-byte total */
typedef struct{
UL riff packed;
UL len packed;
UL wave packed;
UL fmt packed;
UL flen packed;
US one packed;
US chan packed;
UL hz packed;
UL bpsec packed;
US bpsmp packed;
US bitpsmp packed;
UL dat packed;
UL dlen packed;
}WAVHDR;
int savefile(const char*const s,const void*const m,const int ml){
FILE*f=fopen(s,"wb");
int ok=0;
if(f){
ok=fwrite(m,1,ml,f)==ml;
fclose(f);
}
return ok;
}
/* "converts" 4-char string to long int */
#define dw(a) (*(UL*)(a))
/* Makes 44-byte header for 8-bit WAV in memory
usage: wavhdr(pointer,sampleRate,dataLength) */
void wavhdr(void*m,UL hz,UL dlen){
WAVHDR*p=m;
p->riff=dw("RIFF");
p->len=dlen+44;
p->wave=dw("WAVE");
p->fmt=dw("fmt ");
p->flen=0x10;
p->one=1;
p->chan=1;
p->hz=hz;
p->bpsec=hz;
p->bpsmp=1;
p->bitpsmp=8;
p->dat=dw("data");
p->dlen=dlen;
}
/* returns 8-bit sample for a sine wave */
UC sinewave(UL rate,float freq,UC amp,UL z){
return sin(z*((PI*2/rate)*freq))*amp+128;
}
/* make arbitrary audio data here */
void makeaud(UC*p,const UL rate,UL z){
float freq=500;
UC amp=120;
while(z--){
*p++=sinewave(rate,freq,amp,z);
}
}
/* makes wav file */
void makewav(const UL rate,const UL dlen){
const UL mlen=dlen+44;
UC*const m=malloc(mlen);
if(m){
wavhdr(m,rate,dlen);
makeaud(m+44,rate,dlen);
savefile("out.wav",m,mlen);
}
}
int main(){
if(sizeof(WAVHDR)!=44)puts("bad struct");
makewav(22050,64000);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* M_PI is declared in math.h */
#define PI 3.1415
typedef unsigned int UI;
typedef unsigned long int UL;
typedef unsigned short int US;
typedef unsigned char UC;
typedef signed int SI;
typedef signed long int SL;
typedef signed short int SS;
typedef signed char SC;
//#define attr(a) __attribute__((a))
//
//#define packed attr(packed)
/* WAV header, 44-byte total */
typedef struct {
UL riff ;
UL len ;
UL wave ;
UL fmt ;
UL flen ;
US one ;
US chan ;
UL hz ;
UL bpsec ;
US bpsmp ;
US bitpsmp ;
UL dat ;
UL dlen ;
}WAVHDR;
int savefile(const char*const s, const void*const m, const int ml) {
FILE*f;
fopen_s(&f, s, "wb");
int ok = 0;
if (f) {
ok = fwrite(m, 1, ml, f) == ml;
fclose(f);
}
return ok;
}
/* "converts" 4-char string to long int */
#define dw(a) (*(UL*)(a))
/* Makes 44-byte header for 8-bit WAV in memory
usage: wavhdr(pointer,sampleRate,dataLength) */
void wavhdr(void*m, UL hz, UL dlen) {
WAVHDR*p = static_cast<WAVHDR*>(m);
p->riff = dw("RIFF");
p->len = dlen + 44;
p->wave = dw("WAVE");
p->fmt = dw("fmt ");
p->flen = 0x10;
p->one = 1;
p->chan = 1;
p->hz = hz;
p->bpsec = hz;
p->bpsmp = 1;
p->bitpsmp = 8;
p->dat = dw("data");
p->dlen = dlen;
}
/* returns 8-bit sample for a sine wave */
UC sinewave(UL rate, float freq, UC amp, UL z) {
return sin(z*((PI * 2 / rate)*freq))*amp + 128;
}
/* make arbitrary audio data here */
void makeaud(UC*p, const UL rate, UL z) {
float freq = 500;
UC amp = 120;
while (z--) {
*p++ = sinewave(rate, freq, amp, z);
}
}
/* makes wav file */
void makewav(const UL rate, const UL dlen) {
const UL mlen = dlen + 44;
UC*const m = (UC*)malloc(mlen);
if (m) {
wavhdr(m, rate, dlen);
makeaud(m + 44, rate, dlen);
savefile("out.wav", m, mlen);
}
}
int main() {
if (sizeof(WAVHDR) != 44)puts("bad struct");
makewav(22050, 64000);
return 0;
}