Объединение SoundEffects (wav) и сохранение в изолированном хранилище - PullRequest
2 голосов
/ 16 ноября 2011

Я ужасно стараюсь найти хорошее решение для моей проблемы.

У меня есть 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

1 Ответ

1 голос
/ 16 ноября 2011

В основном вы должны получить байты из потока и объединить в новый байтовый массив.Затем сохраните этот массив в 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

Я написал больше об аудио WP7 для журнала CoDe.

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

...