void CreateSound() { // Sound source needs a node so that it is considered enabled node = new Node(); SoundSource source = node.CreateComponent <SoundSource>(); soundStream = new BufferedSoundStream(); // Set format: 44100 Hz, sixteen bit, mono soundStream.SetFormat(44100, true, false); // Start playback. We don't have data in the stream yet, but the SoundSource will wait until there is data, // as the stream is by default in the "don't stop at end" mode source.Play(soundStream); }
// https://github.com/microsoft/MixedRealityCompanionKit/blob/master/LegacySpectatorView/Samples/SharedHolograms/Assets/HoloToolkit/Utilities/Scripts/TextToSpeech.cs private async Task <BufferedSoundStream> SpeechStreamToSoundStream(SpeechSynthesisStream inStream) { var outStream = new BufferedSoundStream(); uint size = (uint)inStream.Size; byte[] wavAudio = new byte[size]; using (var inputStream = inStream.GetInputStreamAt(0)) { inStream.Dispose(); using (var reader = new DataReader(inputStream)) { await reader.LoadAsync(size); reader.ReadBytes(wavAudio); } } int channelCount = wavAudio[22]; int frequency = BytesToInt(wavAudio, 24); outStream.SetFormat((uint)frequency, true, channelCount == 2); // Get past all the other sub chunks to get to the data subchunk: int pos = 12; // First subchunk ID from 12 to 16 // Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal)) while (!(wavAudio[pos] == 100 && wavAudio[pos + 1] == 97 && wavAudio[pos + 2] == 116 && wavAudio[pos + 3] == 97)) { pos += 4; int chunkSize = wavAudio[pos] + wavAudio[pos + 1] * 256 + wavAudio[pos + 2] * 65536 + wavAudio[pos + 3] * 16777216; pos += 4 + chunkSize; } pos += 8; // Pos is now positioned to start of actual sound data. int sampleCount = (wavAudio.Length - pos) / 2; // 2 bytes per sample (16 bit sound mono) if (channelCount == 2) { sampleCount /= 2; } // 4 bytes per sample (16 bit stereo) outStream.AddData(wavAudio, pos); return(outStream); }