//!!!!было. file stream менялся, пока отключено //protected override Sound _SoundCreate( VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, SoundModes mode ) //{ // criticalSection.Enter(); // OpenALSound sound; // bool initialized; // if( (int)( mode & SoundModes.Stream ) == 0 ) // { // sound = new OpenALSampleSound( stream, soundType, null, mode, out initialized ); // if( closeStreamAfterReading ) // stream.Dispose(); // } // else // sound = new OpenALFileStreamSound( stream, closeStreamAfterReading, soundType, null, mode, out initialized ); // if( !initialized ) // { // sound.Dispose(); // sound = null; // } // criticalSection.Leave(); // return sound; //} protected override Sound _SoundCreateDataBuffer(SoundModes mode, int channels, int frequency, int bufferSize, DataReadDelegate dataReadCallback) { criticalSection.Enter(); Sound sound; if ((mode & SoundModes.Record) != 0) { OpenALCaptureSound captureSound = new OpenALCaptureSound(mode, channels, frequency, bufferSize); if (captureSound.alCaptureDevice == IntPtr.Zero) { criticalSection.Leave(); return(null); } sound = captureSound; } else { sound = new OpenALDataStreamSound(mode, channels, frequency, bufferSize, dataReadCallback); } criticalSection.Leave(); return(sound); }
//!!!!было //internal static void _UpdateAfterEnginePause( double time ) //{ // //update after system pause. it need only for some bugged sound drivers // startTimeAfterEnginePauseUpdate = time; //} protected virtual Sound _SoundCreate(string fileName, SoundModes mode) { Sound sound; string nameWithMode = fileName.ToLower() + " " + ((int)mode & ~(int)SoundModes.Software).ToString(); if (sounds.TryGetValue(nameWithMode, out sound)) { return(sound); } return(null); }
// public OpenALDataStreamSound(SoundModes mode, int channels, int frequency, int bufferSize, SoundWorld.DataReadDelegate dataReadCallback) { this.channels = channels; this.frequency = frequency; this.dataReadCallback = dataReadCallback; this.bufferSize = bufferSize; //if( !GenerateBuffers( 2 ) ) //{ // Log.Warning( "OpenALSoundSystem: Creating data stream sound failed." ); // return; //} Init(null, mode, 100000.0f, channels, frequency); }
// public OpenALCaptureSound(SoundModes mode, int channels, int frequency, int bufferSize) { mode |= SoundModes.Loop | SoundModes.Software; int alFormat = channels == 2 ? Al.AL_FORMAT_STEREO16 : Al.AL_FORMAT_MONO16; alCaptureDevice = Alc.alcCaptureOpenDevice(OpenALSoundWorld.captureDeviceName, frequency, alFormat, bufferSize); if (alCaptureDevice == IntPtr.Zero) { return; } this.channels = channels; this.frequency = frequency; Init(null, mode, 100000.0f, channels, frequency); }
//public Sound Sound //{ // get { return sound; } //} //Sound GetSoundByMode( SoundModes mode ) //{ // soundByMode.TryGetValue( mode, out var sound ); // return sound; //} public Sound LoadSoundByMode(SoundModes mode) { if (!soundByMode.TryGetValue(mode, out var sound)) { //!!!!threading var v = owner.LoadFile.Value; var resourceName = v != null ? v.ResourceName : ""; if (VirtualFile.Exists(resourceName)) { sound = SoundWorld.SoundCreate(resourceName, mode); } soundByMode.Add(mode, sound); } return(sound); }
protected override Sound _SoundCreate(string name, SoundModes mode) { criticalSection.Enter(); OpenALSound sound; sound = (OpenALSound)base._SoundCreate(name, mode); if (sound != null) { criticalSection.Leave(); return(sound); } VirtualFileStream stream = CreateFileStream(name); if (stream == null) { criticalSection.Leave(); Log.Warning(string.Format("Creating sound \"{0}\" failed.", name)); return(null); } bool initialized; if ((mode & SoundModes.Stream) == 0) { sound = new OpenALSampleSound(stream, SoundType.Unknown, name, mode, out initialized); stream.Dispose(); } else { sound = new OpenALFileStreamSound(stream, /*true, */ SoundType.Unknown, name, mode, out initialized); } if (!initialized) { sound.Dispose(); sound = null; } criticalSection.Leave(); return(sound); }
public override void StartAudioStream() { if (sound != null) { return; } SoundModes mode = SoundModes.Loop | SoundModes.Stream; int channels = vorbisInfo.channels; int bufferSize = soundBufferSize; if (oggFile.Sound3D) { mode |= SoundModes.Mode3D; if (vorbisInfo.channels == 2) { channels = 1; bufferSize /= 2; } } sound = SoundWorld.SoundCreateDataBuffer(mode, channels, vorbisInfo.rate, bufferSize, ReadData); if (sound == null) { return; } //!!!!attachedToScene channel = SoundWorld.SoundPlay(null, sound, EngineApp.DefaultSoundChannelGroup, 1, true); if (channel != null) { if (oggFile.Sound3D) { channel.Position = oggFile.SoundPosition; } channel.Volume = oggFile.Volume; channel.Pause = oggFile.Pause; } }
// protected void Init(string name, SoundModes mode, double length, int channels, int frequency) { this.mode = mode; if (name != null) { this.name = name.ToLower(); string nameWithMode = this.name + " " + ((int)mode & ~(int)SoundModes.Software).ToString(); SoundWorld.sounds.Add(nameWithMode, this); } this.length = length; this.channels = channels; this.frequency = frequency; createIndex = createIndexCounter; unchecked { createIndexCounter++; } }
void Play(bool loop) { var soundComponent = ObjectForPreview as Component_Sound; if (soundComponent != null && soundComponent.Result != null) { long length = 0; string fileName = soundComponent.LoadFile.Value.ResourceName; if (!string.IsNullOrEmpty(fileName)) { try { length = VirtualFile.GetLength(fileName); //using( var stream = VirtualFile.Open( fileName ) ) // length = (int)stream.Length; } catch { } } if (length != 0) { SoundModes mode = 0; if (Path.GetExtension(fileName).ToLower() == ".ogg" && length > 400000) { mode |= SoundModes.Stream; } if (loop) { mode |= SoundModes.Loop; } sound = soundComponent.Result.LoadSoundByMode(mode); if (sound != null) { channel = SoundWorld.SoundPlay(null, sound, EngineApp.DefaultSoundChannelGroup, .5f); } } } }
public static Sound SoundCreateDataBuffer(SoundModes mode, int channels, int frequency, int bufferSize, DataReadDelegate dataReadCallback) { return(instance._SoundCreateDataBuffer(mode, channels, frequency, bufferSize, dataReadCallback)); }
protected abstract Sound _SoundCreateDataBuffer(SoundModes mode, int channels, int frequency, int bufferSize, DataReadDelegate dataReadCallback);
public static Sound SoundCreate(string fileName, SoundModes mode) { return(instance._SoundCreate(fileName, mode)); }
protected override Sound _SoundCreateDataBuffer(SoundModes mode, int channels, int frequency, int bufferSize, DataReadDelegate dataBufferCallback) { return(null); }
void Play() { if (playCalling) { return; } playCalling = true; Stop(); if (EnabledInHierarchyAndIsNotResource && ParentScene != null) { var res = ParentRoot?.HierarchyController?.CreatedByResource; if (res != null && res.InstanceType == Resource.InstanceType.SeparateInstance) { var soundComponent = Sound.Value; if (soundComponent != null && soundComponent.Result != null) { bool streaming = false; switch (Streaming.Value) { case StreamingEnum.Auto: { string fileName = soundComponent.LoadFile.Value.ResourceName; if (!string.IsNullOrEmpty(fileName) && Path.GetExtension(fileName).ToLower() == ".ogg") { long length = 0; try { length = VirtualFile.GetLength(fileName); } catch { } if (length > 400000) { streaming = true; } } } break; case StreamingEnum.Enable: streaming = true; break; } SoundModes mode = SoundModes.Mode3D; if (ReplayDelay == 0 || streaming) //if( replayDelay == 0 ) { mode |= SoundModes.Loop; } if (streaming) { mode |= SoundModes.Stream; } sound = soundComponent.Result.LoadSoundByMode(mode); if (sound != null) { channel = SoundWorld.SoundPlay(ParentScene, sound, EngineApp.DefaultSoundChannelGroup, Priority, true); if (channel != null) { channel.Position = Transform.Value.Position; channel.Volume = Volume; UpdateChannelRolloffGraph(); channel.Pitch = Pitch; OnBeforePlay(); BeforePlayEvent?.Invoke(this); channel.Pause = false; lastUpdateEngineTime = EngineApp.EngineTime; } } } } } playCalling = false; }
unsafe public OpenALSampleSound(VirtualFileStream stream, SoundType soundType, string name, SoundModes mode, out bool initialized) { initialized = false; byte[] samples; int sizeInBytes; float timeLength; if (string.Compare(Path.GetExtension(name), ".ogg", true) == 0) { //ogg VorbisFileReader vorbisFileReader = new VorbisFileReader(stream, false); VorbisFile.File vorbisFile = new VorbisFile.File(); if (!vorbisFileReader.OpenVorbisFile(vorbisFile)) { vorbisFile.Dispose(); vorbisFileReader.Dispose(); Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" (Reading failed).", name); return; } int numSamples = (int)vorbisFile.pcm_total(-1); vorbisFile.get_info(-1, out channels, out frequency); timeLength = (float)vorbisFile.time_total(-1); sizeInBytes = numSamples * channels * 2; samples = new byte[sizeInBytes]; fixed(byte *pSamples = samples) { int samplePos = 0; while (samplePos < sizeInBytes) { int readBytes = vorbisFile.read((IntPtr)(pSamples + samplePos), sizeInBytes - samplePos, 0, 2, 1, IntPtr.Zero); if (readBytes <= 0) { break; } samplePos += readBytes; } } vorbisFile.Dispose(); vorbisFileReader.Dispose(); } else if (string.Compare(Path.GetExtension(name), ".wav", true) == 0) { //wav string error; if (!WavLoader.Load(stream, out channels, out frequency, out samples, out sizeInBytes, out error)) { Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" ({1}).", name, error); return; } timeLength = (float)(samples.Length / channels / 2) / (float)frequency; } else { Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" (Unknown file type).", name); return; } //create buffer Al.alGenBuffers(1, out alBuffer); int alFormat = (channels == 1) ? Al.AL_FORMAT_MONO16 : Al.AL_FORMAT_STEREO16; //bug fix: half volume mono 2D sounds //convert to stereo if ((mode & SoundModes.Mode3D) == 0 && alFormat == Al.AL_FORMAT_MONO16) { byte[] stereoSamples = new byte[sizeInBytes * 2]; for (int n = 0; n < sizeInBytes; n += 2) { stereoSamples[n * 2 + 0] = samples[n]; stereoSamples[n * 2 + 1] = samples[n + 1]; stereoSamples[n * 2 + 2] = samples[n]; stereoSamples[n * 2 + 3] = samples[n + 1]; } samples = stereoSamples; alFormat = Al.AL_FORMAT_STEREO16; sizeInBytes *= 2; } //convert to mono for 3D if ((mode & SoundModes.Mode3D) != 0 && channels == 2) { byte[] oldSamples = samples; samples = new byte[oldSamples.Length / 2]; for (int n = 0; n < samples.Length; n += 2) { samples[n + 0] = oldSamples[n * 2 + 0]; samples[n + 1] = oldSamples[n * 2 + 1]; } alFormat = Al.AL_FORMAT_MONO16; sizeInBytes /= 2; } fixed(byte *pSamples = samples) Al.alBufferData(alBuffer, alFormat, pSamples, sizeInBytes, frequency); if (OpenALSoundWorld.CheckError()) { Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\".", name); return; } Init(name, mode, timeLength, channels, frequency); initialized = true; }
// //public OpenALFileStreamSound( VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, string name, SoundModes mode, out bool initialized ) public OpenALFileStreamSound(VirtualFileStream stream, SoundType soundType, string name, SoundModes mode, out bool initialized) { initialized = false; if (soundType == SoundType.Unknown) { if (name != null) { soundType = GetSoundTypeByName(name); } else { soundType = GetSoundTypeByStream(stream); } } if (soundType != SoundType.OGG) { Log.Warning(string.Format("Streaming is not supported for \"{0}\" files ({1}).", soundType, name)); return; } //get sound properties var vorbisFile = new VorbisFile.File(); var vorbisFileReader = new VorbisFileReader(stream, true); // closeStreamAfterReading ); if (!vorbisFileReader.OpenVorbisFile(vorbisFile)) { vorbisFileReader.Dispose(); Log.Warning(string.Format("Creating sound \"{0}\" failed.", name)); return; } long numSamples = vorbisFile.pcm_total(-1); vorbisFile.get_info(-1, out channels, out frequency); vorbisFile?.Dispose(); vorbisFileReader?.Dispose(); //vorbisFile = new VorbisFile.File(); //vorbisFileReader = new VorbisFileReader( stream, closeStreamAfterReading ); //if( !vorbisFileReader.OpenVorbisFile( vorbisFile ) ) //{ // vorbisFileReader.Dispose(); // Log.Warning( string.Format( "Creating sound \"{0}\" failed.", name ) ); // return; //} //long numSamples = vorbisFile.pcm_total( -1 ); //vorbisFile.get_info( -1, out channels, out frequency ); //convert to mono for 3D if ((mode & SoundModes.Mode3D) != 0 && channels == 2) { needConvertToMono = true; channels = 1; } //if( !GenerateBuffers( 2 ) ) //{ // Log.Warning( "OpenALSoundSystem: Creating sound failed \"{0}\".", name ); // return; //} double length = (double)numSamples / (double)frequency; Init(name, mode, (float)length, channels, frequency); initialized = true; }