连接声音效果(wav)并保存到独立存储

本文关键字:保存 独立 存储 wav 声音 音效 连接 | 更新日期: 2023-09-27 18:14:37

我正绞尽脑汁想办法解决我的这个问题。

我有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

连接声音效果(wav)并保存到独立存储

基本上你必须从流中获得字节并组合成一个新的字节数组。然后将该数组存储到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