// Called when new audio is passed to player for playback. // Initializes all asset dependent members excluding stream dependent members private void InitAudio(urakawa.media.data.audio.AudioMediaData audio) { this.audio = audio; WaveFormat format = new WaveFormat(); BufferDescription desc = new BufferDescription(); urakawa.media.data.audio.PCMFormatInfo info = audio.getPCMFormat(); this.frameSize = info.getBlockAlign(); this.channels = info.getNumberOfChannels(); this.sampleRate = (int)info.getSampleRate(); format.AverageBytesPerSecond = (int)info.getSampleRate() * info.getBlockAlign(); format.BitsPerSample = Convert.ToInt16(info.getBitDepth()); format.BlockAlign = Convert.ToInt16(info.getBlockAlign()); format.Channels = Convert.ToInt16(info.getNumberOfChannels()); format.FormatTag = WaveFormatTag.Pcm; format.SamplesPerSecond = (int)info.getSampleRate(); desc.Format = format; this.bufferSize = format.AverageBytesPerSecond; this.refreshLength = this.bufferSize / 2; desc.BufferBytes = this.bufferSize; desc.GlobalFocus = true; this.soundBuffer = new SecondaryBuffer(desc, this.device); this.pausePosition = 0; this.previousPosition = 0; }
/// <summary> /// Play an asset from beginning to end /// </summary> public void Play(urakawa.media.data.audio.AudioMediaData audio) { if (this.state == PlayerState.Stopped || this.state == PlayerState.Paused) { this.startPosition = 0; InitPlay(audio, 0, 0); } }
// Update the display when the node has new audio. private void SetAudio(urakawa.media.data.audio.AudioMediaData audio) { if (InvokeRequired) { Invoke(new SetAudioDelegate(SetAudio), audio); } else { Width = WidthForAudio(audio); this.waveformCanvas.Audio = audio; this.cursorBar.Invalidate(); if (audio != null && Parent != null && Parent.Parent is Track) { ((Track)Parent.Parent).UpdateSize(); } } }
/// <summary> /// Play an asset from a specified time position its to end /// </summary> public void Play(urakawa.media.data.audio.AudioMediaData audio, double from) { if (this.state == PlayerState.Stopped || this.state == PlayerState.Paused) { if (audio != null && audio.getAudioDuration().getTimeDeltaAsMillisecondFloat() > 0) { urakawa.media.data.audio.PCMFormatInfo info = audio.getPCMFormat(); int position = CalculationFunctions.AdaptToFrame( CalculationFunctions.ConvertTimeToByte(from, (int)info.getSampleRate(), info.getBlockAlign()), info.getBlockAlign()); if (position >= 0 && position <= audio.getPCMLength()) { this.startPosition = position; InitPlay(audio, position, 0); } else { throw new Exception("Start Position is out of bounds of Audio Asset"); } } } }
/// <summary> /// Create a new audio node by importing data from a file. /// </summary> public urakawa.core.TreeNode CreateAudioNode(string filename) { urakawa.Presentation p = getPresentation(0); urakawa.core.TreeNode node = p.getTreeNodeFactory().createNode(typeof(AudioNode).Name, DataModelFactory.NS); // Update the media data manager to accept this type of file if (!p.getMediaDataManager().getEnforceSinglePCMFormat()) { System.IO.FileStream audioStream = System.IO.File.OpenRead(filename); urakawa.media.data.audio.PCMDataInfo info = urakawa.media.data.audio.PCMDataInfo.parseRiffWaveHeader(audioStream); audioStream.Close(); p.getMediaDataManager().setDefaultPCMFormat(info); p.getMediaDataManager().setEnforceSinglePCMFormat(true); } // I'll go ahead and not comment this part urakawa.media.data.audio.AudioMediaData audio = p.getMediaDataFactory().createAudioMediaData(); audio.appendAudioDataFromRiffWave(filename); urakawa.media.data.audio.ManagedAudioMedia media = (urakawa.media.data.audio.ManagedAudioMedia)p.getMediaFactory().createAudioMedia(); media.setMediaData(audio); urakawa.property.channel.ChannelsProperty prop = p.getPropertyFactory().createChannelsProperty(); prop.setMedia(FindChannel("bobi.audio"), media); node.addProperty(prop); return(node); }
// Compute the necessary width for the amount of audio at the current audio scale. private int WidthForAudio(urakawa.media.data.audio.AudioMediaData audio) { return((int)Math.Round(this.zoom * (audio == null ? this.baseSize.Width : this.audioScale * audio.getAudioDuration().getTimeDeltaAsMillisecondFloat()))); }
// Convenience function to start playback of an asset // First initialise player with asset followed by starting playback using PlayAssetStream function private void InitPlay(urakawa.media.data.audio.AudioMediaData audio, int from, int to) { InitAudio(audio); PlayAssetStream(from, to); }