簡體   English   中英

串聯SoundEffects(wav)並保存到隔離存儲

[英]Concatenating SoundEffects (wav) and saving to isolated storage

我正在辛苦地嘗試為我的這個問題提出一個好的解決方案。

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

基本上,您必須從流中獲取字節,然后合並為一個新的字節數組。 然后將該數組存儲到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 Magazine撰寫了有關WP7音頻的更多信息。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM