Saturday, February 27, 2010

How to save audio to mp3 on Silverlight 4

As you know Silverlight 4 supports access to the microphone. Plus, besides, it can call COM objects on the client side.

To save the audio in mp3 format we need.

I. Make the COM object wrapper, which allows us to invoke the necessary Alvas.Audio functionality.

0. Create AlvasCom project as ClassLibrary.

1. In AssemblyInfo.cs change the line

[assembly: ComVisible(false)]

to

[assembly: ComVisible(true)]


2. Add a reference to Alvas.Audio.dll

3. Add Alvas.Audio-wrapper interface and its implementation. See the code below.
namespace AlvasCom
{
 
    [Guid("50B29476-F2C0-4445-B0E1-7D4023453EBD")]
    interface IAudioHost
    {
        void Close();
        void WriteData(short channels, short bitsPerSample, int samplesPerSec, byte[] sampleData);
    }
 
    [ClassInterface(ClassInterfaceType.None)]
    [Guid("1EA86BAE-420F-4309-BF06-23730718BF5E")]
    public class AudioHost : IAudioHost
    {
        Mp3Writer mw;
 
        public AudioHost()
        {
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
            string fileName = Path.ChangeExtension(Path.GetFileName(Path.GetTempFileName()), ".mp3");
            string path = Path.Combine(dir, fileName);
            global::System.Windows.Forms.MessageBox.Show(path);
            mw = new Mp3Writer(File.Create(path));
        }
 
        public void WriteData(short channels, short bitsPerSample, int samplesPerSec, byte[] sampleData)
        {
            IntPtr sampleFormat = AudioCompressionManager.GetPcmFormat(channels, bitsPerSample, samplesPerSec);
            IntPtr format = AudioCompressionManager.GetCompatibleFormat(sampleFormat, AudioCompressionManager.MpegLayer3FormatTag);
            byte[] data = AudioCompressionManager.Convert(sampleFormat, format, sampleData, false);
            mw.WriteData(data);
        }
 
        public void Close()
        {
            mw.Close();
        }
 
    }
}
4. Sign the assembly with the snk file.

5. Registering in GAC AlvasCom.dll and Alvas.Audio.dll using gacutil.

6. Register the COM object using regasm.

You can find AlvasCom.bat in the "SilverlightAudio\AlvasCom\bin\Debug\" folder to register these libraries.

II. Make the Silverlight application with microphone support.

0. Create SilverlightAudio project as SilverlightApplication.

1. To call a COM object, we need elevated privileges and run it out of browser. Add to OutOfBrowserSettings.xml

  <OutOfBrowserSettings.SecuritySettings>
    <SecuritySettings 
ElevatedPermissions="Required" />
  </OutOfBrowserSettings.SecuritySettings>


2. Writing code call using the dynamic
namespace SilverlightAudio
{
    public class MyAudioSync : AudioSink
    {
        dynamic ah;
        AudioFormat audioFormat;
 
        protected override void OnCaptureStarted()
        {
            ah = AutomationFactory.CreateObject("AlvasCom.AudioHost");
        }
 
        protected override void OnCaptureStopped()
        {
            ah.Close();
        }
 
        protected override void OnFormatChange(AudioFormat audioFormat)
        {
            this.audioFormat = audioFormat;
        }
 
        protected override void OnSamples(long sampleTime, long sampleDuration, byte[] sampleData)
        {
            ah.WriteData((short)audioFormat.Channels, (short)audioFormat.BitsPerSample, audioFormat.SamplesPerSecond, sampleData);
        }
    }
}
3. Compile and run the application.

4. Enjoy.

The source code and precompiled examples are here (SilverlightAudio.zip).

How can we extend this example? Come immediately to mind

1. Save mp3 file on the server.

2. Make voice chat, but it is better to use gsm instead of mp3 encoding. How to do it - leave it in the form of homework. Hint #tip60 :)

3. And what can you propose?

P.S. If you have any problems - please write me.

Progg it

No comments:

Post a Comment