// for serialization internal void Load(Stream stream) { #if SILICONSTUDIO_PLATFORM_ANDROID var virtualStream = stream as VirtualFileStream; if (virtualStream == null) { throw new InvalidOperationException("Expecting VirtualFileStream. Music files needs to be stored on the virtual file system in a non-compressed form."); } var fileStream = virtualStream.InternalStream as FileStream; if (fileStream == null) { throw new InvalidOperationException("Expecting FileStream in VirtualFileStream.InternalStream. Music files needs to be stored on the virtual file system in a non-compressed form."); } FileName = fileStream.Name; StartPosition = virtualStream.StartPosition; Length = virtualStream.Length; #else // Make a memory copy of the stream so that the source can be properly disposed var memoryStream = new MemoryStream(); stream.CopyTo(memoryStream); Stream = memoryStream; Stream.Position = 0; #endif ResetStateToDefault(); Name = "SoundMusic " + soundMusicCreationCount; AudioEngine.RegisterSound(this); Interlocked.Increment(ref soundMusicCreationCount); }
internal void Attach(AudioEngine engine) { AttachEngine(engine); Name = "Sound Effect " + Interlocked.Add(ref soundEffectCreationCount, 1); // register the sound to the AudioEngine so that it will be properly freed if AudioEngine is disposed before this. AudioEngine.RegisterSound(this); }
internal void Load(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (AudioEngine.IsDisposed) { throw new ObjectDisposedException("Audio Engine"); } // create a native memory stream to extract the lz4 audio stream. nativeDataBuffer = Utilities.AllocateMemory((int)stream.Length); var nativeStream = new NativeMemoryStream(nativeDataBuffer, stream.Length); stream.CopyTo(nativeStream); nativeStream.Position = 0; var waveStreamReader = new SoundStream(nativeStream); var waveFormat = waveStreamReader.Format; if (waveFormat.Channels > 2) { throw new NotSupportedException("The wave file contains more than 2 data channels. Only mono and stereo formats are currently supported."); } if (waveFormat.Encoding != WaveFormatEncoding.Pcm || waveFormat.BitsPerSample != 16) { throw new NotSupportedException("The wave file audio format is not supported. Only 16bits PCM encoded formats are currently supported."); } WaveFormat = waveFormat; WaveDataPtr = nativeDataBuffer + (int)nativeStream.Position; WaveDataSize = (int)waveStreamReader.Length; Name = "Sound Effect " + soundEffectCreationCount; AdaptAudioDataImpl(); // register the sound to the AudioEngine so that it will be properly freed if AudioEngine is disposed before this. AudioEngine.RegisterSound(this); //create a default instance only when actually we load, previously we were creating this on demand which would result sometimes in useless creations and bugs within the editor DefaultInstance = CreateInstance(); //copy back values we might have set before default instance creation DefaultInstance.Pan = defaultPan; DefaultInstance.Volume = defaultVolume; DefaultInstance.IsLooped = defaultIsLooped; Interlocked.Increment(ref soundEffectCreationCount); }
internal void Load(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } if (AudioEngine.IsDisposed) { throw new ObjectDisposedException("Audio Engine"); } // create a native memory stream to extract the lz4 audio stream. nativeDataBuffer = Utilities.AllocateMemory((int)stream.Length); var nativeStream = new NativeMemoryStream(nativeDataBuffer, stream.Length); stream.CopyTo(nativeStream); nativeStream.Position = 0; var waveStreamReader = new SoundStream(nativeStream); var waveFormat = waveStreamReader.Format; if (waveFormat.Channels > 2) { throw new NotSupportedException("The wave file contains more than 2 data channels. Only mono and stereo formats are currently supported."); } if (waveFormat.Encoding != WaveFormatEncoding.Pcm || waveFormat.BitsPerSample != 16) { throw new NotSupportedException("The wave file audio format is not supported. Only 16bits PCM encoded formats are currently supported."); } WaveFormat = waveFormat; WaveDataPtr = nativeDataBuffer + (int)nativeStream.Position; WaveDataSize = (int)waveStreamReader.Length; Name = "Sound Effect " + soundEffectCreationCount; AdaptAudioDataImpl(); // register the sound to the AudioEngine so that it will be properly freed if AudioEngine is disposed before this. AudioEngine.RegisterSound(this); Interlocked.Increment(ref soundEffectCreationCount); }
/// <summary> /// Create a dynamic sound effect instance with the given sound properties. /// </summary> /// <param name="engine">The engine in which the dynamicSoundEffectInstance is created</param> /// <param name="sampleRate">Sample rate, in Hertz (Hz), of audio content. Must between 8000 Hz and 48000 Hz</param> /// <param name="channels">Number of channels in the audio data.</param> /// <param name="encoding">Encoding of a sound data sample</param> /// <returns>A new DynamicSoundEffectInstance instance ready to filled with data and then played</returns> /// <exception cref="ArgumentOutOfRangeException">This exception is thrown for one of the following reason: /// <list type="bullet"> /// <item>The value specified for sampleRate is less than 8000 Hz or greater than 48000 Hz. </item> /// <item>The value specified for channels is something other than mono or stereo. </item> /// <item>The value specified for data encoding is something other than 8 or 16 bits. </item> /// </list> /// </exception> /// <exception cref="ArgumentNullException"><paramref name="engine"/> is null.</exception> public DynamicSoundEffectInstance(AudioEngine engine, int sampleRate, AudioChannels channels, AudioDataEncoding encoding) { AttachEngine(engine); if (sampleRate < 8000 || 48000 < sampleRate) { throw new ArgumentOutOfRangeException("sampleRate"); } if (channels != AudioChannels.Mono && channels != AudioChannels.Stereo) { throw new ArgumentOutOfRangeException("channels"); } if (encoding != AudioDataEncoding.PCM_8Bits && encoding != AudioDataEncoding.PCM_16Bits) { throw new ArgumentOutOfRangeException("encoding"); } waveFormat = new WaveFormat(sampleRate, (int)encoding, (int)channels); Interlocked.Increment(ref totalNbOfInstances); Interlocked.Increment(ref numberOfInstances); // first instance of dynamic sound effect instance => we create the workerThead and the associated event. if (numberOfInstances == 1) { instancesNeedingBuffer = new ThreadSafeQueue <DynamicSoundEffectInstance>(); // to be sure that there is no remaining request from previous sessions awakeWorkerThread = new AutoResetEvent(false); CreateWorkerThread(); } Name = "Dynamic Sound Effect Instance - " + totalNbOfInstances; CreateVoice(WaveFormat); InitializeDynamicSound(); AudioEngine.RegisterSound(this); ResetStateToDefault(); }