protected override async Task <AudioClip> DownloadAudioClipAsync(Voice voice, CancellationToken token) { if (token.IsCancellationRequested) { return(null); } ; try { var url = $"https://texttospeech.googleapis.com/v1/text:synthesize?key={ApiKey}"; var ttsRequest = new GoogleTextToSpeechRequest( voice.Text, voice.GetTTSParam("language") as string ?? Language, voice.GetTTSParam("speakerName") as string ?? SpeakerName, voice.GetTTSParam("gender") as string ?? Gender, "LINEAR16"); var ttsResponse = await client.PostJsonAsync <GoogleTextToSpeechResponse>(url, ttsRequest, cancellationToken : token); if (!string.IsNullOrEmpty(ttsResponse.audioContent)) { var audioBin = Convert.FromBase64String(ttsResponse.audioContent); return(AudioConverter.PCMToAudioClip(audioBin)); } } catch (Exception ex) { Debug.LogError($"Error occured while processing text-to-speech voice: {ex.Message}\n{ex.StackTrace}"); } return(null); }
/// <summary> /// Construct a new voice loopback session. /// </summary> /// <param name="codec">An audio codec to encode and decode with.</param> /// <param name="quality">The encoding quality (usually the sample rate).</param> public VoiceLoopback(VoiceCodec codec, int quality) { var info = new CodecInfo(codec, quality); _waveIn = new WaveIn(this, info.DecodedFormat, info.DecodedBufferSize); _waveOut = new WaveOut(this, info.EncodedFormat, info.EncodedBufferSize); _encoder = new AudioConverter(info.DecodedBufferSize, info.DecodedFormat, info.EncodedFormat); }
public async Task SimonSay(CommandContext ctx, int @in = 4000, int @out = 16000, int inChan = 2, int outChan = 1, string force = null) { try { var voiceConnection = await ctx.EnsureVoiceConnection(); var audio = ctx.Services.GetService <IProvideAudioState>(); if (!audio.SpeechFromUser.ContainsKey(ctx.User.Id) || audio.SpeechFromUser[ctx.User.Id].IsEmpty) { throw new InvalidOperationException("You haven't said or preserved anything for me to say."); } var buff = audio.SpeechFromUser[ctx.User.Id].ToArray(); byte[] resampled = AudioConverter.Resample(buff, @in, @out, inChan, outChan); voiceConnection.SendSpeaking(); voiceConnection.GetTransmitStream().Write(resampled, 0, resampled.Length); voiceConnection.GetTransmitStream().Flush(); voiceConnection.SendSpeaking(false); } catch (Exception ex) { await ctx.RespondAsync($"Sorry, {ctx.User.Username}, I can't simonsay. \n\n``{ex.Message}``"); } }
// Sets the packet table containing information about the number of valid frames in a file and where they begin and end // for the file types that support this information. // Calling this function makes sure we write out the priming and remainder details to the destination file static void WritePacketTableInfo(AudioConverter converter, AudioFile destinationFile) { if (!destinationFile.IsPropertyWritable(AudioFileProperty.PacketTableInfo)) { return; } // retrieve the leadingFrames and trailingFrames information from the converter, AudioConverterPrimeInfo primeInfo = converter.PrimeInfo; // we have some priming information to write out to the destination file // The total number of packets in the file times the frames per packet (or counting each packet's // frames individually for a variable frames per packet format) minus mPrimingFrames, minus // mRemainderFrames, should equal mNumberValidFrames. AudioFilePacketTableInfo?pti_n = destinationFile.PacketTableInfo; if (pti_n == null) { return; } AudioFilePacketTableInfo pti = pti_n.Value; // there's priming to write out to the file // get the total number of frames from the output file long totalFrames = pti.ValidFrames + pti.PrimingFrames + pti.RemainderFrames; pti.PrimingFrames = primeInfo.LeadingFrames; pti.RemainderFrames = primeInfo.TrailingFrames; pti.ValidFrames = totalFrames - pti.PrimingFrames - pti.RemainderFrames; destinationFile.PacketTableInfo = pti; }
private static void CopyToByteBuffer(AudioAsset src, byte[] dst) { for (var i = 0; i < src.SoundData.Length; i++) { AudioConverter.SetSampleAsFloat(i, src.SoundData[i], dst, src.Format); } }
public void OnClientAudioReceived(AudioFrame frame) { this.audioFrameCounter += 1; int inputBufferLength = (int)(frame.sampleRate / 100 * frame.bitsPerSample / 8 * frame.channelCount); byte[] inputBuffer = new byte[inputBufferLength]; Marshal.Copy(frame.audioData, inputBuffer, 0, inputBufferLength); byte[] pcm16Bytes = AudioConverter.ResampleAudio(inputBuffer, (int)frame.sampleRate, (int)frame.bitsPerSample, (int)frame.channelCount, Config.AudioSettings.OUTGOING_SAMPLE_RATE); if (this.audioFrameCounter % this.maxAudioFramesToSend == 0) { try { byte[] stackedFrames = AudioConverter.MergeFrames(this.savedFrame, pcm16Bytes); IntPtr pcm16Pointer = Marshal.AllocHGlobal(stackedFrames.Length); Marshal.Copy(stackedFrames, 0, pcm16Pointer, stackedFrames.Length); var audioSendBuffer = new AudioSendBuffer(pcm16Pointer, (long)stackedFrames.Length, AudioFormat.Pcm16K); this.Call.GetLocalMediaSession().AudioSocket.Send(audioSendBuffer); } catch (Exception e) { Console.WriteLine(e.Message); } } else { this.savedFrame = pcm16Bytes; } }
// Some audio formats have a magic cookie associated with them which is required to decompress audio data // When converting audio, a magic cookie may be returned by the Audio Converter so that it may be stored along with // the output data -- This is done so that it may then be passed back to the Audio Converter at a later time as required static void WriteCookie(AudioConverter converter, AudioFile destinationFile) { var cookie = converter.CompressionMagicCookie; if (cookie != null && cookie.Length != 0) { destinationFile.MagicCookie = cookie; } }
private void InitAudio() { if (_waveIn != null) { _waveIn.Close(); } _waveIn = new WaveIn(this, _codec.DecodedFormat, _codec.DecodedBufferSize); _encoder = new AudioConverter(_codec.DecodedBufferSize, _codec.DecodedFormat, _codec.EncodedFormat); }
public JitterBuffer(CodecInfo codec) { _span = codec.SamplesPerPacket; _decoder = codec.GetDecoder(); _incoming = new ConcurrentQueue<VoicePacket>(); _buffer = new LinkedList<VoicePacket>(); this.Reset(); }
public void SpeechRecognition() { _log.Information("Testing speech recognition."); _log.Information("Press any key when ready to speak. Then press any key again to stop recording."); using var stream = new MemoryStream(); var waveIn = new WaveInEvent(); waveIn.WaveFormat = new WaveFormat(48000, 16, 1); waveIn.DataAvailable += (sender, args) => stream.Write(args.Buffer, 0, args.BytesRecorded); Console.ReadKey(); _log.Information("Recording. Press any key when ready."); waveIn.StartRecording(); Console.ReadKey(); waveIn.StopRecording(); waveIn.Dispose(); _log.Information("Recording stopped."); var data = stream.ToArray(); var speechKit = new SpeechKitApi(_fullAuthorizer, _configuration["FolderId"]); _log.Information("Testing with PCM format."); try { string text = speechKit.RecognizeTextAsync(data, AudioFormat.PCM).Result; _log.Information($"Recognized text: {text}"); } catch (Exception e) { _log.Error(e, "Recognition failed"); } _log.Information("Converting to OPUS"); byte[] oggData; try { oggData = new AudioConverter().ConvertPcmToOpusAsync(data, waveIn.WaveFormat).Result; } catch (Exception e) { _log.Error(e, "Conversion failed"); return; } try { string text = speechKit.RecognizeTextAsync(oggData, AudioFormat.OggOpus).Result; _log.Information($"Recognized text: {text}"); } catch (Exception e) { _log.Error(e, "Recognition failed"); } }
public JitterBuffer(CodecInfo codec) { _span = codec.SamplesPerPacket; _decoder = codec.GetDecoder(); _incoming = new ConcurrentQueue <VoicePacket>(); _buffer = new LinkedList <VoicePacket>(); this.Reset(); }
public AudioBookBuilderViewModel() { OuputFileItem = new OuputFileItem(); _audioConverter = new AudioConverter(); _audioConverter.OnConvert += _audioConverter_OnConvert; FileItems = new ObservableCollection <InputFileItem>(); ConvertCommand = new RelayCommand <ObservableCollection <InputFileItem> >(o => _audioConverter.Convert(o), o => !IsBusy && o?.Any() == true); AbortCommand = new RelayCommand <object>(o => _audioConverter.Abort(), o => IsBusy); }
public double[] ProcessAndExtract(Stream voiceSample) { var processed = AudioConverter.ConvertAudioToDoubleArray(voiceSample, sampleRate); processed = voiceDetector.RemoveSilence(processed, sampleRate); normalizer.Normalize(processed, sampleRate); processed = extractor.ExtractFeatures(processed); return(processed); }
static void Main(string[] args) { AudioConverter audioConverter = new AudioConverter(); VideoConverter imageConverter = new VideoConverter(); audioConverter.Convert("file1", "wav", "Mp3"); audioConverter.Convert("file2", "wav", "Ogg"); imageConverter.Convert("file3", "gif", "Jpg"); imageConverter.Convert("file4", "gif", "Png"); }
static void ReadCookie(AudioFile sourceFile, AudioConverter converter) { // grab the cookie from the source file and set it on the converter var cookie = sourceFile.MagicCookie; // if there is an error here, then the format doesn't have a cookie - this is perfectly fine as some formats do not if (cookie != null && cookie.Length != 0) { converter.DecompressionMagicCookie = cookie; } }
public Mixer() { BuildAUGraph(); _converter = AudioConverter.Create(MixerNode.GetAudioFormat(AudioUnitScopeType.Output), AudioStreamBasicDescription.CreateLinearPCM()); Metronome.Instance.TempoChanged += TempoChanged; _countOff = new PitchStream(StreamInfoProvider.GetDefault(), null); _countOff.IntervalLoop = new SampleIntervalLoop(_countOff, new double[] { 1 }); _countOff.AddFrequency("A4"); }
// Write output channel layout to destination file static void WriteDestinationChannelLayout(AudioConverter converter, AudioFile sourceFile, AudioFile destinationFile) { // if the Audio Converter doesn't have a layout see if the input file does var layout = converter.OutputChannelLayout ?? sourceFile.ChannelLayout; if (layout != null) { // TODO: throw new NotImplementedException(); //destinationFile.ChannelLayout = layout; } }
public override SoundEffectContent Process(AudioContent input, ContentProcessorContext context) { // Fallback if we aren't buiding for iOS. var platform = ContentHelper.GetMonoGamePlatform(); if (platform != MonoGamePlatform.iOS) { return(base.Process(input, context)); } var targetSampleRate = input.Format.SampleRate; // XNA SoundEffects have their sample rate changed based on the quality setting on the processor. //http://blogs.msdn.com/b/etayrien/archive/2008/09/22/audio-input-and-output-formats.aspx switch (this.Quality) { case ConversionQuality.Best: break; case ConversionQuality.Medium: targetSampleRate = (int)(targetSampleRate * 0.75f); break; case ConversionQuality.Low: targetSampleRate = (int)(targetSampleRate * 0.5f); break; } targetSampleRate = Math.Max(8000, targetSampleRate); var wavStream = new MemoryStream(); WaveFormat outputFormat = AudioConverter.ConvertFile(input.FileName, wavStream, AudioFileType.Wav, targetSampleRate, input.Format.BitsPerSample, input.Format.ChannelCount); var outputData = new ReadOnlyCollection <byte>(wavStream.ToArray()); wavStream.Close(); var waveFormatHeader = writeWavHeader(outputFormat); // SoundEffectContent is a sealed class, construct it using reflection var type = typeof(SoundEffectContent); ConstructorInfo c = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(ReadOnlyCollection <byte>), typeof(ReadOnlyCollection <byte>), typeof(int), typeof(int), typeof(int) }, null); var outputSoundEffectContent = (SoundEffectContent)c.Invoke(new Object[] { waveFormatHeader, outputData, input.LoopStart, input.LoopLength, (int)input.Duration.TotalMilliseconds }); return(outputSoundEffectContent); }
public async Task Transcribe(CommandContext ctx) { var audio = ctx.Services.GetService <IProvideAudioState>(); if (!audio.IsSpeechPreservedForUser[ctx.User.Id]) { return; } var buff = audio.SpeechFromUser[ctx.User.Id].ToArray(); var resampled = AudioConverter.Resample(buff, 4000, 16000, 2, 1); await ctx.RespondAsync("I think I heard you say: " + await new AzureSpeechModule(KarvisConfiguration, ctx.Client.DebugLogger).AudioToTextAsync(resampled)); }
private double[] ProvideAmplitudeValues(byte[] sample) { var voiceDetector = new AutocorrellatedVoiceActivityDetector(); File.WriteAllBytes(TEMP_FILE, sample); double[] result = null; using (var stream = File.OpenRead(TEMP_FILE)) { result = AudioConverter.ConvertAudioToDoubleArray(stream, _sampleRate); } File.Delete(TEMP_FILE); return(voiceDetector.RemoveSilence(result, _sampleRate)); }
protected override async Task <string> RecognizeSpeechAsync(AudioClip recordedVoice) { var headers = new Dictionary <string, string>() { { "Ocp-Apim-Subscription-Key", ApiKey } }; // TODO: Sending chunk is the better way // https://docs.microsoft.com/ja-jp/azure/cognitive-services/speech-service/rest-speech-to-text#chunked-transfer var response = await client.PostBytesAsync <SpeechRecognitionResponse>( $"https://{Region}.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language={Language}", AudioConverter.AudioClipToPCM(recordedVoice), headers); return(response?.DisplayText ?? string.Empty); }
public void Complete() { Stop(); var converterOutputPath = DownloadPathConfigurator.Configure(OutputDirectory, AudioConverter.TargetFileExtension, Track); if (AudioConverter != null) { AudioConverter.Convert(_DownloadFilePath, converterOutputPath); } if (AudioFileDescriptor != null) { AudioFileDescriptor.Write(Mp3MetaData, (AudioConverter != null) ? converterOutputPath : _DownloadFilePath); } Finish(); }
public static int GetSamplesAtByte(this AudioConverter converter, AudioFormat format, int startIdx, int frameCount, Span <byte> buffer) { int sampleCount = frameCount * format.Channels; var conversionBuffer = new Span <float>(new float[sampleCount]); int samplesGotten = converter.GetConvertedSamplesAt(format, startIdx, frameCount, conversionBuffer); if (samplesGotten == 0) { return(0); } for (var i = 0; i < samplesGotten; i++) { AudioConverter.SetSampleAsFloat(i, conversionBuffer[i], buffer, format); } return(samplesGotten); }
public void ConvertFormatChanges() { var pepsi = Engine.AssetLoader.Get <AudioAsset>("Sounds/pepsi.wav"); var format = new AudioFormat(32, true, 2, 48000); var streamer = new AudioConverter(pepsi.Format, pepsi.SoundData); // Higher to lower. var testData = new byte[format.SampleRate * format.FrameSize]; var spanData = new Span <byte>(testData); var samplesIdx = 0; samplesIdx += streamer.GetSamplesAtByte(format, samplesIdx, format.SampleRate, spanData); Assert.Equal(samplesIdx, 96000); format = new AudioFormat(8, true, 1, 12000); samplesIdx += streamer.GetSamplesAtByte(format, samplesIdx, format.SampleRate, spanData); samplesIdx += streamer.GetSamplesAtByte(format, samplesIdx, format.SampleRate, spanData); samplesIdx += streamer.GetSamplesAtByte(format, samplesIdx, format.SampleRate, spanData); Assert.Equal(samplesIdx, 108480); }
private async Task StopAudioStream() { if (AudioSource != null) { DoStopAudioStream(); await AudioSource.Stop(); AudioSource.Destroy(); AudioSource = null; } if (AudioDepacketizer != null) { AudioDepacketizer.Destroy(); AudioDepacketizer = null; } if (AudioDecoder != null) { AudioDecoder.Destroy(); AudioDecoder = null; } if (ResetAudioPipe != null) { ResetAudioPipe.Destroy(); ResetAudioPipe = null; } if (AudioConverter != null) { AudioConverter.Destroy(); AudioConverter = null; } if (AudioEncoder != null) { AudioEncoder.Destroy(); AudioEncoder = null; } if (AudioPacketizer != null) { AudioPacketizer.Destroy(); AudioPacketizer = null; } }
private void RecieveAudioMessage(AudioBody body) { byte[] bytes = SdkUtils.DownloadThumbnail(body.url, body.secret, conn.TokenData.access_token); string fileTempDir = SdkUtils.GetRevFileTempDir(); File.WriteAllBytes(fileTempDir + "\\" + body.filename, bytes); string wavFile = AudioConverter.ConvertToWave(fileTempDir, body.filename, Path.GetFileNameWithoutExtension(body.filename) + ".wav"); if (!File.Exists(wavFile)) { return; } chatBrower.AppendLeftHtml(toUserName, "收到音频"); //AppendAudioTag(null); PlayAudio(wavFile); }
protected override void CreateInternal(ReadOnlyMemory <byte> data) { // Check if WAV. if (WavFormat.IsWav(data)) { // Get the data. ReadOnlySpan <byte> soundDataDecoded = WavFormat.Decode(data, out AudioFormat format).Span; if (format.SampleSize == 0) { Engine.Log.Error($"Error reading sound file {Name}.", MessageSource.Audio); return; } // Convert to float and store it as such. SoundData = new float[soundDataDecoded.Length / format.SampleSize]; for (var i = 0; i < SoundData.Length; i++) { SoundData[i] = AudioConverter.GetSampleAsFloat(i, soundDataDecoded, format); } OriginalFormat = format.Copy(); format.BitsPerSample = 32; format.IsFloat = true; ByteSize = SoundData.Length; Format = format; Duration = format.GetSoundDuration(SoundData.Length * sizeof(float)); AudioConverter = new AudioConverter(Format, SoundData, 10); if (Format.UnsupportedBitsPerSample()) { Engine.Log.Error($"The audio format of {Name} has an unsupported number of bits per sample ({Format.BitsPerSample}). Supported values are 8/16/32", MessageSource.Audio); } } if (Format == null || SoundData == null) { Engine.Log.Warning($"Couldn't load audio file - {Name}.", MessageSource.AssetLoader); } }
public List <Match> FindAudioFilesContainingSpeaker(Stream speakerAudioFile, string toBeScreenedForAudioFilesWithSpeakerFolder) { var result = new List <Match>(); var speakerVoicePrint = VoicePrint.FromFeatures(featureExtractor.ProcessAndExtract(speakerAudioFile)); foreach (var file in Directory.GetFiles(toBeScreenedForAudioFilesWithSpeakerFolder, "*.wav", SearchOption.TopDirectoryOnly)) { using (var fs = new FileStream(file, FileMode.Open)) { double[][] words = voiceDetector.SplitBySilence(AudioConverter.ConvertAudioToDoubleArray(fs, sampleRate), sampleRate); int wordsWithinThreshold = 0; for (int i = 0; i < words.Length; i++) { var wordVoicePrint = VoicePrint.FromFeatures(words[i]); double wordDistance = wordVoicePrint.GetDistance(calculator, speakerVoicePrint); if (wordDistance < distanceThreshold) { wordsWithinThreshold++; } } if (words.Length > 0 && (100.0 * ((double)wordsWithinThreshold / words.Length)) > wordsPctThreshold) { var fVoicePrint = VoicePrint.FromFeatures(featureExtractor.ProcessAndExtract(fs)); double fDistance = fVoicePrint.GetDistance(calculator, speakerVoicePrint); if (fDistance < distanceThreshold) { result.Add(new Match(file, fDistance)); } } } } return(result); }
public async Task <byte[]> TextToAudioAsync(string text) { DebugLogger.LogMessage(LogLevel.Info, Constants.ApplicationName, $"AzureSpeechModule: Synthesizing speech for text [{text}]", DateTime.Now); // Creates a speech synthesizer using the default speaker as audio output. using (var synthesizer = BuildAzureSpeechSynthesizer()) using (var result = await synthesizer.SpeakTextAsync(text)) { if (SpeechWasSynthesized(result)) { DebugLogger.LogMessage(LogLevel.Info, Constants.ApplicationName, $"AzureSpeechModule: Speech synthesized for text [{text}]", DateTime.Now); return(AudioConverter.Resample(result.AudioData, 16000, 48000, 1, 2)); } DebugLogger.LogMessage(LogLevel.Error, Constants.ApplicationName, $"AzureSpeechModule: Speech synthesized failed for text [{text}]", DateTime.Now); return(new byte[0]); } }
private Task StopAudioStream() { if (AudioDepacketizer != null) { AudioDepacketizer.Destroy(); AudioDepacketizer = null; } if (AudioDecoder != null) { AudioDecoder.Destroy(); AudioDecoder = null; } if (AudioConverter != null) { AudioConverter.Destroy(); AudioConverter = null; } if (ResetAudioPipe != null) { ResetAudioPipe.Destroy(); ResetAudioPipe = null; } if (AudioEncoder != null) { AudioEncoder.Destroy(); AudioEncoder = null; } if (AudioPacketizer != null) { AudioPacketizer.Destroy(); AudioPacketizer = null; } if (AudioSink != null) { AudioSink.Destroy(); AudioSink = null; } return(Task.CompletedTask); }
private async Task OnUserSpeaking(UserSpeakingEventArgs args, DiscordChannel responseChannel) { if (args.User != null) { if (args.Speaking == false) { var audio = args.Client.GetCommandsNext().Services.GetService <IProvideAudioState>(); var buff = audio.SpeechFromUser[args.User.Id].ToArray(); byte[] resampled = AudioConverter.Resample(buff, 48000, 16000, 1, 1); var text = await new AzureSpeechModule(KarvisConfiguration, args.Client.DebugLogger).AudioToTextAsync(resampled); await args.Client.SendMessageAsync(responseChannel, args.User.Username + ", I think I heard you say: " + text); if (audio.IsSpeechPreservedForUser.ContainsKey(args.User.Id) && !args.Client.GetCommandsNext().Services.GetService <IProvideAudioState>().IsSpeechPreservedForUser[args.User.Id]) { audio.SpeechFromUser[args.User.Id] = new ConcurrentQueue <byte>(); } } } }
// Sets the packet table containing information about the number of valid frames in a file and where they begin and end // for the file types that support this information. // Calling this function makes sure we write out the priming and remainder details to the destination file static void WritePacketTableInfo (AudioConverter converter, AudioFile destinationFile) { if (!destinationFile.IsPropertyWritable (AudioFileProperty.PacketTableInfo)) return; // retrieve the leadingFrames and trailingFrames information from the converter, AudioConverterPrimeInfo primeInfo = converter.PrimeInfo; // we have some priming information to write out to the destination file // The total number of packets in the file times the frames per packet (or counting each packet's // frames individually for a variable frames per packet format) minus mPrimingFrames, minus // mRemainderFrames, should equal mNumberValidFrames. AudioFilePacketTableInfo? pti_n = destinationFile.PacketTableInfo; if (pti_n == null) return; AudioFilePacketTableInfo pti = pti_n.Value; // there's priming to write out to the file // get the total number of frames from the output file long totalFrames = pti.ValidFrames + pti.PrimingFrames + pti.RemainderFrames; Debug.WriteLine ("Total number of frames from output file: {0}", totalFrames); pti.PrimingFrames = primeInfo.LeadingFrames; pti.RemainderFrames = primeInfo.TrailingFrames; pti.ValidFrames = totalFrames - pti.PrimingFrames - pti.RemainderFrames; destinationFile.PacketTableInfo = pti; }
// Write output channel layout to destination file static void WriteDestinationChannelLayout (AudioConverter converter, AudioFile sourceFile, AudioFile destinationFile) { // if the Audio Converter doesn't have a layout see if the input file does var layout = converter.OutputChannelLayout ?? sourceFile.ChannelLayout; if (layout != null) { // TODO: throw new NotImplementedException (); //destinationFile.ChannelLayout = layout; } }
// Some audio formats have a magic cookie associated with them which is required to decompress audio data // When converting audio, a magic cookie may be returned by the Audio Converter so that it may be stored along with // the output data -- This is done so that it may then be passed back to the Audio Converter at a later time as required static void WriteCookie (AudioConverter converter, AudioFile destinationFile) { var cookie = converter.CompressionMagicCookie; if (cookie != null && cookie.Length != 0) { destinationFile.MagicCookie = cookie; } }
// Some audio formats have a magic cookie associated with them which is required to decompress audio data // When converting audio data you must check to see if the format of the data has a magic cookie // If the audio data format has a magic cookie associated with it, you must add this information to anAudio Converter // using AudioConverterSetProperty and kAudioConverterDecompressionMagicCookie to appropriately decompress the data // http://developer.apple.com/mac/library/qa/qa2001/qa1318.html static void ReadCookie (AudioFile sourceFile, AudioConverter converter) { // grab the cookie from the source file and set it on the converter var cookie = sourceFile.MagicCookie; // if there is an error here, then the format doesn't have a cookie - this is perfectly fine as some formats do not if (cookie != null && cookie.Length != 0) { converter.DecompressionMagicCookie = cookie; } }
public void DownloadAsync(MediaType mediaType) { if (ExecutionStatus == ExecutionStatus.Deleted) { Delete(); return; } // Now Download Async calls this // UpdateStatus(DownloadState.DownloadStart, 0.0); MediaType = mediaType; BaseFolder = KnownFolders.VideosLibrary; ProviderFolder = BaseFolder.GetFolder(Enum.GetName(typeof(ContentProviderType), YoutubeUrl.Provider)); VideoFolder = ProviderFolder.GetFolder(DownloadHelper.GetLegalPath(ChannelName)); if (MediaType == MediaType.Audio) { var audioFolder = KnownFolders.MusicLibrary; ProviderFolder = audioFolder.GetFolder(Enum.GetName(typeof(ContentProviderType), YoutubeUrl.Provider)); DownloadFolder = ProviderFolder.GetFolder(DownloadHelper.GetLegalPath(ChannelName)); } var videoInCache = false; if (!String.IsNullOrEmpty(Title)) { VideoExtension = ".mp4"; var videoFile1 = DownloadHelper.GetLegalPath(Title) + VideoExtension; var storageFile1 = VideoFolder.CreateFile(videoFile1); if (!CacheManager.Instance.NeedsDownload(YoutubeUrl.VideoId, storageFile1)) videoInCache = true; } if (!videoInCache) { var videoInfos = DownloadHelper.GetDownloadUrlsAsync(Uri); VideoInfo videoInfo = null; foreach (VideoInfo info in videoInfos) if (info.VideoType == VideoType.Mp4 && info.Resolution == 360) { videoInfo = info; break; } if (videoInfo == null) { UpdateStatus(DownloadState.Error); return; } Title = videoInfo.Title; VideoExtension = videoInfo.VideoExtension; var videoFile = DownloadHelper.GetLegalPath(Title) + VideoExtension; UpdateStatus(DownloadState.TitleChanged, Percentage); var storageFile = VideoFolder.CreateFile(videoFile); if (CacheManager.Instance.NeedsDownload(YoutubeUrl.VideoId, storageFile)) { CacheManager.Instance.SetFinished(YoutubeUrl.VideoId, storageFile.ToString(), false); DownloadHelper.DownloadToFileAsync(this, videoInfo.DownloadUri, storageFile, OnYoutubeLoading); CacheManager.Instance.SetFinished(YoutubeUrl.VideoId, storageFile.ToString(), true); if (OnEntryDownloadStatusChange != null) OnEntryDownloadStatusChange(this, DownloadState.UpdateCache, Percentage); } } DownloadState = DownloadState.DownloadFinish; //UpdateStatus(DownloadState, (MediaType == MediaType.Audio) ? 50 : 100); if (MediaType == MediaType.Audio) { Percentage = 50.0; var converter = new AudioConverter(this, OnAudioConversionStatusChange); converter.ConvertToMp3(); } else if (OnEntryDownloadStatusChange != null) UpdateStatus(DownloadState.Ready); }