public override void _Ready() { // Called every time the node is added to the scene. // Initialization here statManager = (StatManager)GetNode("/root/StatManager"); sample = (AudioStreamSample)Stream; }
public static AudioStreamSample GetSoundClip(int idx) { AudioStreamSample result = _vswapSound.ContainsKey(idx) ? _vswapSound[idx] : null; if (result == null) { byte[] data = VSWAP.GetSoundData((uint)idx); if (data != null && data.Length > 0) { for (int i = 0; i < data.Length; i++) { unchecked { data[i] -= 0x80; } } result = new AudioStreamSample(); result.Format = AudioStreamSample.FormatEnum.Format8Bits; result.MixRate = SoundSampleRate; result.LoopMode = AudioStreamSample.LoopModeEnum.Disabled; result.Data = data; } } return(result); }
private void OnRecordUp() { playBt.Disabled = false; if (recordEffect.IsRecordingActive()) { recordEffect.SetRecordingActive(false); rec = recordEffect.GetRecording(); rec.SaveToWav(System.IO.Path.Combine(OS.GetUserDataDir(), FILENAME)); } }
public static AudioStream loadRaw(byte[] buffer, int sample_rate = 11025) { var sample = new AudioStreamSample(); sample.Format = AudioStreamSample.FormatEnum.Format8Bits; sample.MixRate = sample_rate; sample.Stereo = false; sample.Data = buffer; //sample.LoopMode = loop ? AudioStreamSample.LoopModeEnum.PingPong : AudioStreamSample.LoopModeEnum.Disabled; return(sample); }
public void PlaySfx(AudioStreamSample stream, Node owner, string bus = "SfxA") { AudioStreamPlayer player = new AudioStreamPlayer(); player.SetStream(stream); player.SetBus(bus); owner.AddChild(player); player.Play(); Logger.Debug("Play Sfx: " + stream.ResourcePath); }
public void PlayAmbient(string audioPath) { AudioStreamSample audio = ResourceLoader.Load <AudioStreamSample>(audioPath); if (!_ambient.Playing) { _ambient.SetStream(audio); _ambient.Play(); } else { Iterator.Coroutine.Run(FadeOutAndStartNewStream(_ambient, audio)); } }
public static AudioStreamSample AudioBufferFromFile(string songPath) { // Load wav buffer from song file File songFile = new File(); songFile.Open(songPath, File.ModeFlags.Read); var buffer = songFile.GetBuffer((int)songFile.GetLen()); songFile.Close(); // Load audio stream from wav buffer AudioStreamSample stream = new AudioStreamSample(); stream.Format = AudioStreamSample.FormatEnum.Format16Bits; stream.Data = buffer; stream.Stereo = true; return(stream); }
private void OnPlayDown() { recordBt.Disabled = true; var audioPlayer = GetTree().Root.GetNode <Main>("Main").Audio; if (!audioPlayer.Playing) { AudioStreamSample savedAudStream = new AudioStreamSample(); using (var savedAudFile = new File()) { if (savedAudFile.FileExists("user://" + FILENAME)) { savedAudFile.Open("user://" + FILENAME, File.ModeFlags.Read); savedAudStream.Data = savedAudFile.GetBuffer((int)savedAudFile.GetLen()); savedAudStream.Format = AudioStreamSample.FormatEnum.Format16Bits; savedAudStream.Stereo = true; savedAudFile.Close(); } } savedAudStream.Play(audioPlayer); } }
public override void _Ready() { AudioStreamSample stream = Stream as AudioStreamSample; byte[] samplesBytes = stream?.Data; sampleRate = stream.MixRate; //Convert bytes to signed 16-bit shorts var samplesShorts = BytesToShorts(samplesBytes); //Convert signed 16-bit shorts to floats var samplesFloats = ShortsToFloats(samplesShorts); //Convert floats to frames samplesFrames = FloatsToFrames(samplesFloats); Play(); //Don't autoplay - causes desync stopwatch.Start(); _Process(0); //this works but it's SLOW //((GradientTexture) GetNode<TextureRect>("debugRect").Texture).Gradient = gradient; }
private void PlaySpeechPart(string txtKey, int partIndex, int totalPartCount) { AudioStreamSample audio = ResourceLoader.Load <AudioStreamSample>(SpeechManager.TextPartAudioPath(txtKey, partIndex)); if (!_speech.Playing) { _speech.SetStream(audio); _speech.Play(); } else { Iterator.Coroutine.Run(FadeOutAndStartNewStream(_speech, audio)); } if ((bool)Game.UserPrefs.GetUserPref(UserPrefCategory.Audio, "SHOW_SUBTITLES")) { Game.SubtitleManager.Play(SpeechManager.Text(txtKey, partIndex)); } if (partIndex < (totalPartCount - 1)) { Iterator.Coroutine.Run(WaitUntilSpeechFinishedThenPlayNextSpeechPart(txtKey, partIndex + 1, totalPartCount)); } }
public DoorSliding(int x, int y, Level level) { if (level != null) { Location = new Point2(x, y); Level = level; Type = (DoorType)level.Map.Planes[(int)Level.Planes.Walls][y, x]; level.Cells[y, x] = Level.Cell.Default(); // Adjust neighboring walls to show the door excavation. if (!IsVertical) { if ((x - 1) > -1 && level.Cells[y, x - 1].East != Level.Cell.NoWall) { level.Cells[y, x - 1].East = DoorWestEastWall; } if ((x + 1) < level.Map.Width && level.Cells[y, x + 1].West != Level.Cell.NoWall) { level.Cells[y, x + 1].West = DoorWestEastWall; } } else { if ((y + 1) < level.Map.Height && level.Cells[y + 1, x].North != Level.Cell.NoWall) { level.Cells[y + 1, x].North = DoorNorthSouthWall; } if ((y - 1) > -1 && level.Cells[y - 1, x].South != Level.Cell.NoWall) { level.Cells[y - 1, x].South = DoorNorthSouthWall; } } // Create a static body for the cell so that // we can block the player from entering or exiting the cell // when the door is closed or moving. _cellShape = new CollisionShape(); BoxShape box = new BoxShape(); box.Extents = new Vector3(Level.CellSize * 0.5f, Level.CellSize * 0.5f, Level.CellSize * 0.5f); _cellShape.Shape = box; _cellShape.Name = "CollisionShape"; _cellBody = new StaticBody(); _cellBody.CollisionLayer = (uint)Level.CollisionLayers.Static; _cellBody.CollisionMask = (uint)(Level.CollisionLayers.Characters); _cellBody.AddChild(_cellShape); AddChild(_cellBody); // Create the actual mesh for the door and set it up for // this particular instance. BuildDoorMesh(); _mesh = new MeshInstance(); _mesh.Mesh = _doorMesh; _mesh.MaterialOverride = Assets.GetTexture(_doorTexture[Type]); if (IsVertical) { _mesh.Transform = Transform.Identity.Rotated(Vector3.Up, Mathf.Pi * 0.5f); } else { _mesh.Transform = Transform.Identity.Rotated(Vector3.Up, Mathf.Pi); } // Setup a physics body for the door itself for the purposes // of detecting collisions with projectiles since projectiles shouldn't // be blocked by the cell's static body that is normally only used for // ensuring characters can't enter or exit the cell if the door is closed // or moving. _doorShape = new CollisionShape(); box = new BoxShape(); box.Extents = new Vector3(Level.CellSize * 0.5f, Level.CellSize * 0.5f, Mathf.Epsilon); _doorShape.Shape = box; _doorShape.Name = "CollisionShape"; _doorBody = new RigidBody(); _doorBody.CollisionLayer = (uint)Level.CollisionLayers.Doors; _doorBody.CollisionMask = (uint)Level.CollisionLayers.Projectiles; _doorBody.AddChild(_doorShape); _doorBody.Mode = RigidBody.ModeEnum.Static; _mesh.AddChild(_doorBody); AddChild(_mesh); // Add myself to the world and set my position // along with some default state variables. level.AddChild(this); Transform tform = this.Transform; tform.origin = level.MapToWorld(x, y); this.Transform = tform; State = DoorState.Closed; _openCloseDuration = 0.75f; _canClose = true; _openSound = Assets.GetSoundClip(Assets.DigitalSoundList.DoorOpening); _closeSound = Assets.GetSoundClip(Assets.DigitalSoundList.DoorClosing); // Add a tween node for controlling the animation of the // door opening and closing. _tween = new Tween(); _tween.Connect("tween_all_completed", this, "OnTweenCompleted"); _mesh.AddChild(_tween); // Add an audio player so we can emit a sound // when the door opens and closes. _audioPlayer = new AudioStreamPlayer3D(); _audioPlayer.Name = "AudioPlayer"; AddChild(_audioPlayer); SetProcess(true); SetPhysicsProcess(true); } }
private void ToWav(MemoryStream stream) { float[] data = null; using (BinaryReader reader = new BinaryReader(stream)) { bool formatFound = false; bool dataFound = false; int compressionCode = 0; int channel = 0; int sampleRate = 0; int bitForSample = 0; /* Check RIFF */ byte[] riff = new byte[12]; reader.Read(riff, 0, 12); if (riff[0] != 'R' || riff[1] != 'I' || riff[2] != 'F' || riff[3] != 'F') { throw new FileLoadException(); } /* Check WAVE */ if (riff[8] != 'W' || riff[9] != 'A' || riff[10] != 'V' || riff[11] != 'E') { throw new FileLoadException("Not a WAV file (no WAVERIFF header)."); } while (reader.BaseStream.Position != reader.BaseStream.Length) { var oldPosition = reader.BaseStream.Position; byte[] chunkID = new byte[4]; reader.Read(chunkID, 0, 4); var chunkSize = reader.ReadInt32(); /* Check format */ if (chunkID[0] == 'f' && chunkID[1] == 'm' && chunkID[2] == 't' && chunkID[3] == ' ' && !formatFound) { /* Check audio format */ compressionCode = reader.ReadInt16(); if (compressionCode != 1 && compressionCode != 3) { throw new FileLoadException("Format not supported for WAVE file (not PCM). Save WAVE files as uncompressed PCM instead."); } /* Check Channel */ channel = reader.ReadInt16(); if (channel != 1 && channel != 2) { throw new FileLoadException("Format not supported for WAVE file (not stereo or mono)."); } /* Get Sample Rate */ sampleRate = reader.ReadInt32(); reader.ReadBytes(6); bitForSample = reader.ReadUInt16(); if (bitForSample % 8 != 0 || bitForSample == 0) { throw new FileLoadException("Invalid amount of bits in the sample (should be one of 8, 16, 24 or 32)."); } formatFound = true; } /* Get Data */ if (chunkID[0] == 'd' && chunkID[1] == 'a' && chunkID[2] == 't' && chunkID[3] == 'a' && !dataFound) { dataFound = true; if (!formatFound) { throw new FileLoadException("'data' chunk before 'format' chunk found."); } int frames = chunkSize; if (channel == 0) { throw new FileLoadException(); } frames /= channel; frames /= (bitForSample >> 3); data = new float[frames * channel]; if (bitForSample == 8) { for (int i = 0; i < frames * channel; i++) { // 8 bit samples are UNSIGNED data[i] = (reader.ReadByte() - 128) / 128f; } } else if (bitForSample == 32 && compressionCode == 3) { for (int i = 0; i < frames * channel; i++) { //32 bit IEEE Float data[i] = reader.ReadSingle(); } } else if (bitForSample == 16) { for (int i = 0; i < frames * channel; i++) { //16 bit SIGNED data[i] = reader.ReadInt16() / 32768f; } } else { for (int i = 0; i < frames * channel; i++) { //16+ bits samples are SIGNED // if sample is > 16 bits, just read extra bytes Int32 s = 0; for (int b = 0; b < (bitForSample >> 3); b++) { s |= reader.ReadByte() << (b * 8); } s <<= (32 - bitForSample); data[i] = (s >> 16) / 32768f; } } } if (oldPosition + chunkSize + 8 > reader.BaseStream.Position) { reader.ReadBytes((int)(oldPosition + chunkSize + 8 - reader.BaseStream.Position)); } } var sample = new AudioStreamSample(); bool is16 = bitForSample != 8; var dstData = new byte[data.Length * (is16 ? 2 : 1)]; for (int i = 0; i < data.Length; i++) { if (is16) { var v = Mathf.Clamp(data[i] * 32768, -32768, 32767); var b = BitConverter.GetBytes(Convert.ToInt16(v)); dstData[i * 2] = b[0]; dstData[i * 2 + 1] = b[1]; } else { var v = Mathf.Clamp(data[i] * 128, -128, 127); dstData[i] = Convert.ToByte(v); } } sample.Data = dstData; sample.Format = bitForSample == 8 ? AudioStreamSample.FormatEnum.Format8Bits : AudioStreamSample.FormatEnum.Format16Bits; sample.MixRate = sampleRate; sample.Stereo = channel == 2; AudioStream = sample; } }