public void CopyTo(JaggedShortArray destination, int sourceindex, int num, int index) { for (int i = sourceindex; i < sourceindex + num; i++) { this[i].CopyTo(destination[index + i - sourceindex], 0); } }
public void CopyTo(JaggedShortArray destination, int index) { for (int i = 0; i < Rank1; i++) { this[i].CopyTo(destination[index + i], 0); } }
public Decoder(string filename, AudioFormat format) : this(format) { RawkError error = RawkError.Unknown; int channels; int samplerate; long samples; switch (Format) { case AudioFormat.VorbisOgg: error = CreateVorbisDecoder(filename, out channels, out samplerate, out samples, out identifier); break; case AudioFormat.FmodSoundBank: error = CreateFsbDecoder(filename, out channels, out samplerate, out samples, out identifier); break; case AudioFormat.BinkAudio: case AudioFormat.BinkEncryptedRB2: error = CreateBinkDecoder(filename, out channels, out samplerate, out samples, out identifier); break; case AudioFormat.Vgs: error = CreateVgsDecoder(filename, out channels, out samplerate, out samples, out identifier); break; default: throw new ArgumentException(); } Channels = channels; SampleRate = samplerate; Samples = samples; ThrowRawkError(error); AudioBuffer = new JaggedShortArray(Channels, BufferSize); }
public void AddDecoder(int targetchannel, IDecoder decoder, int channel) { if (SampleRate == 0) { SampleRate = decoder.SampleRate; } else if (SampleRate != decoder.SampleRate) { throw new FormatException(); } if (AudioBuffer != null && decoder.AudioBuffer.Rank2 != AudioBuffer.Rank2) { throw new FormatException(); } Mapping.Add(new Pair <int, Pair <IDecoder, int> >(targetchannel, new Pair <IDecoder, int>(decoder, channel))); AudioBuffer = new JaggedShortArray(Channels, BufferSize); if (!Decoders.Contains(decoder)) { Decoders.Add(decoder); TemporaryAudioBuffer = new JaggedShortArray(Decoders.Sum(d => d.Channels), AudioBuffer.Rank2); } UpdateMasks(); }
public ZeroDecoder(int channels, int samplerate, long samples) { Channels = channels; SampleRate = samplerate; Samples = samples; Position = 0; AudioBuffer = new JaggedShortArray(Channels, RawkAudio.Decoder.BufferSize); }
public void Write(JaggedShortArray samples, int count) { if (disposed) { throw new ObjectDisposedException(string.Empty); } RawkError error = Compress(identifier, samples.Pointer, count); ThrowRawkError(error); }
public static void TranscodePreview(IList <int> previewtimes, List <AudioFormat.Mapping> maps, IDecoder decoder, Stream stream, ProgressIndicator progress) { RawkAudio.Encoder encoder; if (maps != null && maps.Count(m => m.Instrument == Instrument.Preview) > 0) { decoder.Seek(0); List <ushort> masks = new List <ushort>(); foreach (var m in maps) { if (m.Instrument == Instrument.Preview) { masks.Add((ushort)(1 << maps.IndexOf(m))); } } ushort[] mask = masks.ToArray(); encoder = new RawkAudio.Encoder(stream, mask.Length, decoder.SampleRate, 28000); long samples = decoder.Samples; progress.NewTask("Transcoding Preview", samples); JaggedShortArray buffer = new JaggedShortArray(encoder.Channels, decoder.AudioBuffer.Rank2); while (samples > 0) { int read = decoder.Read((int)Math.Min(samples, decoder.AudioBuffer.Rank2)); if (read <= 0) { break; } decoder.AudioBuffer.DownmixTo(buffer, mask, read); encoder.Write(buffer, read); samples -= read; progress.Progress(read); } progress.EndTask(); } else { long start = Math.Min(decoder.Samples, (long)previewtimes[0] * decoder.SampleRate / 1000); decoder.Seek(start); long duration = Math.Min(decoder.Samples - start, (long)(previewtimes[1] - previewtimes[0]) * decoder.SampleRate / 1000); encoder = new RawkAudio.Encoder(stream, 1, decoder.SampleRate); AudioFormat.Transcode(encoder, decoder, duration, progress); } encoder.Dispose(); }
public static long ProcessOffset(IDecoder decoder, IEncoder encoder, long offset) { offset = offset * encoder.SampleRate / 1000; if (offset > 0) { decoder.Seek(offset); } else if (offset < 0) // Silence { offset = -offset; JaggedShortArray buffer = new JaggedShortArray(encoder.Channels, decoder.AudioBuffer.Rank2); while (offset > 0) { int samples = (int)Math.Min(offset, buffer.Rank2); encoder.Write(buffer, samples); offset -= samples; } } return(offset); }
public override void EncodeAudio(AudioFormat audioformat, FormatData formatdata, ProgressIndicator progress) { progress.NewTask(20); Stream audio = formatdata.AddStream(this, AudioName); progress.SetNextWeight(1); List <AudioFormat.Mapping> oldmaps = audioformat.Mappings; ushort[] masks = PlatformRB2WiiCustomDLC.RemixAudioTracks(formatdata.Song, audioformat); progress.SetNextWeight(14); long samples = audioformat.Decoder.Samples; CryptedMoggStream mogg = new CryptedMoggStream(audio); //mogg.WriteHeader(samples); mogg.WriteHeader(); RawkAudio.Encoder encoder = new RawkAudio.Encoder(mogg, audioformat.Channels, audioformat.Decoder.SampleRate, 28000); progress.NewTask("Transcoding Audio", samples); JaggedShortArray buffer = new JaggedShortArray(encoder.Channels, audioformat.Decoder.AudioBuffer.Rank2); AudioFormat.ProcessOffset(audioformat.Decoder, encoder, audioformat.InitialOffset); while (samples > 0) { //int read = audioformat.Decoder.Read((int)Math.Min(samples, 0x4E20)); //int read = audioformat.Decoder.Read((int)Math.Min(samples, 0x20)); int read = audioformat.Decoder.Read((int)Math.Min(samples, buffer.Rank2)); if (read <= 0) { break; } audioformat.Decoder.AudioBuffer.DownmixTo(buffer, masks, read); encoder.Write(buffer, read); samples -= read; progress.Progress(read); //mogg.Update(read); } progress.EndTask(); progress.Progress(14); encoder.Dispose(); mogg.WriteEntries(); formatdata.CloseStream(audio); progress.SetNextWeight(6); Stream preview = formatdata.AddStream(this, PreviewName); mogg = new CryptedMoggStream(preview); mogg.WriteHeader(); PlatformRB2WiiCustomDLC.TranscodePreview(formatdata.Song.PreviewTimes, oldmaps, audioformat.Decoder != null ? audioformat.Decoder : new ZeroDecoder(1, 28000, 0x7FFFFFFFFFFFFFFF), mogg, progress); formatdata.CloseStream(preview); progress.EndTask(); progress.Progress(6); Stream formatstream = formatdata.AddStream(this, FormatName); audioformat.Save(formatstream); formatdata.CloseStream(formatstream); }
public override void SaveSong(PlatformData data, FormatData formatdata, ProgressIndicator progress) { string path = data.Session["path"] as string; SongData song = formatdata.Song; progress.NewTask(8); int i; string songpath = null; for (i = 0; i < 0x1000; i++) { songpath = Path.Combine(path, song.ID + (i == 0 ? "" : i.ToString())); if (!Directory.Exists(songpath)) { break; } } Directory.CreateDirectory(songpath); AudioFormat audio = (formatdata.GetFormat(FormatType.Audio) as IAudioFormat).DecodeAudio(formatdata, progress); progress.Progress(); ChartFormat chart = (formatdata.GetFormat(FormatType.Chart) as IChartFormat).DecodeChart(formatdata, progress); progress.Progress(); Stream chartstream = new FileStream(Path.Combine(songpath, "notes.mid"), FileMode.Create, FileAccess.Write); chart.Save(chartstream); chartstream.Close(); Ini ini = new Ini(); ini.SetValue("song", "name", song.Name); ini.SetValue("song", "artist", song.Artist); ini.SetValue("song", "album", song.Album); ini.SetValue("song", "genre", song.Genre); ini.SetValue("song", "year", song.Year.ToString()); ini.SetValue("song", "diff_band", ImportMap.GetBaseTier(Instrument.Ambient, song.Difficulty[Instrument.Ambient]).ToString()); ini.SetValue("song", "diff_guitar", ImportMap.GetBaseTier(Instrument.Guitar, song.Difficulty[Instrument.Guitar]).ToString()); ini.SetValue("song", "diff_bass", ImportMap.GetBaseTier(Instrument.Bass, song.Difficulty[Instrument.Bass]).ToString()); ini.SetValue("song", "diff_drums", ImportMap.GetBaseTier(Instrument.Drums, song.Difficulty[Instrument.Drums]).ToString()); Stream inistream = new FileStream(Path.Combine(songpath, "song.ini"), FileMode.Create, FileAccess.Write); ini.Save(inistream); inistream.Close(); if (song.AlbumArt != null) { Stream albumart = new FileStream(Path.Combine(songpath, "album.png"), FileMode.Create, FileAccess.Write); song.AlbumArt.Save(albumart, ImageFormat.Png); albumart.Close(); } JaggedShortArray encoderdata; var instruments = audio.Mappings.GroupBy(m => m.Instrument == Instrument.Vocals ? Instrument.Ambient : m.Instrument); encoderdata = new JaggedShortArray(2, RawkAudio.Decoder.BufferSize); int count = instruments.Count(); Stream[] streams = new Stream[count]; IEncoder[] encoders = new IEncoder[count]; ushort[][] masks = new ushort[count][]; i = 0; foreach (var item in instruments) { string filename = null; switch (item.Key) { case Instrument.Guitar: filename = "guitar.ogg"; break; case Instrument.Bass: filename = "rhythm.ogg"; break; case Instrument.Drums: filename = "drums.ogg"; break; case Instrument.Ambient: filename = "song.ogg"; break; case Instrument.Preview: filename = "preview.ogg"; break; } streams[i] = new FileStream(Path.Combine(songpath, filename), FileMode.Create, FileAccess.Write); masks[i] = new ushort[2]; foreach (var map in item) { int index = audio.Mappings.IndexOf(map); if (map.Balance <= 0) { masks[i][0] |= (ushort)(1 << index); } if (map.Balance >= 0) { masks[i][1] |= (ushort)(1 << index); } } encoders[i] = new RawkAudio.Encoder(streams[i], 2, audio.Decoder.SampleRate, 44100); i++; } if (audio.InitialOffset > 0) { AudioFormat.ProcessOffset(audio.Decoder, encoders[0], audio.InitialOffset); } else { for (i = 0; i < encoders.Length; i++) { AudioFormat.ProcessOffset(audio.Decoder, encoders[i], audio.InitialOffset); } } long samples = audio.Decoder.Samples; progress.NewTask("Transcoding Audio", samples, 6); while (samples > 0) { int read = audio.Decoder.Read(); if (read <= 0) { break; } // TODO: Apply volumes to each channel for (i = 0; i < count; i++) { audio.Decoder.AudioBuffer.DownmixTo(encoderdata, masks[i], read, false); encoders[i].Write(encoderdata, read); } samples -= read; progress.Progress(read); } for (i = 0; i < count; i++) { encoders[i].Dispose(); streams[i].Close(); } progress.EndTask(); progress.Progress(6); progress.EndTask(); }
public void DownmixTo(JaggedShortArray output, ushort[] masks, int samples, bool normalize = true) { RawkAudio.RawkError error = RawkAudio.Downmix(this.Pointer, Rank1, output.Pointer, Math.Min(masks.Length, output.Rank1), masks, samples, normalize ? 1 : 0); RawkAudio.ThrowRawkError(error); }
private Decoder(AudioFormat format) { disposed = false; Format = format; AudioBuffer = new JaggedShortArray(Channels, BufferSize); }
public FFmpegDecoder(Stream stream) { if (!Enabled) { try { FFmpeg.av_register_all(); Enabled = true; } catch (Exception ex) { throw ex; } } stream.Position = 0; Stream = new TemporaryStream(); Util.StreamCopy(Stream, stream, stream.Length); Stream.ClosePersist(); FFmpeg.av_open_input_file(out FormatPointer, Stream.Name, IntPtr.Zero, 0, IntPtr.Zero); if (FFmpeg.av_find_stream_info(FormatPointer) < 0) { throw new FormatException(); } Format = (FFmpeg.AVFormatContext)Marshal.PtrToStructure(FormatPointer, typeof(FFmpeg.AVFormatContext)); for (StreamIndex = 0; StreamIndex < Format.nb_streams; StreamIndex++) { if (Format.streams[StreamIndex] == IntPtr.Zero) { continue; } AVStream = (FFmpeg.AVStream)Marshal.PtrToStructure(Format.streams[StreamIndex], typeof(FFmpeg.AVStream)); Codec = (FFmpeg.AVCodecContext)Marshal.PtrToStructure(AVStream.codec, typeof(FFmpeg.AVCodecContext)); if (Codec.codec_type == FFmpeg.CodecType.CODEC_TYPE_AUDIO) { DecoderPointer = FFmpeg.avcodec_find_decoder(Codec.codec_id); break; } } if (DecoderPointer == IntPtr.Zero) { throw new FormatException(); } FFmpeg.avcodec_open(AVStream.codec, DecoderPointer); Channels = Codec.channels; SampleRate = Codec.sample_rate; if (AVStream.duration < 0) { Samples = long.MaxValue; } else { Samples = AVStream.time_base.num * AVStream.duration * SampleRate / AVStream.time_base.den; } PacketPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FFmpeg.AVPacket))); //FFmpegBufferSize = (FFmpeg.AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2; FFmpegBufferSize = BufferSize * Channels * 2; FFmpegBuffer = Marshal.AllocHGlobal(FFmpegBufferSize); AudioBuffer = new JaggedShortArray(Channels, BufferSize); CacheBuffer = new byte[FFmpegBufferSize]; Cache = new MemoryStream(); CacheOffset = 0; CacheLength = 0; }