static uint Vorbis_read_func(IntPtr ptr, uint size, uint nmemb, IntPtr datasource) { GCHandle gcHandle = GCHandle.FromIntPtr(datasource); VirtualFileStream stream = (VirtualFileStream)gcHandle.Target; return((uint)stream.ReadUnmanaged(ptr, (int)size * (int)nmemb) / size); }
protected static SoundType GetSoundTypeByStream(VirtualFileStream stream) { byte[] buffer = new byte[4]; if (stream.Read(buffer, 0, 4) != 4) { return(SoundType.Unknown); } stream.Seek(-4, SeekOrigin.Current); if ((buffer[0] == 'o' || buffer[0] == 'O') && (buffer[1] == 'g' || buffer[1] == 'g') && (buffer[2] == 'g' || buffer[2] == 'g')) { return(SoundType.OGG); } if ((buffer[0] == 'r' || buffer[0] == 'R') && (buffer[1] == 'i' || buffer[1] == 'I') && (buffer[2] == 'f' || buffer[2] == 'F') && (buffer[3] == 'f' || buffer[3] == 'F')) { return(SoundType.WAV); } return(SoundType.Unknown); }
static int Vorbis_seek_func(IntPtr datasource, long offset, int whence) { GCHandle gcHandle = GCHandle.FromIntPtr(datasource); VirtualFileStream stream = (VirtualFileStream)gcHandle.Target; const int c_SEEK_CUR = 1; const int c_SEEK_END = 2; const int c_SEEK_SET = 0; SeekOrigin origin = SeekOrigin.Begin; switch (whence) { case c_SEEK_CUR: origin = SeekOrigin.Current; break; case c_SEEK_END: origin = SeekOrigin.End; break; case c_SEEK_SET: origin = SeekOrigin.Begin; break; } try { stream.Seek(offset, origin); } catch { return(1); } return(0); }
protected static SoundType GetSoundTypeByStream( VirtualFileStream stream ) { byte[] buffer = new byte[ 4 ]; if( stream.Read( buffer, 0, 4 ) != 4 ) return SoundType.Unknown; stream.Seek( -4, SeekOrigin.Current ); if( ( buffer[ 0 ] == 'o' || buffer[ 0 ] == 'O' ) && ( buffer[ 1 ] == 'g' || buffer[ 1 ] == 'g' ) && ( buffer[ 2 ] == 'g' || buffer[ 2 ] == 'g' ) ) { return SoundType.OGG; } if( ( buffer[ 0 ] == 'r' || buffer[ 0 ] == 'R' ) && ( buffer[ 1 ] == 'i' || buffer[ 1 ] == 'I' ) && ( buffer[ 2 ] == 'f' || buffer[ 2 ] == 'F' ) && ( buffer[ 3 ] == 'f' || buffer[ 3 ] == 'F' ) ) { return SoundType.WAV; } return SoundType.Unknown; }
public override Sound SoundCreate(VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, SoundMode mode) { criticalSection.Enter(); DirectSound sound; bool initialized; if ((int)(mode & SoundMode.Stream) == 0) { sound = new DirectSampleSound(stream, soundType, null, mode, out initialized); if (closeStreamAfterReading) { stream.Close(); } } else { sound = new DirectFileStreamSound(stream, closeStreamAfterReading, soundType, null, mode, out initialized); } if (!initialized) { sound.Dispose(); sound = null; } criticalSection.Leave(); return(sound); }
//static int Vorbis_close_func( IntPtr datasource ) //{ // return 0; //} static int Vorbis_tell_func(IntPtr datasource) { GCHandle gcHandle = GCHandle.FromIntPtr(datasource); VirtualFileStream stream = (VirtualFileStream)gcHandle.Target; return((int)stream.Position); }
public void Init(VirtualFileStream stream, VideoDriver videoDriver, AudioDriver audioDriver, bool sound3D) { this.stream = stream; this.audioDriver = audioDriver; this.videoDriver = videoDriver; this.sound3D = sound3D; if (videoDriver != null) { videoDriver.oggFile = this; } if (audioDriver != null) { audioDriver.oggFile = this; } oggSyncState = new ogg.SyncState(); oggPage = new ogg.Page(); ParsePrimaryHeaders(); ParseSecondaryHeaders(); if (this.audioDriver != null) { this.audioDriver.InitDSP(); } if (this.videoDriver != null) { this.videoDriver.InitTheora(); } //Update first frame Update(0); }
// public OpenALFileStreamSound(VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, string name, SoundMode mode, out bool initialized) { initialized = false; if (soundType == SoundType.Unknown) { if (name != null) { soundType = GetSoundTypeByName(name); } else { soundType = GetSoundTypeByStream(stream); } } if (soundType != SoundType.OGG) { Log.Warning(string.Format("Streaming is not supported for \"{0}\" files ({1}).", soundType, name)); return; } vorbisFile = new VorbisFile.File(); vorbisFileReader = new VorbisFileReader(stream, closeStreamAfterReading); if (!vorbisFileReader.OpenVorbisFile(vorbisFile)) { vorbisFileReader.Dispose(); Log.Warning(string.Format("Creating sound \"{0}\" failed.", name)); return; } long numSamples = vorbisFile.pcm_total(-1); vorbisFile.get_info(-1, out channels, out frequency); //convert to mono for 3D if ((mode & SoundMode.Mode3D) != 0 && channels == 2) { needConvertToMono = true; channels = 1; } if (!GenerateBuffers(2)) { Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\".", name); return; } double length = (double)numSamples / (double)frequency; Init(name, mode, (float)length, channels, frequency); initialized = true; }
public void DecryptAllVideos(string folderPath, Module module, string outputPath, bool hasTranscript) { try { // Get all clips of this module from database List <Clip> listClips = module.Clips; if (listClips.Count > 0) { // integer to add 1 if index should start at 1 int startAt1 = Convert.ToInt16(chkStartClipIndexAt1.Checked); foreach (Clip clip in listClips) { // Get current path of the encrypted video string currentPath = Path.Combine(folderPath, $"{clip.Name}.psv"); if (File.Exists(currentPath)) { // Create new path with output folder string newPath = Path.Combine(outputPath, $"{(startAt1 + clip.Index):00}. {clip.Title}.mp4"); // Init video and get it from iStream var playingFileStream = new VirtualFileStream(currentPath); playingFileStream.Clone(out IStream iStream); string fileName = Path.GetFileName(currentPath); bgwDecrypt.ReportProgress(1, new Log { Text = $"Start to Decrypt File \"{fileName}\"", TextColor = Color.Yellow, NewLine = false }); this.DecryptVideo(iStream, newPath); if (chkCreateSub.Checked && hasTranscript) { // Generate transcript file if user ask this.WriteTranscriptFile(clip, newPath); } bgwDecrypt.ReportProgress(1, new Log { Text = $"Decrypt File \"{Path.GetFileName(newPath)}\" success!", TextColor = Color.Green, NewLine = true }); playingFileStream.Dispose(); } else { bgwDecrypt.ReportProgress(1, new Log { Text = $"File \"{Path.GetFileName(currentPath)}\" cannot be found", TextColor = Color.Gray, NewLine = true, IsError = true }); } } } } catch (Exception ex) { logger.Error(ex); } }
public void DecryptAllVideos(string folderPath, Module module, string outputPath) { // Get all clips of this module from database List <Clip> listClips = module.Clips; if (listClips.Count > 0) { foreach (Clip clip in listClips) { // Get current path of the encrypted video string currPath = Path.Combine(folderPath, clip.ClipName + ".psv"); if (File.Exists(currPath)) { // Create new path with output folder string newPath = Path.Combine(outputPath, clip.ClipIndex + ". " + clip.ClipTitle + ".mp4"); // If length too long, rename it if (newPath.Length > 240) { newPath = Path.Combine(outputPath, clip.ClipIndex + ".mp4"); } // Init video and get it from istream IStream iStream; var playingFileStream = new VirtualFileStream(currPath); playingFileStream.Clone(out iStream); string fileName = Path.GetFileName(currPath); //this.AddText($"Start to Decrypt File \"{fileName}\"", Color.Yellow); bgwDecrypt.ReportProgress(1, new { Text = $"Start to Decrypt File \"{fileName}\"", Color = Color.Yellow, newLine = false }); this.DecryptVideo(iStream, newPath); if (chkCreateSub.Checked) { // Generate transcript file if user ask this.WriteTranscriptFile(clip, newPath); } //this.AddText($"Decryption File \"{Path.GetFileName(newPath)}\" successfully", Color.Green, true); bgwDecrypt.ReportProgress(1, new { Text = $"Decryption File \"{Path.GetFileName(newPath)}\" successfully", Color = Color.Green, newLine = true }); playingFileStream.Dispose(); } else { //this.AddText($"File \"{Path.GetFileName(currPath)}\" cannot be found ", Color.Gray, true); bgwDecrypt.ReportProgress(1, new { Text = $"File \"{Path.GetFileName(currPath)}\" cannot be found", Color = Color.Gray, newLine = true }); } } } }
public static void DecryptFile(string srcFile, string dstFile) { var stream = new VirtualFileStream(srcFile); var output = new FileStream(dstFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); output.SetLength(0); byte[] buffer = stream.ReadAll(); output.Write(buffer, 0, buffer.Length); output.Close(); }
public void Dispose() { streamGCHandle.Free(); if( needCloseStream && stream != null ) { stream.Close(); needCloseStream = false; } stream = null; }
public void Dispose() { streamGCHandle.Free(); if (needCloseStream && stream != null) { stream.Dispose(); needCloseStream = false; } stream = null; }
// public VorbisFileReader( VirtualFileStream stream, bool needCloseStream ) { this.stream = stream; this.needCloseStream = needCloseStream; streamGCHandle = GCHandle.Alloc( stream ); callbacks.read_func = Vorbis_read_func; callbacks.seek_func = Vorbis_seek_func; //callbacks.close_func = Vorbis_close_func; callbacks.tell_func = Vorbis_tell_func; }
// public VorbisFileReader(VirtualFileStream stream, bool needCloseStream) { this.stream = stream; this.needCloseStream = needCloseStream; streamGCHandle = GCHandle.Alloc(stream); callbacks.read_func = Vorbis_read_func; callbacks.seek_func = Vorbis_seek_func; //callbacks.close_func = Vorbis_close_func; callbacks.tell_func = Vorbis_tell_func; }
/// <summary> /// Decrypt all videos in current module folder. /// </summary> /// <param name="folderPath">Current module folder</param> /// <param name="moduleId">Module Id</param> /// <param name="outputPath">Destination of output video</param> public void DecryptAllVideos(string folderPath, Module moduleId, string outputPath) { // Get all clips of this module from database List <Clip> listClips = moduleId.Clips; if (listClips.Count > 0) { foreach (Clip clip in listClips) { // Get current path of the encrypted video string currPath = Path.Combine(folderPath, clip.ClipName + ".psv"); if (File.Exists(currPath)) { // Create new path with output folder var newPath = Path.Combine(outputPath, clip.ClipIndex.ToString("00") + ". " + clip.ClipTitle + ".mp4"); // If length too long, rename it if (newPath.Length > 240) { newPath = Path.Combine(outputPath, clip.ClipIndex + ".mp4"); } // Init video and get it from iStream var playingFileStream = new VirtualFileStream(currPath); playingFileStream.Clone(out var iStream); string fileName = Path.GetFileName(currPath); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Start to decrypt File \"{fileName}\""); Console.ForegroundColor = color_default; DecryptVideo(iStream, newPath); if (Options.CreateTranscript) { // Generate transcript file if user ask WriteTranscriptFile(clip, newPath); } WriteToConsole($"Decrypt File \"{Path.GetFileName(newPath)}\" success!", ConsoleColor.Green, true); playingFileStream.Dispose(); } else { WriteToConsole($"File \"{Path.GetFileName(currPath)}\" cannot be found.", ConsoleColor.Gray, true); } } } }
public static bool CreateVideo(string coursesFolder, string outputFolder, string courseTitle, Module module, Clip clip) { var encryptedVideoPath = $@"{ModuleFolder(coursesFolder, module)}/{clip.Name}.psv"; var encryptedFileInfo = new FileInfo(encryptedVideoPath); if (!encryptedFileInfo.Exists) { return(false); } using var fs = FileManager.CreateVideo(outputFolder, courseTitle, module.ModuleIndex, module.Title, clip.ClipIndex, clip.Title); var stream = new VirtualFileStream(encryptedVideoPath); stream.Read(fs); return(true); }
public override Sound SoundCreate(string name, SoundMode mode) { criticalSection.Enter(); DirectSound sound; sound = (DirectSound)base.SoundCreate(name, mode); if (sound != null) { criticalSection.Leave(); return(sound); } VirtualFileStream stream = CreateFileStream(name); if (stream == null) { criticalSection.Leave(); DirectSoundWorld.Warning(string.Format("Creating sound \"{0}\" failed.", name)); return(null); } bool initialized; if ((int)(mode & SoundMode.Stream) == 0) { sound = new DirectSampleSound(stream, SoundType.Unknown, name, mode, out initialized); stream.Close(); } else { sound = new DirectFileStreamSound(stream, true, SoundType.Unknown, name, mode, out initialized); } if (!initialized) { sound.Dispose(); sound = null; } criticalSection.Leave(); return(sound); }
protected override Sound _SoundCreate(string name, SoundModes mode) { criticalSection.Enter(); OpenALSound sound; sound = (OpenALSound)base._SoundCreate(name, mode); if (sound != null) { criticalSection.Leave(); return(sound); } VirtualFileStream stream = CreateFileStream(name); if (stream == null) { criticalSection.Leave(); Log.Warning(string.Format("Creating sound \"{0}\" failed.", name)); return(null); } bool initialized; if ((mode & SoundModes.Stream) == 0) { sound = new OpenALSampleSound(stream, SoundType.Unknown, name, mode, out initialized); stream.Dispose(); } else { sound = new OpenALFileStreamSound(stream, /*true, */ SoundType.Unknown, name, mode, out initialized); } if (!initialized) { sound.Dispose(); sound = null; } criticalSection.Leave(); return(sound); }
/// <summary> /// Decrypts the selected file. /// </summary> private async Task DecryptFileAsync(string srcFile, string destFile) { if (string.IsNullOrWhiteSpace(srcFile) || !File.Exists(srcFile)) { _loggingService.Log(LogLevel.Warning, $"Invalid source file {srcFile}, skipping..."); return; } using (var input = new VirtualFileStream(srcFile)) using (var output = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None, 128000, FileOptions.Asynchronous | FileOptions.SequentialScan)) { output.SetLength(0); var buffer = await input.ReadAsync().ConfigureAwait(false); await output.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false); _loggingService.Log(LogLevel.Information, $"Decrypted clip {Path.GetFileName(destFile)}."); } }
private void CreateVideoClip(Clip clip, Module module, DirectoryInfo moduleFolder, string courseTitle) { Utils.WriteText($"\t\t{clip.ClipIndex + 1:00} - {clip.Title}"); var file = moduleFolder.GetFiles($"{clip.Name}.psv").FirstOrDefault(); if (!file.Exists) { Utils.WriteRedText($"\t\t{clip.Title} does not exist"); } else { using var fs = DecryptFileHelper.CreateVideo(options.OutputPath, courseTitle, module, clip); var stream = new VirtualFileStream(file.FullName); stream.Read(fs); if (options.CreateTranscript) { WriteTranscriptFile(clip, module, courseTitle); } } }
// bool LoadSamplesFromStream( VirtualFileStream stream, SoundType soundType, out int channels, out int frequency, out float timeLength, out string error ) { channels = 0; frequency = 0; timeLength = 0; error = null; switch( soundType ) { case SoundType.OGG: { VorbisFileReader vorbisFileReader = new VorbisFileReader( stream, false ); VorbisFile.File vorbisFile = new VorbisFile.File(); if( !vorbisFileReader.OpenVorbisFile( vorbisFile ) ) { vorbisFile.Dispose(); vorbisFileReader.Dispose(); error = "Reading failed"; return false; } int numSamples = (int)vorbisFile.pcm_total( -1 ); vorbisFile.get_info( -1, out channels, out frequency ); timeLength = (float)vorbisFile.time_total( -1 ); int size = numSamples * channels; int sizeInBytes = size * 2; soundSamples = new byte[ sizeInBytes ]; unsafe { fixed( byte* pSoundSamples = soundSamples ) { int samplePos = 0; while( samplePos < sizeInBytes ) { int readBytes = vorbisFile.read( (IntPtr)( pSoundSamples + samplePos ), sizeInBytes - samplePos, 0, 2, 1, IntPtr.Zero ); if( readBytes <= 0 ) break; samplePos += readBytes; } } } vorbisFile.Dispose(); vorbisFileReader.Dispose(); } return true; case SoundType.WAV: { int sizeInBytes; if( !WavLoader.Load( stream, out channels, out frequency, out soundSamples, out sizeInBytes, out error ) ) { return false; } timeLength = (float)( soundSamples.Length / channels / 2 ) / (float)frequency; } return true; } error = "Unknown file type"; return false; }
unsafe public DirectSampleSound( VirtualFileStream stream, SoundType soundType, string name, SoundMode mode, out bool initialized ) { initialized = false; int channels; int frequency; float timeLength; if( soundType == SoundType.Unknown ) { if( name != null ) soundType = GetSoundTypeByName( name ); else soundType = GetSoundTypeByStream( stream ); } string error; if( !LoadSamplesFromStream( stream, soundType, out channels, out frequency, out timeLength, out error ) ) { if( name != null ) { DirectSoundWorld.Warning( string.Format( "Creating sound \"{0}\" failed ({1}).", name, error ) ); } else { DirectSoundWorld.Warning( string.Format( "Creating sound from stream failed ({0}).", error ) ); } return; } //convert to mono for 3D if( (int)( mode & SoundMode.Mode3D ) != 0 && channels == 2 ) { byte[] oldSamples = soundSamples; soundSamples = new byte[ oldSamples.Length / 2 ]; for( int n = 0; n < soundSamples.Length; n += 2 ) { soundSamples[ n + 0 ] = oldSamples[ n * 2 + 0 ]; soundSamples[ n + 1 ] = oldSamples[ n * 2 + 1 ]; } channels = 1; } //create buffer waveFormat = (WAVEFORMATEX*)NativeUtils.Alloc( NativeMemoryAllocationType.SoundAndVideo, sizeof( WAVEFORMATEX ) ); NativeUtils.ZeroMemory( (IntPtr)waveFormat, sizeof( WAVEFORMATEX ) ); waveFormat->wFormatTag = DSound.WAVE_FORMAT_PCM; waveFormat->nChannels = (ushort)channels; waveFormat->nSamplesPerSec = (uint)frequency; waveFormat->wBitsPerSample = 16; waveFormat->nBlockAlign = (ushort)( ( waveFormat->nChannels * waveFormat->wBitsPerSample ) / 8 ); waveFormat->nAvgBytesPerSec = waveFormat->nSamplesPerSec * waveFormat->nBlockAlign; Init( name, mode, timeLength, channels, frequency ); initialized = true; }
private void UpdateHibernateConfig() { var file = new VirtualFileInfo(string.Format(@"\Webroot\{0}\hibernate.xml", _context.Portal.PortalAlias)); if (_context.Package != null && _context.Package.Entities.Count > 0 && file.Exists) { XmlSerializer serializer = new XmlSerializer(typeof (HibernateConfiguration)); HibernateConfiguration config; using (Stream stream = new VirtualFileStream(file, VirtualFileMode.Open)) { config = (HibernateConfiguration) serializer.Deserialize(stream); } bool exists = CollectionUtils.Contains( config.MappingAssemblies, delegate(string item) { return (StringUtils.CaseInsensitiveEquals(item, _assemblyName)); }); if (!exists) { config.MappingAssemblies.Add(_assemblyName); using (Stream stream = new VirtualFileStream(file, VirtualFileMode.Create)) { serializer.Serialize(stream, config); } } FetchLinkedFile(_context.Portal.SupportFiles, @"SupportFiles\hibernate.xml"); } }
// unsafe public DirectFileStreamSound(VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, string name, SoundMode mode, out bool initialized) { initialized = false; if (soundType == SoundType.Unknown) { if (name != null) { soundType = GetSoundTypeByName(name); } else { soundType = GetSoundTypeByStream(stream); } } if (soundType != SoundType.OGG) { DirectSoundWorld.Warning(string.Format( "Streaming is not supported for \"{0}\" files ({1}).", soundType, name)); return; } vorbisFile = new VorbisFile.File(); vorbisFileReader = new VorbisFileReader(stream, closeStreamAfterReading); if (!vorbisFileReader.OpenVorbisFile(vorbisFile)) { vorbisFileReader.Dispose(); DirectSoundWorld.Warning(string.Format("Creating sound \"{0}\" failed.", name)); return; } int channels; int frequency; long numSamples = vorbisFile.pcm_total(-1); vorbisFile.get_info(-1, out channels, out frequency); //convert to mono for 3D if ((int)(mode & SoundMode.Mode3D) != 0 && channels == 2) { needConvertToMono = true; channels = 1; } waveFormat = (WAVEFORMATEX *)NativeUtils.Alloc(NativeMemoryAllocationType.SoundAndVideo, sizeof(WAVEFORMATEX)); NativeUtils.ZeroMemory((IntPtr)waveFormat, sizeof(WAVEFORMATEX)); waveFormat->wFormatTag = DSound.WAVE_FORMAT_PCM; waveFormat->nChannels = (ushort)channels; waveFormat->nSamplesPerSec = (uint)frequency; waveFormat->wBitsPerSample = 16; waveFormat->nBlockAlign = (ushort)((waveFormat->nChannels * waveFormat->wBitsPerSample) / 8); waveFormat->nAvgBytesPerSec = waveFormat->nSamplesPerSec * waveFormat->nBlockAlign; double length = (double)numSamples / (double)frequency; Init(name, mode, (float)length, channels, frequency); initialized = true; }
unsafe public DirectSampleSound(VirtualFileStream stream, SoundType soundType, string name, SoundMode mode, out bool initialized) { initialized = false; int channels; int frequency; float timeLength; if (soundType == SoundType.Unknown) { if (name != null) { soundType = GetSoundTypeByName(name); } else { soundType = GetSoundTypeByStream(stream); } } string error; if (!LoadSamplesFromStream(stream, soundType, out channels, out frequency, out timeLength, out error)) { if (name != null) { DirectSoundWorld.Warning(string.Format("Creating sound \"{0}\" failed ({1}).", name, error)); } else { DirectSoundWorld.Warning(string.Format("Creating sound from stream failed ({0}).", error)); } return; } //convert to mono for 3D if ((int)(mode & SoundMode.Mode3D) != 0 && channels == 2) { byte[] oldSamples = soundSamples; soundSamples = new byte[oldSamples.Length / 2]; for (int n = 0; n < soundSamples.Length; n += 2) { soundSamples[n + 0] = oldSamples[n * 2 + 0]; soundSamples[n + 1] = oldSamples[n * 2 + 1]; } channels = 1; } //create buffer waveFormat = (WAVEFORMATEX *)NativeUtils.Alloc(NativeMemoryAllocationType.SoundAndVideo, sizeof(WAVEFORMATEX)); NativeUtils.ZeroMemory((IntPtr)waveFormat, sizeof(WAVEFORMATEX)); waveFormat->wFormatTag = DSound.WAVE_FORMAT_PCM; waveFormat->nChannels = (ushort)channels; waveFormat->nSamplesPerSec = (uint)frequency; waveFormat->wBitsPerSample = 16; waveFormat->nBlockAlign = (ushort)((waveFormat->nChannels * waveFormat->wBitsPerSample) / 8); waveFormat->nAvgBytesPerSec = waveFormat->nSamplesPerSec * waveFormat->nBlockAlign; Init(name, mode, timeLength, channels, frequency); initialized = true; }
// bool LoadSamplesFromStream(VirtualFileStream stream, SoundType soundType, out int channels, out int frequency, out float timeLength, out string error) { channels = 0; frequency = 0; timeLength = 0; error = null; switch (soundType) { case SoundType.OGG: { VorbisFileReader vorbisFileReader = new VorbisFileReader(stream, false); VorbisFile.File vorbisFile = new VorbisFile.File(); if (!vorbisFileReader.OpenVorbisFile(vorbisFile)) { vorbisFile.Dispose(); vorbisFileReader.Dispose(); error = "Reading failed"; return(false); } int numSamples = (int)vorbisFile.pcm_total(-1); vorbisFile.get_info(-1, out channels, out frequency); timeLength = (float)vorbisFile.time_total(-1); int size = numSamples * channels; int sizeInBytes = size * 2; soundSamples = new byte[sizeInBytes]; unsafe { fixed(byte *pSoundSamples = soundSamples) { int samplePos = 0; while (samplePos < sizeInBytes) { int readBytes = vorbisFile.read((IntPtr)(pSoundSamples + samplePos), sizeInBytes - samplePos, 0, 2, 1, IntPtr.Zero); if (readBytes <= 0) { break; } samplePos += readBytes; } } } vorbisFile.Dispose(); vorbisFileReader.Dispose(); } return(true); case SoundType.WAV: { int sizeInBytes; if (!WavLoader.Load(stream, out channels, out frequency, out soundSamples, out sizeInBytes, out error)) { return(false); } timeLength = (float)(soundSamples.Length / channels / 2) / (float)frequency; } return(true); } error = "Unknown file type"; return(false); }
// unsafe public DirectFileStreamSound( VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, string name, SoundMode mode, out bool initialized ) { initialized = false; if( soundType == SoundType.Unknown ) { if( name != null ) soundType = GetSoundTypeByName( name ); else soundType = GetSoundTypeByStream( stream ); } if( soundType != SoundType.OGG ) { DirectSoundWorld.Warning( string.Format( "Streaming is not supported for \"{0}\" files ({1}).", soundType, name ) ); return; } vorbisFile = new VorbisFile.File(); vorbisFileReader = new VorbisFileReader( stream, closeStreamAfterReading ); if( !vorbisFileReader.OpenVorbisFile( vorbisFile ) ) { vorbisFileReader.Dispose(); DirectSoundWorld.Warning( string.Format( "Creating sound \"{0}\" failed.", name ) ); return; } int channels; int frequency; long numSamples = vorbisFile.pcm_total( -1 ); vorbisFile.get_info( -1, out channels, out frequency ); //convert to mono for 3D if( (int)( mode & SoundMode.Mode3D ) != 0 && channels == 2 ) { needConvertToMono = true; channels = 1; } waveFormat = (WAVEFORMATEX*)NativeUtils.Alloc( NativeMemoryAllocationType.SoundAndVideo, sizeof( WAVEFORMATEX ) ); NativeUtils.ZeroMemory( (IntPtr)waveFormat, sizeof( WAVEFORMATEX ) ); waveFormat->wFormatTag = DSound.WAVE_FORMAT_PCM; waveFormat->nChannels = (ushort)channels; waveFormat->nSamplesPerSec = (uint)frequency; waveFormat->wBitsPerSample = 16; waveFormat->nBlockAlign = (ushort)( ( waveFormat->nChannels * waveFormat->wBitsPerSample ) / 8 ); waveFormat->nAvgBytesPerSec = waveFormat->nSamplesPerSec * waveFormat->nBlockAlign; double length = (double)numSamples / (double)frequency; Init( name, mode, (float)length, channels, frequency ); initialized = true; }
public void DecryptAllVideos(string folderPath, int moduleId, string outputPath, bool isCreateTranscript, bool ignoreException = true) { var clipsRepository = databaseSQLiteConnection.Connect().GetClipsRepository(); var listClips = clipsRepository.GetClipsFromDb(moduleId); databaseSQLiteConnection.Disconnect(); if (listClips.Count > 0) { foreach (Clip clip in listClips) { try { var currPath = Path.Combine(folderPath, clip.ClipName + ".psv"); if (File.Exists(currPath)) { var newPath = Path.Combine(outputPath, clip.ClipIndex + ". " + clip.ClipTitle.CleanPath(this.InvalidPathCharacters) + ".mp4"); // If length too long, rename it if (newPath.Length > 240) { newPath = Path.Combine(outputPath, clip.ClipIndex + ".mp4"); } //newPath = newPath.EscapeIllegalCharacters(); playingFileStream = new VirtualFileStream(currPath); playingFileStream.Clone(out iStream); var fileName = Path.GetFileName(currPath); Logger.Info($"Start to Decrypt File {fileName}"); Semaphore.Wait(); TaskList.Add(Task.Run(() => { DecryptVideo(iStream, newPath); if (isCreateTranscript) { WriteTranscriptFile(clip.ClipId, newPath); } lock (SemaphoreLock) { Semaphore.Release(); } })); Logger.Info($"Decryption File { Path.GetFileName(newPath) } successfully"); } else { if (!ignoreException) { throw new Exception($"File { Path.GetFileName(currPath) } cannot be found."); } Logger.ErrorFormat($"File {Path.GetFileName(currPath)} cannot be found."); } } catch (Exception ex) { if (!ignoreException) { throw new Exception($"Cannot decrypt clip. {ex}"); } } } } }
unsafe public OpenALSampleSound( VirtualFileStream stream, SoundType soundType, string name, SoundMode mode, out bool initialized ) { initialized = false; byte[] samples; int sizeInBytes; float timeLength; if( string.Compare( Path.GetExtension( name ), ".ogg", true ) == 0 ) { //ogg VorbisFileReader vorbisFileReader = new VorbisFileReader( stream, false ); VorbisFile.File vorbisFile = new VorbisFile.File(); if( !vorbisFileReader.OpenVorbisFile( vorbisFile ) ) { vorbisFile.Dispose(); vorbisFileReader.Dispose(); Log.Warning( "OpenALSoundSystem: Creating sound failed \"{0}\" (Reading failed).", name ); return; } int numSamples = (int)vorbisFile.pcm_total( -1 ); vorbisFile.get_info( -1, out channels, out frequency ); timeLength = (float)vorbisFile.time_total( -1 ); sizeInBytes = numSamples * channels * 2; samples = new byte[ sizeInBytes ]; fixed( byte* pSamples = samples ) { int samplePos = 0; while( samplePos < sizeInBytes ) { int readBytes = vorbisFile.read( (IntPtr)( pSamples + samplePos ), sizeInBytes - samplePos, 0, 2, 1, IntPtr.Zero ); if( readBytes <= 0 ) break; samplePos += readBytes; } } vorbisFile.Dispose(); vorbisFileReader.Dispose(); } else if( string.Compare( Path.GetExtension( name ), ".wav", true ) == 0 ) { //wav string error; if( !WavLoader.Load( stream, out channels, out frequency, out samples, out sizeInBytes, out error ) ) { Log.Warning( "OpenALSoundSystem: Creating sound failed \"{0}\" ({1}).", name, error ); return; } timeLength = (float)( samples.Length / channels / 2 ) / (float)frequency; } else { Log.Warning( "OpenALSoundSystem: Creating sound failed \"{0}\" (Unknown file type).", name ); return; } //create buffer Al.alGenBuffers( 1, out alBuffer ); int alFormat = ( channels == 1 ) ? Al.AL_FORMAT_MONO16 : Al.AL_FORMAT_STEREO16; //bug fix: half volume mono 2D sounds //convert to stereo if( ( mode & SoundMode.Mode3D ) == 0 && alFormat == Al.AL_FORMAT_MONO16 ) { byte[] stereoSamples = new byte[ sizeInBytes * 2 ]; for( int n = 0; n < sizeInBytes; n += 2 ) { stereoSamples[ n * 2 + 0 ] = samples[ n ]; stereoSamples[ n * 2 + 1 ] = samples[ n + 1 ]; stereoSamples[ n * 2 + 2 ] = samples[ n ]; stereoSamples[ n * 2 + 3 ] = samples[ n + 1 ]; } samples = stereoSamples; alFormat = Al.AL_FORMAT_STEREO16; sizeInBytes *= 2; } //convert to mono for 3D if( ( mode & SoundMode.Mode3D ) != 0 && channels == 2 ) { byte[] oldSamples = samples; samples = new byte[ oldSamples.Length / 2 ]; for( int n = 0; n < samples.Length; n += 2 ) { samples[ n + 0 ] = oldSamples[ n * 2 + 0 ]; samples[ n + 1 ] = oldSamples[ n * 2 + 1 ]; } alFormat = Al.AL_FORMAT_MONO16; sizeInBytes /= 2; } fixed( byte* pSamples = samples ) { Al.alBufferData( alBuffer, alFormat, pSamples, sizeInBytes, frequency ); } if( OpenALSoundWorld.CheckError() ) { Log.Warning( "OpenALSoundSystem: Creating sound failed \"{0}\".", name ); return; } Init( name, mode, timeLength, channels, frequency ); initialized = true; }
public override Sound SoundCreate( VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, SoundMode mode ) { criticalSection.Enter(); DirectSound sound; bool initialized; if( (int)( mode & SoundMode.Stream ) == 0 ) { sound = new DirectSampleSound( stream, soundType, null, mode, out initialized ); if( closeStreamAfterReading ) stream.Close(); } else { sound = new DirectFileStreamSound( stream, closeStreamAfterReading, soundType, null, mode, out initialized ); } if( !initialized ) { sound.Dispose(); sound = null; } criticalSection.Leave(); return sound; }
// public OpenALFileStreamSound( VirtualFileStream stream, bool closeStreamAfterReading, SoundType soundType, string name, SoundMode mode, out bool initialized ) { initialized = false; if( soundType == SoundType.Unknown ) { if( name != null ) soundType = GetSoundTypeByName( name ); else soundType = GetSoundTypeByStream( stream ); } if( soundType != SoundType.OGG ) { Log.Warning( string.Format( "Streaming is not supported for \"{0}\" files ({1}).", soundType, name ) ); return; } vorbisFile = new VorbisFile.File(); vorbisFileReader = new VorbisFileReader( stream, closeStreamAfterReading ); if( !vorbisFileReader.OpenVorbisFile( vorbisFile ) ) { vorbisFileReader.Dispose(); Log.Warning( string.Format( "Creating sound \"{0}\" failed.", name ) ); return; } long numSamples = vorbisFile.pcm_total( -1 ); vorbisFile.get_info( -1, out channels, out frequency ); //convert to mono for 3D if( ( mode & SoundMode.Mode3D ) != 0 && channels == 2 ) { needConvertToMono = true; channels = 1; } if( !GenerateBuffers( 2 ) ) { Log.Warning( "OpenALSoundSystem: Creating sound failed \"{0}\".", name ); return; } double length = (double)numSamples / (double)frequency; Init( name, mode, (float)length, channels, frequency ); initialized = true; }
private PortalApplication GetPortal(out bool isNew) { string alias = StringUtils.ReplaceIllegalChars(_settings.PortalName); PortalApplication portal = PortalApplication.Get(alias); isNew = (portal == null); if (isNew) { alias = alias.Replace(" ", string.Empty).Replace("!", "_"); portal = BuildTransientItem<PortalApplication>(); portal.PortalAlias = alias; portal.PortalTitle = _settings.PortalName; portal.Validate(); portal.Save(); _hierarchyNodeService.InsertPortalNode(portal); using (Stream stream = new VirtualFileStream(string.Format(@"\Webroot\{0}\hibernate.xml", alias), VirtualFileMode.Create)) { new XmlSerializer(typeof (HibernateConfiguration)).Serialize(stream, new HibernateConfiguration()); } } return portal; }
public void Dispose() { try { if (criticalSection != null) { criticalSection.Enter(); } Pause = true; if (videoDriver != null) { videoDriver.Dispose(); videoDriver = null; } if (audioDriver != null) { audioDriver.Dispose(); audioDriver = null; } if (oggSyncState != null) { oggSyncState.Dispose(); oggSyncState = null; } if (oggPage != null) { oggPage.Dispose(); oggPage = null; } if (stream != null) { stream.Dispose(); stream = null; } } catch (Exception ex) { Log.Error("OggFile: Exception: " + ex.ToString()); } finally { if (criticalSection != null) { criticalSection.Leave(); } } if (criticalSection != null) { criticalSection.Dispose(); criticalSection = null; } GC.SuppressFinalize(this); }
public void DecryptAllVideos(string folderPath, Module module, string outputPath, bool hasTranscript) { try { // Get all clips of this module from database List <Clip> listClips = module.Clips; if (listClips.Count > 0) { foreach (Clip clip in listClips) { // Get current path of the encrypted video string currentPath = Path.Combine(folderPath, $"{clip.Name}.psv"); if (File.Exists(currentPath)) { // Create new path with output folder string newPath = Path.Combine(outputPath, $"{clip.Index + 1:00}. {clip.Title}.mp4"); // Init video and get it from iStream var playingFileStream = new VirtualFileStream(currentPath); playingFileStream.Clone(out IStream iStream); string fileName = Path.GetFileName(currentPath); bgwDecrypt.ReportProgress(1, new Log { Text = $"Decrypting \"{fileName}\"...", TextColor = Color.Yellow }); DecryptVideo(iStream, newPath); if (createSubCheckBox.Checked && hasTranscript) { // Generate transcript file if user ask WriteTranscriptFile(clip, newPath); } bgwDecrypt.ReportProgress(1, new Log { Text = $"\"{Path.GetFileName(newPath)}\" decrypt success.\n", TextColor = Color.Green }); playingFileStream.Dispose(); } else { bgwDecrypt.ReportProgress(1, new Log { Text = $"File \"{Path.GetFileName(currentPath)}\" cannot be found\n", TextColor = Color.Gray, }); } } } } catch (Exception ex) { Logger.Error(ex); } }
/// <summary> /// Decrypt all videos in current module folder. /// </summary> /// <param name="folderPath">Current module folder</param> /// <param name="moduleId">Module Id</param> /// <param name="outputPath">Destination of output video</param> public void DecryptAllVideos(string folderPath, Module module, string outputPath) { // Get all clips of this module from database List <Clip> listClips = module.Clips; if (listClips.Count > 0) { foreach (Clip clip in listClips) { // Get current path of the encrypted video string currPath = Path.Combine(folderPath, clip.ClipName + ".psv"); if (File.Exists(currPath)) { // Create new path with output folder string newPath = Path.Combine(outputPath, clip.ClipIndex + ". " + clip.ClipTitle + ".mp4"); // If length too long, rename it if (newPath.Length > 240) { newPath = Path.Combine(outputPath, clip.ClipIndex + ".mp4"); } // Init video and get it from istream IStream iStream; var playingFileStream = new VirtualFileStream(currPath); playingFileStream.Clone(out iStream); string fileName = Path.GetFileName(currPath); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Start to Decrypt File \"{0}\"", fileName); Console.ForegroundColor = color_default; DecryptVideo(iStream, newPath); if (Options.CreateTranscript) { // Generate transcript file if user ask WriteTranscriptFile(clip, newPath); } //Semaphore.Wait(); //TaskList.Add(Task.Run(() => //{ // // Write the decrypted video from istream to new file mp4 // DecryptVideo(iStream, newPath); // if (Options.CreateTranscript) // { // // Generate transcript file if user ask // WriteTranscriptFile(clip, newPath); // } // lock (SemaphoreLock) // { // Semaphore.Release(); // } //})); WriteToConsole($"Decryption File \"{Path.GetFileName(newPath)}\" successfully ", ConsoleColor.Green, true); playingFileStream.Dispose(); } else { WriteToConsole($"File \"{Path.GetFileName(currPath)}\" cannot be found ", ConsoleColor.Gray, true); } } } }
unsafe public OpenALSampleSound(VirtualFileStream stream, SoundType soundType, string name, SoundModes mode, out bool initialized) { initialized = false; byte[] samples; int sizeInBytes; float timeLength; if (string.Compare(Path.GetExtension(name), ".ogg", true) == 0) { //ogg VorbisFileReader vorbisFileReader = new VorbisFileReader(stream, false); VorbisFile.File vorbisFile = new VorbisFile.File(); if (!vorbisFileReader.OpenVorbisFile(vorbisFile)) { vorbisFile.Dispose(); vorbisFileReader.Dispose(); Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" (Reading failed).", name); return; } int numSamples = (int)vorbisFile.pcm_total(-1); vorbisFile.get_info(-1, out channels, out frequency); timeLength = (float)vorbisFile.time_total(-1); sizeInBytes = numSamples * channels * 2; samples = new byte[sizeInBytes]; fixed(byte *pSamples = samples) { int samplePos = 0; while (samplePos < sizeInBytes) { int readBytes = vorbisFile.read((IntPtr)(pSamples + samplePos), sizeInBytes - samplePos, 0, 2, 1, IntPtr.Zero); if (readBytes <= 0) { break; } samplePos += readBytes; } } vorbisFile.Dispose(); vorbisFileReader.Dispose(); } else if (string.Compare(Path.GetExtension(name), ".wav", true) == 0) { //wav string error; if (!WavLoader.Load(stream, out channels, out frequency, out samples, out sizeInBytes, out error)) { Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" ({1}).", name, error); return; } timeLength = (float)(samples.Length / channels / 2) / (float)frequency; } else { Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\" (Unknown file type).", name); return; } //create buffer Al.alGenBuffers(1, out alBuffer); int alFormat = (channels == 1) ? Al.AL_FORMAT_MONO16 : Al.AL_FORMAT_STEREO16; //bug fix: half volume mono 2D sounds //convert to stereo if ((mode & SoundModes.Mode3D) == 0 && alFormat == Al.AL_FORMAT_MONO16) { byte[] stereoSamples = new byte[sizeInBytes * 2]; for (int n = 0; n < sizeInBytes; n += 2) { stereoSamples[n * 2 + 0] = samples[n]; stereoSamples[n * 2 + 1] = samples[n + 1]; stereoSamples[n * 2 + 2] = samples[n]; stereoSamples[n * 2 + 3] = samples[n + 1]; } samples = stereoSamples; alFormat = Al.AL_FORMAT_STEREO16; sizeInBytes *= 2; } //convert to mono for 3D if ((mode & SoundModes.Mode3D) != 0 && channels == 2) { byte[] oldSamples = samples; samples = new byte[oldSamples.Length / 2]; for (int n = 0; n < samples.Length; n += 2) { samples[n + 0] = oldSamples[n * 2 + 0]; samples[n + 1] = oldSamples[n * 2 + 1]; } alFormat = Al.AL_FORMAT_MONO16; sizeInBytes /= 2; } fixed(byte *pSamples = samples) Al.alBufferData(alBuffer, alFormat, pSamples, sizeInBytes, frequency); if (OpenALSoundWorld.CheckError()) { Log.Warning("OpenALSoundSystem: Creating sound failed \"{0}\".", name); return; } Init(name, mode, timeLength, channels, frequency); initialized = true; }