Click here to Skip to main content
15,897,704 members
Articles / Desktop Programming / Win32

Play Audio Files with DirectSound and Display its Spectrum in Real Time - Part 3

Rate me:
Please Sign up or sign in to vote.
4.93/5 (21 votes)
24 Dec 2008CPOL3 min read 274.6K   17.7K   86  
An article to show how to play audio file with DirectSound and display its spectrum in real time accurately
#pragma once

#include "stdafx.h"
#include "CriticalSection.h"
#include "Stream.h"

#ifndef INCLUDE_AUDIOSTREAM
#define INCLUDE_AUDIOSTREAM

#pragma warning(disable : 4006)
#pragma warning(disable : 4995)

class CAudioStream;

typedef struct _WMA_SYNC_READER
{
	CAudioStream* pAudioStream;
	IWMSyncReader* pWMSyncReader;
	INSSBuffer* pNSSBuffer;
	IWMHeaderInfo* pWMHeaderInfo;
	DWORD dwOffset;
	IStream* pStream;
	WORD wStream;
	DWORD dwOutput;
	BOOL bHasAudio;
	BOOL bProtected;
	WORD dwChannels;
	DWORD dwSampleRate;
	WORD dwBitsPerSample;
	LARGE_INTEGER liDuration;
} WMA_SYNC_READER, *LPWMA_SYNC_READER;

typedef struct _WMAFormatSpec
{
	DWORD BitsPerSample;
	DWORD Channels;
	DWORD SampleRate;
} WMAFormatSpec, LPWMAFormatSpec;

class CAudioStream : public IStream
{
private:
	LONG m_cRef;
	CStream* m_Stream;
public:
	CAudioStream();
	~CAudioStream(void);

	void SetStream(CStream* pStream) { m_Stream = pStream; }

	HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
	ULONG STDMETHODCALLTYPE AddRef(void);
	ULONG STDMETHODCALLTYPE Release(void);

	HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead);
	HRESULT STDMETHODCALLTYPE Write(const void *pv, ULONG cb, ULONG *pcbWritten);

	HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition);
	HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize);
	HRESULT STDMETHODCALLTYPE CopyTo(IStream *pstm, ULARGE_INTEGER cb, ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten);
	HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags);
	HRESULT STDMETHODCALLTYPE Revert(void);
	HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
	HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
	HRESULT STDMETHODCALLTYPE Stat(STATSTG *pstatstg, DWORD grfStatFlag);
	HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm);
};

void WMA_Reader_Init(WMA_SYNC_READER* reader, CStream* pStream, BOOL bDescrete, SHORT iSpeakers);
DWORD WMA_Reader_GetDuration(WMA_SYNC_READER* reader);
void WMA_Reader_GetTag(WMA_SYNC_READER* reader, WCHAR* pszTagName, WCHAR* pszTagValue, size_t vsize);
WORD WMA_Reader_GetTagLength(WMA_SYNC_READER* reader, WCHAR* pszTagName);
void WMA_Reader_GetAuthor(WMA_SYNC_READER* reader, WCHAR* pszAuthor);
void WMA_Reader_Uninit(WMA_SYNC_READER* reader);
DWORD WMA_Reader_GetBitrate(WMA_SYNC_READER* reader);
DWORD WMA_Reader_GetFormatCount(WMA_SYNC_READER* reader, BOOL Descrete);
void WMA_Reader_GetFormat(WMA_SYNC_READER* reader, BOOL Descrete, INT index, WMAFormatSpec* pFormatSpec);
BOOL WMA_Reader_GetIsVBR(WMA_SYNC_READER* reader);
void WMA_Reader_Free(WMA_SYNC_READER* reader);
void WMA_Reader_GetData(WMA_SYNC_READER* reader, void** buffer, DWORD* bytes);
void WMA_Reader_GetAudioPropties(WMA_SYNC_READER* reader, WORD* channels, WORD* bitspersamples, DWORD* sampledate);
void WMA_Reader_Seek(WMA_SYNC_READER* reader, DWORD offset);
void WMA_Reader_SetFormat(WMA_SYNC_READER* reader, BOOL Descrete, DWORD FormatIndex);

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer none
China China
To be, or not to be, this is question. That's are all depend on your decision. What do you think?

Comments and Discussions