void CAudioFormat::OnOK() { // TODO: Add extra validation here HRESULT hr; DWORD dwInputSelect; DWORD dwInputWavIndex; HWND hInputList; hInputList = GetDlgItem(IDC_FORMATS_INPUT_LISTBOX )->GetSafeHwnd(); dwInputSelect = (DWORD)::SendMessage( hInputList, LB_GETCURSEL, 0, 0 ); dwInputWavIndex = (DWORD)::SendMessage( hInputList, LB_GETITEMDATA, dwInputSelect, 0 ); ZeroMemory( &(theApp.m_wfxInput), sizeof(WAVEFORMATEX)); theApp.m_wfxInput.wFormatTag = WAVE_FORMAT_PCM; GetWaveFormatFromIndex( dwInputWavIndex, &(theApp.m_wfxInput)); if( FAILED( hr = CreateCaptureBuffer( &theApp.m_wfxInput ) ) ) { DXTRACE_ERR_MSGBOX( TEXT("CreateCaptureBuffer"), hr ); } CDialog::OnOK(); }
HRESULT CCaptureSound::StartRecord() { HRESULT hr; // Create a capture buffer, and tell the capture // buffer to start recording if( FAILED( hr = CreateCaptureBuffer() ) ) return hr; if( FAILED( hr = m_pDSBCapture->Start( DSCBSTART_LOOPING ) ) ) return hr; return S_OK; }
int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr = CoInitialize(0); if (SUCCEEDED(hr)) { hr = DirectSoundCaptureEnumerate((LPDSENUMCALLBACK)&callback_enumerate_capture_devices, NULL); } if (g_capture_guids_count > 0) { IDirectSoundCapture8 * p_capture_itf = NULL; g_selected_capture_device_idx = 0; hr = DirectSoundCaptureCreate8(&g_capture_guids[g_selected_capture_device_idx], &p_capture_itf, NULL); if (SUCCEEDED(hr)) { DSCCAPS dsCaps; ZeroMemory(&dsCaps, sizeof(dsCaps)); dsCaps.dwSize = sizeof(dsCaps); hr = p_capture_itf->GetCaps(&dsCaps); if (SUCCEEDED(hr)) { printf("dwFlags: "); if (dsCaps.dwFlags & DSCCAPS_CERTIFIED ) printf("DSCAPS_CERTIFIED | "); if (dsCaps.dwFlags & DSCCAPS_EMULDRIVER) printf("DSCCAPS_EMULDRIVER | "); if (dsCaps.dwFlags & DSCCAPS_MULTIPLECAPTURE) printf("DSCCAPS_MULTIPLECAPTURE | "); printf("\n"); size_t idx; printf("dwFormat: 0x%8.8x\n", dsCaps.dwFormats); for (idx = 0; idx < sizeof(g_wave_format_table)/sizeof(g_wave_format_table[0]); ++idx) { if (dsCaps.dwFormats & g_wave_format_table[idx].dwFormat_) printf("\t%s %s\n", g_wave_format_table[idx].tag_, g_wave_format_table[idx].text_desc_); } IDirectSoundCaptureBuffer8 * p_capture_buffer = NULL; hr = CreateCaptureBuffer(p_capture_itf, &p_capture_buffer); if (SUCCEEDED(hr)) { /* Now we have a valid Capture buffer interface. We can now capture WAV data */ DSCBCAPS dscbCaps; DWORD dwStatus = 0; WAVEFORMATEX wavFormatEx; ZeroMemory(&wavFormatEx, sizeof(wavFormatEx)); dscbCaps.dwSize = sizeof(dscbCaps); p_capture_buffer->GetCaps(&dscbCaps); p_capture_buffer->GetFormat(&wavFormatEx, sizeof(wavFormatEx), NULL); p_capture_buffer->GetStatus(&dwStatus); p_capture_buffer->Start(0); do { static unsigned int count = 0; #if 1 DWORD dwCapPos, dwReadPos; if (0 == (count & 0x0ff)) { p_capture_buffer->GetCurrentPosition(&dwCapPos, &dwReadPos); fprintf(stdout, "%8.8x %8.8x \n", dwCapPos, dwReadPos); } #endif p_capture_buffer->GetStatus(&dwStatus); if (0 == (DSCBSTATUS_CAPTURING & dwStatus)) break; Sleep(1); ++count; }while (1); LPVOID pAudioPtr1, pAudioPtr2; DWORD dwAudioBytes1, dwAudioBytes2; hr = p_capture_buffer->Lock(0, 0, &pAudioPtr1, &dwAudioBytes1, &pAudioPtr2, &dwAudioBytes2, DSCBLOCK_ENTIREBUFFER); CopyMemory(&g_wav_data_buffer[0], pAudioPtr1, dwAudioBytes1); { HANDLE hOutFile; hOutFile = CreateFile("caputre.dat", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE != hOutFile) { DWORD dwBytesWritten; WriteFile(hOutFile, &g_wav_data_buffer, sizeof(g_wav_data_buffer), &dwBytesWritten, NULL); CloseHandle(hOutFile); } } p_capture_buffer->Release(); } } p_capture_itf->Release(); } } return SUCCEEDED(hr); }
static int DSOUND_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { const DWORD numchunks = 8; HRESULT result; SDL_bool valid_format = SDL_FALSE; SDL_bool tried_format = SDL_FALSE; SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format); LPGUID guid = (LPGUID) handle; DWORD bufsize; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc((sizeof *this->hidden)); if (this->hidden == NULL) { return SDL_OutOfMemory(); } SDL_zerop(this->hidden); /* Open the audio device */ if (iscapture) { result = pDirectSoundCaptureCreate8(guid, &this->hidden->capture, NULL); if (result != DS_OK) { return SetDSerror("DirectSoundCaptureCreate8", result); } } else { result = pDirectSoundCreate8(guid, &this->hidden->sound, NULL); if (result != DS_OK) { return SetDSerror("DirectSoundCreate8", result); } result = IDirectSound_SetCooperativeLevel(this->hidden->sound, GetDesktopWindow(), DSSCL_NORMAL); if (result != DS_OK) { return SetDSerror("DirectSound SetCooperativeLevel", result); } } while ((!valid_format) && (test_format)) { switch (test_format) { case AUDIO_U8: case AUDIO_S16: case AUDIO_S32: case AUDIO_F32: tried_format = SDL_TRUE; this->spec.format = test_format; /* Update the fragment size as size in bytes */ SDL_CalculateAudioSpec(&this->spec); bufsize = numchunks * this->spec.size; if ((bufsize < DSBSIZE_MIN) || (bufsize > DSBSIZE_MAX)) { SDL_SetError("Sound buffer size must be between %d and %d", (DSBSIZE_MIN < numchunks) ? 1 : DSBSIZE_MIN / numchunks, DSBSIZE_MAX / numchunks); } else { int rc; WAVEFORMATEX wfmt; SDL_zero(wfmt); if (SDL_AUDIO_ISFLOAT(this->spec.format)) { wfmt.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; } else { wfmt.wFormatTag = WAVE_FORMAT_PCM; } wfmt.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format); wfmt.nChannels = this->spec.channels; wfmt.nSamplesPerSec = this->spec.freq; wfmt.nBlockAlign = wfmt.nChannels * (wfmt.wBitsPerSample / 8); wfmt.nAvgBytesPerSec = wfmt.nSamplesPerSec * wfmt.nBlockAlign; rc = iscapture ? CreateCaptureBuffer(this, bufsize, &wfmt) : CreateSecondary(this, bufsize, &wfmt); if (rc == 0) { this->hidden->num_buffers = numchunks; valid_format = SDL_TRUE; } } break; } test_format = SDL_NextAudioFormat(); } if (!valid_format) { if (tried_format) { return -1; /* CreateSecondary() should have called SDL_SetError(). */ } return SDL_SetError("DirectSound: Unsupported audio format"); } /* Playback buffers will auto-start playing in DSOUND_WaitDevice() */ return 0; /* good to go. */ }