Click here to Skip to main content
15,886,693 members
Articles / Programming Languages / Visual Basic

Windows Media Audio compressor

Rate me:
Please Sign up or sign in to vote.
4.76/5 (25 votes)
8 Apr 20045 min read 302.6K   5K   106  
Managed C++ Windows Media Audio (WMA) compressor.
//
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER 
//  REMAINS UNCHANGED.
//
//  Email:  yetiicb@hotmail.com
//
//  Copyright (C) 2002-2004 Idael Cardoso. 
//
#include "StdAfx.h"
#include ".\wmawriter.h"
#include <wmsdk.h>

#using <mscorlib.dll>

CWmaWriter::CWmaWriter(WAVEFORMATEX __nogc* pInFormat, IWMProfile __nogc* pProfile, IWMWriterSink __nogc* pSink)
{
  if ( (pInFormat ==NULL) || (pProfile == NULL) || (pSink ==NULL) )
  {
    AtlThrow(E_INVALIDARG);
  }
  m_MsAudioTime = 0;
  m_cAudioBytes = 0;
  m_AvgBytesPerSec = pInFormat->nAvgBytesPerSec;
  ATLTHROW_IF_FAILED(WMCreateWriter(NULL, &m_Writer))
  CComPtr<IWMWriterAdvanced> pWriterAdvanced;
  ATLTHROW_IF_FAILED(m_Writer.QueryInterface(&pWriterAdvanced));
  ATLTHROW_IF_FAILED(pWriterAdvanced->AddSink(pSink));
  m_Sink = pSink;
  ATLTHROW_IF_FAILED(m_Writer->SetProfile(pProfile))
  
  DWORD   cInputs = 0;
  ATLTHROW_IF_FAILED(m_Writer->GetInputCount(&cInputs));
  
  CComPtr<IWMInputMediaProps>  pProps;
  GUID InputType;
  WM_MEDIA_TYPE   mt;
  
  mt.majortype			= WMMEDIATYPE_Audio;
  mt.subtype				= WMMEDIASUBTYPE_PCM;
  mt.bFixedSizeSamples	= TRUE;
  mt.bTemporalCompression = FALSE;
  mt.lSampleSize			= pInFormat->nBlockAlign;
  mt.formattype			= WMFORMAT_WaveFormatEx;
  mt.pUnk					= NULL;
  mt.cbFormat				= sizeof( WAVEFORMATEX ) + pInFormat->cbSize;
  mt.pbFormat				= (BYTE*)pInFormat;
  for(DWORD input = 0; input < cInputs; input++)
  {
    pProps = NULL;
    m_AudioInput = input;
    ATLTHROW_IF_FAILED(m_Writer->GetInputProps(input, &pProps));
    ATLTHROW_IF_FAILED(pProps->GetType(&InputType));
    if (InputType != WMMEDIATYPE_Audio)
    {
      AtlThrow(E_INVALIDARG); //Only Audio profile 
    }
    ATLTHROW_IF_FAILED(pProps->SetMediaType(&mt));
    ATLTHROW_IF_FAILED(m_Writer->SetInputProps(input, pProps));
  }
  ATLTHROW_IF_FAILED(m_Writer->BeginWriting());
}

CWmaWriter::~CWmaWriter(void)
{
  if (m_Writer.p != NULL)
  {
    CComPtr<IWMWriterAdvanced> pWriterAdvanced;
    m_Writer->EndWriting();
    if( SUCCEEDED(m_Writer.QueryInterface(&pWriterAdvanced)) )
    {
      pWriterAdvanced->RemoveSink(m_Sink);
    }
  }
}

void CWmaWriter::Write(INSSBuffer* pSample)
{
  DWORD Length;
  pSample->GetLength(&Length);
  ATLTHROW_IF_FAILED(m_Writer->WriteSample(m_AudioInput, m_MsAudioTime * 10000, 0, pSample));
  m_cAudioBytes += Length;
  m_MsAudioTime += MulDiv(Length, 1000, m_AvgBytesPerSec);
}

void CWmaWriter::Write(BYTE* pBuffer, DWORD Length)
{
  if (m_Writer.p != NULL)
  {
    CComPtr<INSSBuffer> pSample;
    BYTE* pSampleBuff;
    ATLTHROW_IF_FAILED(m_Writer->AllocateSample(Length, &pSample));
    ATLTHROW_IF_FAILED(pSample->GetBuffer(&pSampleBuff));
    memcpy(pSampleBuff, pBuffer, Length);
    ATLTHROW_IF_FAILED(pSample->SetLength(Length));
    Write(pSample);
  }
  else
  {
    AtlThrow(E_POINTER);
  }
}


INSSBuffer* CWmaWriter::AllocateSample(DWORD Length)
{
  if (m_Writer.p != NULL)
  {
    INSSBuffer* pSample = NULL;
    m_Writer->AllocateSample(Length, &pSample);
    return pSample;
  }
  else
  {
    return NULL;
  }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions