// Update is called once per frame void Update() { if (PlayClip && !audioSource.isPlaying) { audioSource.Play(); PlayClip = false; } else if (!PlayClip && audioSource.isPlaying) { //audioSource.Stop(); } if (Play) { extraTimeSinceLastUpdate += Time.deltaTime; int samplesSinceLastUpdate = (int)(Channels * Frequency * extraTimeSinceLastUpdate); //check if audioclip ended samplesSinceLastUpdate = Math.Min(CurrentAudioClip.samples - SamplesPlayedInClip, samplesSinceLastUpdate); extraTimeSinceLastUpdate -= (float)samplesSinceLastUpdate / (float)(Channels * Frequency); float[] samplesPlayed = new float[samplesSinceLastUpdate]; CurrentAudioClip.GetData(samplesPlayed, SamplesPlayedInClip); RecentlyPlayedAudio.SetData(samplesPlayed, TotalSamplesPlayed % RecentlyPlayedAudio.samples); SamplesPlayedInClip += samplesSinceLastUpdate; TotalSamplesPlayed += samplesSinceLastUpdate; if (SamplesPlayedInClip == CurrentAudioClip.samples) { WavHelper.WriteWavData("CurrentAudioClip.txt", CurrentAudioClip, 0); //TODO: check channels for each clip AudioClipIndex = (AudioClipIndex + 1) % AudioClipsToPlay.Count; //next and wrap Debug.Log($"Switching to next audiofile {CurrentAudioClip.name}"); SamplesPlayedInClip = 0; Play = false; WavHelper.WriteWavData("RecentlyPlayedAudio.txt", RecentlyPlayedAudio, 0); } VokaturiIntervalRemaining -= Time.deltaTime; if (VokaturiIntervalRemaining <= 0) { //calculate emotions List <VokaturiAnalysis> vokaturiAnalyses = Vokaturi.CalculateEmotion(RecentlyPlayedAudio); Neutrality = vokaturiAnalyses.Average(va => va.EmotionProbabilities.Neutrality); Happiness = vokaturiAnalyses.Average(va => va.EmotionProbabilities.Happiness); Sadness = vokaturiAnalyses.Average(va => va.EmotionProbabilities.Sadness); Anger = vokaturiAnalyses.Average(va => va.EmotionProbabilities.Anger); Fear = vokaturiAnalyses.Average(va => va.EmotionProbabilities.Fear); VokaturiIntervalRemaining = VokaturiInterval; } } }
void TouchRayCast() { for (int i = 0; i < Input.touchCount; ++i) { Vector2 test = theCamera.ScreenToWorldPoint(Input.GetTouch(i).position); if (Input.GetTouch(i).phase == TouchPhase.Began) { test = theCamera.ScreenToWorldPoint(Input.GetTouch(i).position); RaycastHit2D hit = Physics2D.Raycast(test, (Input.GetTouch(i).position)); if (hit.collider && hit.collider.tag == "Touchable") { recordButton.color = Color.red; state = "Recording....."; clip = Microphone.Start("", false, 2, 44100); } } else if (Input.GetTouch(i).phase == TouchPhase.Ended) { if (Microphone.IsRecording("")) {//if the recorder is recording Debug.Log("End record"); Microphone.End("");//stop to record } recordButton.color = Color.green; string filename = GameObject.Find("ImageOfRecording").GetComponent <Image>().sprite.name; Debug.Log("start to save"); state = "Successful save"; WavHelper.Save(filename, clip, saveInfoText.text);//Save the voice recorded source.clip = clip; source.Play(); } } }
private unsafe float InitSound(float elapsedSinceLastCall, float elapsedTimeSinceLastFlightLoop, int counter) { CheckError(); var oldContext = ALC.GetCurrentContext(); if (oldContext == default) { XPlane.Trace.WriteLine("[OpenAL Sample] I found no OpenAL, I will be the first to init."); _device = ALC.OpenDevice(null); if (_device == null) { XPlane.Trace.WriteLine("[OpenAL Sample] Could not open the default OpenAL device."); return(0); } _context = ALC.CreateContext(_device, new ALContextAttributes()); if (_context == null) { ALC.CloseDevice(_device); _device = default; XPlane.Trace.WriteLine("[OpenAL Sample] Could not open the default OpenAL device."); return(0); } ALC.MakeContextCurrent(_context); XPlane.Trace.WriteLine("[OpenAL Sample] Created the Open AL context."); var hardware = ALC.GetString(_device, AlcGetString.DeviceSpecifier); var extensions = ALC.GetString(_device, AlcGetString.Extensions); var major = ALC.GetInteger(_device, AlcGetInteger.MajorVersion); var minor = ALC.GetInteger(_device, AlcGetInteger.MinorVersion); XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL version : {major}.{minor}"); XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL hardware : {hardware}"); XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL extensions: {extensions}"); CheckError(); } else { XPlane.Trace.WriteLine($"[OpenAL Sample] I found someone else's context: {(ulong)oldContext.Handle:X8}"); } var path = Path.Combine(Path.GetDirectoryName(PluginInfo.ThisPlugin.FilePath), "sound.wav"); // Generate 1 source and load a buffer of audio. _soundSource = AL.GenSource(); CheckError(); _soundBuffer = WavHelper.LoadWav(path); XPlane.Trace.WriteLine($"[OpenAL Sample] Loaded {_soundBuffer} from {path}."); // Basic initialization code to play a sound: specify the buffer the source is playing, as well as some // sound parameters. This doesn't play the sound - it's just one-time initialization. AL.Source(_soundSource, ALSourcei.Buffer, _soundBuffer); AL.Source(_soundSource, ALSourcef.Pitch, 1f); AL.Source(_soundSource, ALSourcef.Gain, 1f); AL.Source(_soundSource, ALSourceb.Looping, false); AL.Source(_soundSource, ALSource3f.Position, 0f, 0f, 0f); AL.Source(_soundSource, ALSource3f.Velocity, 0f, 0f, 0f); CheckError(); return(0); }
static wavinfo_t GetWavInfo(string name, byte[] wav) { wavinfo_t info = new wavinfo_t(); if (wav == null) { return(info); } // debug //using (FileStream fs = new FileStream(Path.GetFileName(name), FileMode.Create, FileAccess.Write, FileShare.Read)) //{ // fs.Write(wav, 0, wav.Length); //} WavHelper helper = new WavHelper(wav); int offset = 0; // find "RIFF" chunk int riff = helper.FindChunk("RIFF", offset); if (riff == -1) { Con_Printf("Missing RIFF chunk\n"); return(info); } string wave = Encoding.ASCII.GetString(wav, offset + 8, 4); if (wave != "WAVE") { Con_Printf("RIFF chunk is not WAVE\n"); return(info); } // get "fmt " chunk offset += 12; //iff_data = data_p + 12; int fmt = helper.FindChunk("fmt ", offset); if (fmt == -1) { Con_Printf("Missing fmt chunk\n"); return(info); } int format = helper.GetLittleShort(fmt + 8); if (format != 1) { Con_Printf("Microsoft PCM format only\n"); return(info); } info.channels = helper.GetLittleShort(fmt + 10); info.rate = helper.GetLittleLong(fmt + 12); info.width = helper.GetLittleShort(fmt + 16 + 4 + 2) / 8; // get cue chunk int cue = helper.FindChunk("cue ", offset); if (cue != -1) { info.loopstart = helper.GetLittleLong(cue + 32); // if the next chunk is a LIST chunk, look for a cue length marker int list = helper.FindChunk("LIST", cue); if (list != -1) { string mark = Encoding.ASCII.GetString(wav, list + 28, 4); if (mark == "mark") { // this is not a proper parse, but it works with cooledit... int i = helper.GetLittleLong(list + 24); // samples in loop info.samples = info.loopstart + i; } } } else { info.loopstart = -1; } // find data chunk int data = helper.FindChunk("data", offset); if (data == -1) { Con_Printf("Missing data chunk\n"); return(info); } int samples = helper.GetLittleLong(data + 4) / info.width; if (info.samples > 0) { if (samples < info.samples) { Sys_Error("Sound {0} has a bad loop length", name); } } else { info.samples = samples; } info.dataofs = data + 8; return(info); }
private float InitSound(float elapsedSinceLastCall, float elapsedTimeSinceLastFlightLoop, int counter) { CheckError(); // We have to save the old context and restore it later, so that we don't interfere with X-Plane // and other plugins. var oldContext = ALC.GetCurrentContext(); // Try to create our own default device and context. If we fail, we're dead, we won't play any sound. _device = ALC.OpenDevice(null); if (_device == null) { XPlane.Trace.WriteLine("[OpenAL Sample] Could not open the default OpenAL device."); return(0); } _context = ALC.CreateContext(_device, new ALContextAttributes()); if (_context == null) { if (oldContext != default) { ALC.MakeContextCurrent(oldContext); } ALC.CloseDevice(_device); _device = default; XPlane.Trace.WriteLine("[OpenAL Sample] Could not create a context."); return(0); } // Make our context current, so that OpenAL commands affect our, um, stuff. ALC.MakeContextCurrent(_context); var path = Path.Combine(Path.GetDirectoryName(PluginInfo.ThisPlugin.FilePath), "sound.wav"); // Generate 1 source and load a buffer of audio. AL.GenSource(out _soundSource); CheckError(); _soundBuffer = WavHelper.LoadWav(path); XPlane.Trace.WriteLine($"[OpenAL Sample] Loaded {_soundBuffer} from {path}."); // Basic initialization code to play a sound: specify the buffer the source is playing, as well as some // sound parameters. This doesn't play the sound - it's just one-time initialization. AL.Source(_soundSource, ALSourcei.Buffer, _soundBuffer); AL.Source(_soundSource, ALSourcef.Pitch, 1f); AL.Source(_soundSource, ALSourcef.Gain, 1f); AL.Source(_soundSource, ALSourceb.Looping, false); AL.Source(_soundSource, ALSource3f.Position, 0f, 0f, 0f); AL.Source(_soundSource, ALSource3f.Velocity, 0f, 0f, 0f); CheckError(); // Finally: put back the old context _if_ we had one. If old_ctx was null, X-Plane isn't using OpenAL. if (oldContext != default) { ALC.MakeContextCurrent(oldContext); } return(0); }
static void Main(string[] args) { WavHelper.SplitWave(PathToWav, PathToJson, OutputFileName, WorkingDirectory); }
private unsafe float InitSound(float elapsedSinceLastCall, float elapsedTimeSinceLastFlightLoop, int counter) { CheckError(); var oldContext = _alc.GetCurrentContext(); if (oldContext == null) { XPlane.Trace.WriteLine("[OpenAL Sample] I found no OpenAL, I will be the first to init."); _device = _alc.OpenDevice(null); if (_device == null) { XPlane.Trace.WriteLine("[OpenAL Sample] Could not open the default OpenAL device."); return(0); } _context = _alc.CreateContext(_device, null); if (_context == null) { _alc.CloseDevice(_device); _device = null; XPlane.Trace.WriteLine("[OpenAL Sample] Could not open the default OpenAL device."); return(0); } _alc.MakeContextCurrent(_context); XPlane.Trace.WriteLine("[OpenAL Sample] Created the Open AL context."); var hardware = _alc.GetContextProperty(_device, GetContextString.DeviceSpecifier); var extensions = _alc.GetContextProperty(_device, GetContextString.Extensions); int major, minor; _alc.GetContextProperty(_device, GetContextInteger.MajorVersion, 1, &major); _alc.GetContextProperty(_device, GetContextInteger.MinorVersion, 1, &minor); XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL version : {major}.{minor}"); XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL hardware : {hardware}"); XPlane.Trace.WriteLine($"[OpenAL Sample] OpenAL extensions: {extensions}"); CheckError(); } else { XPlane.Trace.WriteLine($"[OpenAL Sample] I found someone else's context: {(ulong)oldContext:X8}"); } var path = Path.Combine(Path.GetDirectoryName(PluginInfo.ThisPlugin.FilePath), "sound.wav"); // Generate 1 source and load a buffer of audio. _soundSource = _al.GenSource(); CheckError(); _soundBuffer = WavHelper.LoadWav(path, _al); XPlane.Trace.WriteLine($"[OpenAL Sample] Loaded {_soundBuffer} from {path}."); // Basic initialization code to play a sound: specify the buffer the source is playing, as well as some // sound parameters. This doesn't play the sound - it's just one-time initialization. _al.SetSourceProperty(_soundSource, SourceInteger.Buffer, _soundBuffer); _al.SetSourceProperty(_soundSource, SourceFloat.Pitch, 1f); _al.SetSourceProperty(_soundSource, SourceFloat.Gain, 1f); _al.SetSourceProperty(_soundSource, SourceBoolean.Looping, false); _al.SetSourceProperty(_soundSource, SourceVector3.Position, Vector3.Zero); _al.SetSourceProperty(_soundSource, SourceVector3.Velocity, Vector3.Zero); CheckError(); return(0); }