【问题标题】:Concatenating SoundEffects (wav) and saving to isolated storage连接 SoundEffects (wav) 并保存到独立存储
【发布时间】:2011-11-16 08:04:26
【问题描述】:

我很难想出一个好的解决方案来解决我的这个问题。

我有 3 种音效在手机上标记为内容,比特率都相同。

  • sound1.wav
  • sound2.wav
  • sound3.wav

我希望用户能够以任何顺序或任意次数选择 wav 文件的播放顺序,然后根据他们的选择生成一个新的 wav 文件,该文件可以存储和从隔离存储中带回来。

到目前为止,Thank You Walt Ritscher 有帮助,但你最终会看到我的编码技能充其量是零散的。我在下面的代码中试图传达的是,用户将被提示选择任何/所有带有点击事件的声音,他的选择将决定新的音效听起来像什么(它的顺序等)但是还有很多我不知道的,这是我想出的代码(虽然不在我的编码计算机上);

//SO I have this master list of sounds to use, indicated in this block of code:
//sound1.wav, sound2.wav, sound3.wav
// my wav files are marked as resources
// get the wav file from the DLL
var streamInfo1 = Application.GetResourceStream(
    new Uri(sound1.wav, UriKind.Relative));
var streamInfo2 = Application.GetResourceStream(
    new Uri(sound2.wav, UriKind.Relative));
var streamInfo3 = Application.GetResourceStream(
    new Uri(sound3.wav, UriKind.Relative));

//With the above declarations made, I run each one as a stream as a variable.
var stream1 = streamInfo1.Stream as UnmanagedMemoryStream;
var stream2 = streamInfo2.Stream as UnmanagedMemoryStream;
var stream3 = streamInfo3.Stream as UnmanagedMemoryStream;



//The user can choose the sounds in any order, and repeat if desired.
//Let us assume the user chose in this order:
//sound1.wav + sound1.wav + sound2.wav +sound3.wav


for (int i = 0; i < 10; i++)
{
  var bytesA = new byte[stream1.Length];
  var bytesB = new byte[stream1.Length];
  var bytesC = new byte[stream2.Length];
}

// read the bytes from the stream into the array
stream1.Read(bytesA, 0, (int)stream1.Length);
stream2.Read(bytesB, 0, (int)stream1.Length);
stream3.Read(bytesC, 0, (int)stream2.Length);

var combined = new byte[bytesA.Length + bytesA.Length + bytesB.Length] + bytesC.Length]];

// copy the bytes into the combined array
System.Buffer.BlockCopy(bytesA, 0, combined, 0, bytesA.Length);
System.Buffer.BlockCopy(bytesB, 0, combined, bytesA.Length, bytesB.Length);

var outputStream = new MemoryStream();
outputStream.Write(combined, 0, combined.Length);

// substitute your own sample rate
var effect = new SoundEffect(
      buffer: outputStream.ToArray(),
      sampleRate: 48000,
      channels: AudioChannels.Mono);
var instance = effect.CreateInstance();
instance.Play();

// save stream to IsolatedStorage

【问题讨论】:

    标签: c# silverlight windows-phone-7 bytearray soundeffect


    【解决方案1】:

    基本上,您必须从流中获取字节并组合成一个新的字节数组。然后将该数组存储到 UnmanagedMemoryStream 中。

       // my wav files are marked as resources
       // get the wav file from the DLL
       var streamInfo1 = Application.GetResourceStream(
            new Uri(loopWav, UriKind.Relative));
       var streamInfo2 = Application.GetResourceStream(
            new Uri(beatWav, UriKind.Relative));
    
        var stream1 = streamInfo1.Stream as UnmanagedMemoryStream;
        var stream2 = streamInfo2.Stream as UnmanagedMemoryStream;
    
        var bytesA = new byte[stream1.Length];
        var bytesB = new byte[stream2.Length];
    
        // read the bytes from the stream into the array
        stream1.Read(bytesA, 0, (int)stream1.Length);
        stream2.Read(bytesB, 0, (int)stream2.Length);
    
        var combined = new byte[bytesA.Length + bytesB.Length];
    
        // copy the bytes into the combined array
        System.Buffer.BlockCopy(bytesA, 0, combined, 0, bytesA.Length);
        System.Buffer.BlockCopy(bytesB, 0, combined, bytesA.Length, bytesB.Length);
    
        var outputStream = new MemoryStream();
        outputStream.Write(combined, 0, combined.Length);
    
        // substitute your own sample rate
        var effect = new SoundEffect(
              buffer: outputStream.ToArray(),
              sampleRate: 48000,
              channels: AudioChannels.Mono);
    
    
        var instance = effect.CreateInstance();
        instance.Play();
    
        // save stream to IsolatedStorage
    

    我为 CoDe 杂志撰写了更多关于 WP7 音频的文章。

    http://www.code-magazine.com/Article.aspx?quickid=1109071

    【讨论】:

    • 在尝试将其应用到我需要的东西(感谢您提供精彩的代码示例)时,我应该继续为每个声音文件提供一个唯一的“var bytes#”并创建一个相应的唯一“流#.read” 行。我可以创建一个 while (i=0, i
    • 我会回答我自己的问题,只是为了更好地了解代码,并在问题解决后将其删除以给予您适当的信任:)
    • 您可以修改自己的问题并在那里添加视觉效果。
    • 我尽我所能从记忆中做到这一点,但由于我没有坐在家里的电脑前,我无法创建最优雅(或正确)的代码。我希望它传达了我的目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多