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>
- WP7-wav 合并demo
- ffmpeg 多wav合并
- 用c#合并wav文件。
- 合并多个wav文件
- 安卓Wav音频文件合并
- WP7录音-IsolatedStorage保存wav文件-IsolatedStorage上传wav录音文件(完整版)
- windows播放wav声音文件--java播放wav完整demo
- 将两个Wav文件合并为一个
- 合并两个格式相同的wav文件
- Java中wav格式语音文件合并
- wp7
- wav
- WAV
- WP7-WebService-SQL Server-IIS综合应用Demo
- Qt 使用 phonon 播放 wav音乐文件 的 demo
- HDFS过滤合并并上传文件demo
- php 生成图片水印且合并多个图片 Demo
- Java 关于两个集合合并,并排序的demo
- PNG做WinMobile程序快捷方式
- 无线上网卡安装与卸载时导致本地网络闪断
- linux开机流程
- 查找 EXC_BAD_ACCESS 问题根源的方法
- 加载plist文件中的数据,两种方法
- WP7-wav 合并demo
- 删除表空间操作
- C++程序设计实验报告(一)
- Android应用开发之SQLite数据库
- C++程序设计实验报告(二)
- SCI、EI和ISTP收录号的查询方法
- linux apt-get 命令大全
- 第八周实验指导
- 小议大小端模式对C语言的共用体结构的影响