WP7-wav 合并demo

来源:互联网 发布:淘宝新人礼包怎么领取 编辑:程序博客网 时间:2024/06/05 00:19

移植了C#上面的wav合并算法。原算法地址:http://www.diybl.com/course/4_webprogram/asp.net/asp_netxl/2008925/145543.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;


using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;


using System.IO;
using System.IO.IsolatedStorage;


using System.Text.RegularExpressions;


using System.Windows.Resources;


using System.Diagnostics;


using Microsoft.Xna.Framework.Media;


namespace WavDemo
{
    public struct WavInfo
    {
        public string groupid;
        public string rifftype;
        public long filesize;
        public string chunkid;
        public long chunksize;
        public short wformattag; //记录着此声音的格式代号,例如WAVE_FORMAT_PCM,WAVE_F0RAM_ADPCM等等。 
        public ushort wchannels; //记录声音的频道数。 
        public ulong dwsamplespersec;//记录每秒取样数。 
        public ulong dwavgbytespersec;//记录每秒的数据量。 
        public ushort wblockalign;//记录区块的对齐单位。 
        public ushort wbitspersample;//记录每个取样所需的位元数。 
        public string datachunkid;
        public long datasize;
        public long HeadSize; //文件头长度 
    }
    public class Wav
    {
        //wav 头文件结构 
        public Wav()
        {


        }
        public byte[] bInfo;
        public WavInfo GetWavInfo(byte[] wave)
        {
            WavInfo wavInfo = new WavInfo();
            if (wave.Length >= 100)
            {
                bInfo = new byte[100];
                wave.CopyTo(bInfo, 0);


                int Length = GetHeadLen(bInfo);


                //wavInfo.groupid = System.Text.Encoding.Default.GetString(bInfo, 0, 4);
                wavInfo.filesize = System.BitConverter.ToInt32(bInfo, 4);
                //wavInfo.rifftype = System.Text.Encoding.Default.GetString(bInfo, 8, 4);
                //wavInfo.chunkid = System.Text.Encoding.Default.GetString(bInfo, 12, 4);
                wavInfo.chunksize = System.BitConverter.ToInt32(bInfo, 16);
                wavInfo.wformattag = System.BitConverter.ToInt16(bInfo, 20);
                wavInfo.wchannels = System.BitConverter.ToUInt16(bInfo, 22);
                wavInfo.dwsamplespersec = System.BitConverter.ToUInt32(bInfo, 24);
                wavInfo.dwavgbytespersec = System.BitConverter.ToUInt32(bInfo, 28);
                wavInfo.wblockalign = System.BitConverter.ToUInt16(bInfo, 32);
                wavInfo.wbitspersample = System.BitConverter.ToUInt16(bInfo, 34);
                wavInfo.datachunkid = "data";
                wavInfo.datasize = GetWavLen(bInfo);
                wavInfo.HeadSize = GetHeadLen(bInfo);
            }
            return wavInfo;
        }


        public int GetWavLen(byte[] fs)
        {
            //"data"后面的4个字节就是声音数据长度了 
            byte[] bLen = new byte[4];
            int ilen = GetDataLen(fs);


            bLen[0] = fs[ilen + 0];
            bLen[1] = fs[ilen + 1];
            bLen[2] = fs[ilen + 2];
            bLen[3] = fs[ilen + 3];
            return System.BitConverter.ToInt32(bLen, 0);


        }


        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);


            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }


        /// <summary> 
        /// 将 byte[] 转成 Stream 
        /// </summary> 
        public Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }


        public int GetHeadLen(byte[] fs)
        {
            //头文件长度 
            return GetDataLen(fs) + 4;
        }


        /// <summary> 
        /// data标识出现的位置 从32位开始向后找 返回位置 
        /// </summary> 
        /// <param name="fs"></param> 
        /// <returns></returns> 
        private int GetDataLen(byte[] fs)
        {
            if (fs.Length >= 90)
            {
                for (int i = 32; i <= 100; i++)
                {
                    byte[] tmpfs = new byte[1];
                    tmpfs[0] = fs[i - 4];
                    int sgadasd = tmpfs[0];
                    if (fs[i - 4] == 0x64 && fs[i - 3] == 0x61 && fs[i - 2] == 0x74 && fs[i - 1] == 0x61)
                    {
                        return i;
                    }
                }
                return 0;
            }
            else
            {
                return 0;
            }
        }
    }
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }


        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);


            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }


        /// <summary> 
        /// 将 byte[] 转成 Stream 
        /// </summary> 
        public Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
        private void Btn_Play(object sender, EventArgs e)
        {
            PlayOption();
        }


        private void Btn_Merge(object sender, EventArgs e)
        {
            MergeOption();
        }


        public void MergeOption()
        {
            Stream fs_1 = TitleContainer.OpenStream("SourceWav/今天白天.wav");
            Stream fs_2 = TitleContainer.OpenStream("SourceWav/雷阵雨.wav");


            Wav w_1 = new Wav();
            Wav w_2 = new Wav();


            byte[] bInfo1 = new byte[100];
            fs_1.Read(bInfo1, 0, 100);
            WavInfo wavinfo_1 = w_1.GetWavInfo(bInfo1);


            byte[] bInfo2 = new byte[100];
            fs_2.Read(bInfo2, 0, 100);
            WavInfo wavinfo_2 = w_2.GetWavInfo(bInfo2);


            byte[] bInfo = w_1.bInfo;
            byte[] filesize = System.BitConverter.GetBytes((int)wavinfo_1.filesize + (int)wavinfo_2.datasize);
            //修改文件长度 
            for (int i = 4, j = 0; i < 8; i++, j++)
            {
                bInfo[i] = filesize[j];
            }


            //修改裸数据长度 
            byte[] Datasize = System.BitConverter.GetBytes((int)wavinfo_1.datasize + (int)wavinfo_2.datasize);
            int iDataStart = (int)wavinfo_1.HeadSize - 4;
            for (int i = iDataStart, j = 0; i < iDataStart + 4; i++, j++)
            {
                bInfo[i] = Datasize[j];
            }


            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            if (store.FileExists("123.wav"))
            {
                store.DeleteFile("123.wav");
            }
            IsolatedStorageFileStream storeStream = new IsolatedStorageFileStream("123.wav", FileMode.OpenOrCreate, store);


            byte[] dushu_1 = new byte[wavinfo_1.filesize + 8];
            byte[] dushu_2 = new byte[wavinfo_2.filesize + 8];




            storeStream.Write(bInfo, 0, 100);
            fs_1.Read(dushu_1, 0, (int)wavinfo_1.filesize + 8 - 100);
            storeStream.Write(dushu_1, 0, (int)wavinfo_1.filesize + 8 - 100);


            fs_2.Read(dushu_2, 0, (int)wavinfo_2.filesize + 8);
            storeStream.Write(dushu_2, (int)wavinfo_2.HeadSize, (int)wavinfo_2.datasize);
            storeStream.Flush();
            storeStream.Close();


            //重新赋值 




            fs_1.Close();
            fs_2.Close();
            storeStream.Close();
        }


        public void PlayOption()
        {
            try
            {
                IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
                if (!store.FileExists("123.wav"))
                {
                    Song song = Song.FromUri("music", new Uri("123.wav", UriKind.Relative));
                    MediaPlayer.Play(song);


                    //int k = 10;
                }
                IsolatedStorageFileStream readStream = new IsolatedStorageFileStream("123.wav", FileMode.Open, store);


                StreamReader sd = new StreamReader(readStream);


                byte[] fdasgd = StreamToBytes(sd.BaseStream);


                Stream daghag = BytesToStream(fdasgd);


                long w = readStream.Length;


                string a = sd.ReadToEnd();


                byte[] result = new byte[w];


                readStream.Read(result, 0, (int)w);


                int ko = result.Length;


                SoundEffect effect = SoundEffect.FromStream(daghag);
                SoundEffectInstance instance = effect.CreateInstance();
                instance.Play();


                sd.Close();
            }
            catch (System.Exception ex)
            {
                String strError = ex.ToString();
            }
        }
    }

}


xaml

<phone:PhoneApplicationPage 
    x:Class="WavDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">


    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>


        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
            <Button Name="Merge" Content="merge" Height="100" Margin="50, 50, 50, 50" Click="Btn_Merge"/>
            <Button Name="Play"  Content=" play" Height="100" Margin="50, 50, 50, 50" Click="Btn_Play"/>
        </StackPanel>


        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
    </Grid>
 
    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->


</phone:PhoneApplicationPage>

原创粉丝点击