?? windows media audio compressor - the code project - managed c++.mht
字號:
=
href=3D"http://www.codeproject.com/managedcpp/WmaCompressor.asp#Figure =
2">figure=20
2</A>, with no more added complexity. Unfortunately, there =
is no=20
such managed WMF SDK, therefore, some tricks and wrapper =
classes are=20
necessary to implement the compressor. If you are interested =
in=20
knowing the details, you can look at <A=20
=
href=3D"http://www.codeproject.com/managedcpp/WmaCompressor.asp#Figure =
3">figure=20
3</A> in the <A=20
=
href=3D"http://www.codeproject.com/managedcpp/WmaCompressor.asp#Implement=
ation details">implementation=20
details</A> section of this article.</P>
<H2>Using the code</H2>
<P>Using the <CODE>WmaWriter</CODE> is easy. You can use it =
in the=20
same way you use <CODE>BinaryWriter</CODE> =
(<CODE>WmaWriter</CODE>=20
is a specialization of <CODE>BinaryWriter</CODE>: see <A=20
=
href=3D"http://www.codeproject.com/managedcpp/WmaCompressor.asp#Figure =
2">figure=20
2</A>). The only difference with <CODE>BinaryWriter</CODE> =
is that=20
you must indicate at creation time the format of the input =
data=20
(data that the writer will receive) using a =
<CODE>WaveFormat</CODE>=20
instance, and a profile defining the settings of the =
resulting=20
compressed stream.</P>
<P>The following VB.NET code shows a simple way of =
compressing a WAV=20
file using this compressor:</P><PRE lang=3Dvbnet><SPAN =
class=3Dvb-keyword>Imports</SPAN> Yeti.MMedia
<SPAN class=3Dvb-keyword>Imports</SPAN> Yeti.MMedia.Wmf
<SPAN class=3Dvb-keyword>Imports</SPAN> WaveLib
<SPAN class=3Dvb-keyword>Imports</SPAN> System.IO
....
<SPAN class=3Dvb-keyword>Dim</SPAN> InStr <SPAN =
class=3Dvb-keyword>As</SPAN> WaveStream
<SPAN class=3Dvb-keyword>Dim</SPAN> writer <SPAN =
class=3Dvb-keyword>As</SPAN> AudioWriter
<SPAN class=3Dvb-keyword>Dim</SPAN> buff() <SPAN =
class=3Dvb-keyword>As</SPAN> <SPAN class=3Dvb-keyword>Byte</SPAN>
<SPAN class=3Dvb-keyword>Dim</SPAN> read <SPAN =
class=3Dvb-keyword>As</SPAN> <SPAN class=3Dvb-keyword>Integer</SPAN>
InStr =3D <SPAN class=3Dvb-keyword>New</SPAN> WaveStream(<SPAN =
class=3Dvb-string>"SomeFile.wav"</SPAN>)
<SPAN class=3Dvb-keyword>Try</SPAN>
writer =3D <SPAN class=3Dvb-keyword>New</SPAN> WmaWriter(<SPAN =
class=3Dvb-keyword>New</SPAN> FileStream(<SPAN =
class=3Dvb-string>"SomeFile.wma"</SPAN>, FileMode.Create), _
InStr.Format)
<SPAN class=3Dvb-comment>'The previous line is equivalent to:</SPAN>
<SPAN class=3Dvb-comment>'writer =3D New WmaWriter(New =
FileStream("SomeFile.wma", FileMode.Create), _</SPAN>
<SPAN class=3Dvb-comment>' InStr.Format, =
_</SPAN>
<SPAN class=3Dvb-comment>' =
WmaWriterProfileManager.AudioSystemProfiles(0))</SPAN>
<SPAN class=3Dvb-keyword>Try</SPAN>
buff =3D <SPAN class=3Dvb-keyword>New</SPAN> <SPAN =
class=3Dvb-keyword>Byte</SPAN>(writer.OptimalBufferSize - <SPAN =
class=3Dvb-literal>1</SPAN>) {}
read =3D InStr.Read(buff, <SPAN class=3Dvb-literal>0</SPAN>, =
buff.Length)
<SPAN class=3Dvb-keyword>While</SPAN> (read > <SPAN =
class=3Dvb-literal>0</SPAN>)
writer.Write(buff, <SPAN class=3Dvb-literal>0</SPAN>, read)
read =3D InStr.Read(buff, <SPAN class=3Dvb-literal>0</SPAN>, =
buff.Length)
<SPAN class=3Dvb-keyword>End</SPAN> <SPAN =
class=3Dvb-keyword>While</SPAN>
<SPAN class=3Dvb-keyword>Finally</SPAN>
writer.Close()
<SPAN class=3Dvb-keyword>End</SPAN> <SPAN =
class=3Dvb-keyword>Try</SPAN>
<SPAN class=3Dvb-keyword>Finally</SPAN>
InStr.Close()
<SPAN class=3Dvb-keyword>End</SPAN> <SPAN =
class=3Dvb-keyword>Try</SPAN></PRE>
<P>I made the sample project in VB.NET just to please those =
who love=20
VB. Anyway, you can use the compressor in C#, Managed C++, =
J# or any=20
other CLS compliant language. The sample project of the MP3=20
compressor (also included in the source files) is written in =
C#. You=20
can also refer to my <A=20
=
href=3D"http://www.codeproject.com/csharp/CSharpRipper.asp">C Sharp=20
MP3 Compressor</A> article.</P>
<P>Another example of using the writer could be an improved =
version=20
of the ripper described in my article "<A=20
=
href=3D"http://www.thecodeproject.com/csharp/CSharpRipper.asp">C Sharp=20
Ripper</A>" to rip directly to WMA format. In the file=20
<I>Form1.cs</I>, inside the handler of the "Save as.." =
button, there=20
is the line:</P><PRE lang=3Dcs>m_Writer =3D <SPAN =
class=3Dcs-keyword>new</SPAN> WaveWriter(WaveFile, Format);</PRE>
<P>Which may be changed to:</P><PRE lang=3Dcs>m_Writer =3D =
<SPAN class=3Dcs-keyword>new</SPAN> WmaWriter(WaveFile, Format);</PRE>
<P>The rest of the code remains without change and, of =
course, if=20
you need more control on the compressor parameters, then you =
should=20
add extra code to supply a <CODE>WmaWriterProfile</CODE> =
instance in=20
the constructor.</P>
<H2><A name=3D"Implementation details">Implementation =
details</A></H2>
<P>The figure below is a more complete class diagram of the=20
compressor, showing the managed classes and interfaces and =
their=20
relations with the WMF SDK interfaces.</P>
<P><A=20
=
href=3D"http://www.codeproject.com/managedcpp/WmaCompressor/ClassDiagram_=
big.jpg"=20
target=3D_blank><IMG height=3D470=20
=
src=3D"http://www.codeproject.com/managedcpp/WmaCompressor/ClassDiagram.j=
pg"=20
width=3D600></A></P>
<P><A name=3D"Figure 3">Figure 3</A>: Windows Media Audio =
Compressor:=20
implementation details.</P>
<P>I'm going to avoid the explanation of the implementation =
details.=20
I just included the class diagram for those who want to see =
how this=20
project was implemented and help them to better understand =
the code=20
included with this article.</P>
<P>If you only want to use the compressor as is, then you =
can safely=20
ignore these implementation details.</P>
<H2>Conclusion</H2>
<P>As showed here, the use of this compressor is relatively =
easy=20
even if the implementation is somewhat complex due to =
interaction of=20
managed and unmanaged code. There are some features of =
Windows Media=20
Format that are not included in this project, such as DRM =
support,=20
metadata attributes and two-pass encoding.</P>
<P>The sample code is given for demonstration purposes only, =
you can=20
use it as reference if you need to develop some product with =
these=20
technologies. When a managed version of WMF SDK is =
available, it=20
will be easier to implement managed solutions using WMF. At =
the=20
moment, this approach could be a good solution, considering =
that the=20
unmanaged part can be suppressed without changing the design =
of=20
managed part.</P><!-- Article Ends --></DIV></SPAN>
<SCRIPT =
src=3D"http://www.codeproject.com/script/togglePre.js"=20
type=3Dtext/javascript></SCRIPT>
<H2>Idael Cardoso</H2>
<DIV style=3D"OVERFLOW: hidden">
<TABLE border=3D0>
<TBODY>
<TR vAlign=3Dtop>
<TD class=3DsmallText noWrap><BR></TD>
<TD class=3DsmallText>
<P class=3DsmallText>Click <A=20
=
href=3D"http://www.codeproject.com/script/profile/whos_who.asp?vt=3Darts&=
amp;id=3D325803">here</A>=20
to view Idael Cardoso's online=20
profile.</P></TD></TR></TBODY></TABLE></DIV><BR>
<TABLE cellPadding=3D4 width=3D"100%" border=3D0>
<TBODY>
<TR vAlign=3Dtop>
<TD width=3D"100%">
<H2>Other popular Managed C++ articles:</H2>
<UL>
<LI><A=20
=
href=3D"http://www.codeproject.com/managedcpp/quake2.asp">Quake=20
II .NET</A>
<DIV class=3DsmallText>A port of the famous =
C-language based=20
game engine to Visual C++ with a .NET managed =
heads-up=20
display.</DIV>
<LI><A=20
=
href=3D"http://www.codeproject.com/managedcpp/loadbalancing.asp">.NET=20
Dynamic Software Load Balancing</A>
<DIV class=3DsmallText>A Draft Implementation of an =
Idea for=20
.NET Dynamic Software Load Balancing</DIV>
<LI><A=20
=
href=3D"http://www.codeproject.com/managedcpp/cppcliintro01.asp">A=20
first look at C++/CLI</A>
<DIV class=3DsmallText>A brief look at the new =
C++/CLI syntax=20
and how it improves over the old MC++ syntax</DIV>
<LI><A=20
=
href=3D"http://www.codeproject.com/managedcpp/get_the_ip_out_tcpclient.as=
p">How=20
to get the clients IP address using TcpClient in =
Managed=20
C++</A>
<DIV class=3DsmallText>How to get the clients IP =
address using=20
TcpClient in Managed C++</DIV></LI></UL></TD>
<TD width=3D360>
<SCRIPT language=3Djavascript>document.write("<a =
href=3D\"/script/admentor/admentorredir.asp?id=3D3215&way=3Dban\" =
target=3D_blank><img =
src=3D\"/script/ann/ServeImg.aspx?File=3D%2Fscript%2Fadmentor%2Fimages%2F=
vista%5Fmobile%5Fcp%5Fcompetition%5F300x250%2Egif&C=3DFalse&id=3D3215&cb=3D=
1307604\" alt=3D\"\" border=3D0 width=3D300 =
height=3D250></a>");</SCRIPT>
</TD></TR></TBODY></TABLE>
<FORM action=3D/script/rating/code/app/insert_vote.asp=20
method=3Dpost><INPUT type=3Dhidden=20
value=3DWmaCompressor/managedcpp4/9/2004 name=3Dvote_name> =
<INPUT=20
type=3Dhidden value=3D/managedcpp/WmaCompressor.asp =
name=3Dgoal>=20
<TABLE cellSpacing=3D0 cellPadding=3D1 width=3D"100%" =
bgColor=3D#ff9900=20
border=3D0>
<TBODY>
<TR>
<TD width=3D"100%">
<TABLE cellSpacing=3D0 cellPadding=3D4 width=3D"100%"=20
bgColor=3D#fbedbb border=3D0>
<TBODY>
<TR>
<TD class=3Dsmalltext vAlign=3Dcenter>[<A=20
=
href=3D"http://www.codeproject.com/managedcpp/WmaCompressor.asp#__top">To=
p</A>]</TD>
<TD vAlign=3Dcenter noWrap =
align=3Dright><I><B>Rate this=20
Article for us!</B></I> =20
<I>Poor</I><INPUT type=3Dradio value=3D1 =
name=3Drate><INPUT=20
type=3Dradio value=3D2 name=3Drate><INPUT =
type=3Dradio value=3D3=20
name=3Drate><INPUT type=3Dradio value=3D4 =
name=3Drate><INPUT=20
type=3Dradio value=3D5=20
name=3Drate><I>Excellent</I> <INPUT class=3DFormButton =
type=3Dsubmit value=3DVote>=20
=
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM>
<CENTER>
<TABLE cellSpacing=3D0 cellPadding=3D0 border=3D0>
<TBODY>
<TR>
<TD=20
style=3D"PADDING-RIGHT: 2px; PADDING-LEFT: 2px; =
PADDING-BOTTOM: 2px; PADDING-TOP: 2px; BACKGROUND-COLOR: #eee"=20
width=3D120 height=3D60>
<DIV align=3Dcenter>
<SCRIPT language=3Djavascript>document.write("<IFRAME =
src=3D\"/script/ann/ServeHTML.aspx?C=3DFalse&id=3D2469&cb=3D1307607\" =
border=3D0 frameborder=3D0 scrolling=3Dno width=3D120 =
height=3D60></IFRAME>");</SCRIPT>
</DIV></TD>
<TD width=3D100> </TD>
<TD width=3D468 height=3D60><SPAN id=3DAdBanner5>
<SCRIPT language=3Djavascript>document.write("<IFRAME =
src=3D\"/script/ann/ServeHTML.aspx?C=3DFalse&id=3D3025&cb=3D1307590\" =
border=3D0 frameborder=3D0 scrolling=3Dno width=3D468 =
height=3D60></IFRAME>");</SCRIPT>
</SPAN></TD></TR></TBODY></TABLE></CENTER><A =
name=3D__comments></A>
<SCRIPT language=3DJavaScript>
function MsgVoteForm(MemberID, MsgID)
{
document.write("<span id=3D\"MVF" + MsgID + "\">");
document.write("Rate this message: ");
for (var i=3D1; i<=3D5;i++)
{
document.write("<a href=3D'#xx" + MsgID.toString() + "xx' =
title=3D'give this message a vote of " + i.toString());
document.write("' onclick=3D'return RateMsg(" + MemberID.toString() + =
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -