Minor refact

This commit is contained in:
niksedk 2023-01-02 20:11:42 +01:00
parent a36f82b7e7
commit f6af948b52
2 changed files with 58 additions and 90 deletions

View File

@ -96,7 +96,7 @@ namespace Nikse.SubtitleEdit.Core.Common
DataChunkSize = BitConverter.ToUInt32(buffer, 4);
DataStartPosition = ConstantHeaderSize + FmtChunkSize + 8;
// if some other ChunckId than 'data' (e.g. LIST) we search for 'data'
// if some other ChunkId than 'data' (e.g. LIST) we search for 'data'
long oldPos = ConstantHeaderSize + FmtChunkSize;
while (DataId != "data" && oldPos + DataChunkSize + 16 < stream.Length)
{
@ -112,47 +112,24 @@ namespace Nikse.SubtitleEdit.Core.Common
BlockAlign = BytesPerSample * NumberOfChannels;
}
public int BytesPerSample
{
get
{
// round up to the next byte (20 bit WAVs are like 24 bit WAVs with the 4 least significant bits unused)
return (BitsPerSample + 7) / 8;
}
}
public int BytesPerSample =>
// round up to the next byte (20 bit WAVs are like 24 bit WAVs with the 4 least significant bits unused)
(BitsPerSample + 7) / 8;
public long BytesPerSecond
{
get
{
return (long)SampleRate * BlockAlign;
}
}
public long BytesPerSecond => (long)SampleRate * BlockAlign;
public double LengthInSeconds
{
get
{
return (double)DataChunkSize / BytesPerSecond;
}
}
public double LengthInSeconds => (double)DataChunkSize / BytesPerSecond;
public long LengthInSamples
{
get
{
return DataChunkSize / BlockAlign;
}
}
public long LengthInSamples => DataChunkSize / BlockAlign;
internal static void WriteHeader(Stream toStream, int sampleRate, int numberOfChannels, int bitsPerSample, int sampleCount)
{
const int headerSize = 44;
int bytesPerSample = (bitsPerSample + 7) / 8;
int blockAlign = numberOfChannels * bytesPerSample;
int byteRate = sampleRate * blockAlign;
int dataSize = sampleCount * bytesPerSample * numberOfChannels;
byte[] header = new byte[headerSize];
var bytesPerSample = (bitsPerSample + 7) / 8;
var blockAlign = numberOfChannels * bytesPerSample;
var byteRate = sampleRate * blockAlign;
var dataSize = sampleCount * bytesPerSample * numberOfChannels;
var header = new byte[headerSize];
WriteStringToByteArray(header, 0, "RIFF");
WriteInt32ToByteArray(header, 4, headerSize + dataSize - 8); // size of RIFF chunk's data
WriteStringToByteArray(header, 8, "WAVE");
@ -171,19 +148,19 @@ namespace Nikse.SubtitleEdit.Core.Common
private static void WriteInt16ToByteArray(byte[] headerData, int index, int value)
{
byte[] buffer = BitConverter.GetBytes((short)value);
var buffer = BitConverter.GetBytes((short)value);
Buffer.BlockCopy(buffer, 0, headerData, index, buffer.Length);
}
private static void WriteInt32ToByteArray(byte[] headerData, int index, int value)
{
byte[] buffer = BitConverter.GetBytes(value);
var buffer = BitConverter.GetBytes(value);
Buffer.BlockCopy(buffer, 0, headerData, index, buffer.Length);
}
private static void WriteStringToByteArray(byte[] headerData, int index, string value)
{
byte[] buffer = Encoding.ASCII.GetBytes(value);
var buffer = Encoding.ASCII.GetBytes(value);
Buffer.BlockCopy(buffer, 0, headerData, index, buffer.Length);
}
}
@ -199,10 +176,7 @@ namespace Nikse.SubtitleEdit.Core.Common
Min = min;
}
public int Abs
{
get { return Math.Max(Math.Abs((int)Max), Math.Abs((int)Min)); }
}
public int Abs => Math.Max(Math.Abs((int)Max), Math.Abs((int)Min));
}
public class WavePeakData
@ -228,7 +202,7 @@ namespace Nikse.SubtitleEdit.Core.Common
HighestPeak = 0;
foreach (var peak in Peaks)
{
int abs = peak.Abs;
var abs = peak.Abs;
if (abs > HighestPeak)
{
HighestPeak = abs;
@ -268,7 +242,7 @@ namespace Nikse.SubtitleEdit.Core.Common
private SpectrogramData(string loadFromDirectory)
{
_loadFromDirectory = loadFromDirectory;
Images = new Bitmap[0];
Images = Array.Empty<Bitmap>();
}
public int FftSize { get; private set; }
@ -279,10 +253,7 @@ namespace Nikse.SubtitleEdit.Core.Common
public IList<Bitmap> Images { get; private set; }
public bool IsLoaded
{
get { return _loadFromDirectory == null; }
}
public bool IsLoaded => _loadFromDirectory == null;
public void Load()
{
@ -291,12 +262,12 @@ namespace Nikse.SubtitleEdit.Core.Common
return;
}
string directory = _loadFromDirectory;
var directory = _loadFromDirectory;
_loadFromDirectory = null;
try
{
string xmlInfoFileName = Path.Combine(directory, "Info.xml");
var xmlInfoFileName = Path.Combine(directory, "Info.xml");
if (!File.Exists(xmlInfoFileName))
{
return;
@ -325,6 +296,7 @@ namespace Nikse.SubtitleEdit.Core.Common
}
catch
{
// ignore
}
}
@ -338,9 +310,10 @@ namespace Nikse.SubtitleEdit.Core.Common
}
catch
{
// ignore
}
}
Images = new Bitmap[0];
Images = Array.Empty<Bitmap>();
}
public static SpectrogramData FromDisk(string spectrogramDirectory)
@ -371,7 +344,7 @@ namespace Nikse.SubtitleEdit.Core.Common
var hash = MovieHasher.GenerateHash(videoFileName);
string wavePeakName;
if (trackNumber > 0)
if (trackNumber > 0 || Directory.GetFiles(dir, "-*.wav").Length == 0)
{
wavePeakName = $"{hash}-{trackNumber}.wav";
}
@ -452,19 +425,19 @@ namespace Nikse.SubtitleEdit.Core.Common
peaksPerSecond++;
}
int delaySampleCount = (int)(_header.SampleRate * (delayInMilliseconds / TimeCode.BaseUnit));
var delaySampleCount = (int)(_header.SampleRate * (delayInMilliseconds / TimeCode.BaseUnit));
// ignore negative delays for now (pretty sure it can't happen in mkv and some places pass in -1 by mistake)
delaySampleCount = Math.Max(delaySampleCount, 0);
var peaks = new List<WavePeak>();
var readSampleDataValue = GetSampleDataReader();
float sampleAndChannelScale = (float)GetSampleAndChannelScale();
long fileSampleCount = _header.LengthInSamples;
long fileSampleOffset = -delaySampleCount;
var sampleAndChannelScale = (float)GetSampleAndChannelScale();
var fileSampleCount = _header.LengthInSamples;
var fileSampleOffset = -delaySampleCount;
int chunkSampleCount = _header.SampleRate / peaksPerSecond;
byte[] data = new byte[chunkSampleCount * _header.BlockAlign];
float[] chunkSamples = new float[chunkSampleCount * 2];
var data = new byte[chunkSampleCount * _header.BlockAlign];
var chunkSamples = new float[chunkSampleCount * 2];
_stream.Seek(_header.DataStartPosition, SeekOrigin.Begin);
@ -477,7 +450,7 @@ namespace Nikse.SubtitleEdit.Core.Common
while (fileSampleOffset < fileSampleCount)
{
// calculate how many samples to skip at the beginning (for positive delays)
int startSkipSampleCount = 0;
var startSkipSampleCount = 0;
if (fileSampleOffset < 0)
{
startSkipSampleCount = (int)Math.Min(-fileSampleOffset, chunkSampleCount);
@ -485,23 +458,23 @@ namespace Nikse.SubtitleEdit.Core.Common
}
// calculate how many samples to read from the file
long fileSamplesRemaining = fileSampleCount - Math.Max(fileSampleOffset, 0);
int fileReadSampleCount = (int)Math.Min(fileSamplesRemaining, chunkSampleCount - startSkipSampleCount);
var fileSamplesRemaining = fileSampleCount - Math.Max(fileSampleOffset, 0);
var fileReadSampleCount = (int)Math.Min(fileSamplesRemaining, chunkSampleCount - startSkipSampleCount);
// read samples from the file
if (fileReadSampleCount > 0)
{
int fileReadByteCount = fileReadSampleCount * _header.BlockAlign;
var fileReadByteCount = fileReadSampleCount * _header.BlockAlign;
_stream.Read(data, 0, fileReadByteCount);
fileSampleOffset += fileReadSampleCount;
int chunkSampleOffset = 0;
int dataByteOffset = 0;
var chunkSampleOffset = 0;
var dataByteOffset = 0;
while (dataByteOffset < fileReadByteCount)
{
float valuePositive = 0F;
float valueNegative = -0F;
for (int iChannel = 0; iChannel < _header.NumberOfChannels; iChannel++)
var valuePositive = 0F;
var valueNegative = -0F;
for (var iChannel = 0; iChannel < _header.NumberOfChannels; iChannel++)
{
var v = readSampleDataValue(data, ref dataByteOffset);
if (v < 0)
@ -551,13 +524,13 @@ namespace Nikse.SubtitleEdit.Core.Common
public static WavePeakData GenerateEmptyPeaks(string peakFileName, int totalSeconds)
{
int peaksPerSecond = Configuration.Settings.VideoControls.WaveformMinimumSampleRate;
var peaksPerSecond = Configuration.Settings.VideoControls.WaveformMinimumSampleRate;
var peaks = new List<WavePeak>
{
new WavePeak(1000, -1000)
};
var totalPeaks = peaksPerSecond * totalSeconds;
for (int i = 0; i < totalPeaks; i++)
for (var i = 0; i < totalPeaks; i++)
{
peaks.Add(new WavePeak(1, -1));
}
@ -567,7 +540,7 @@ namespace Nikse.SubtitleEdit.Core.Common
using (var stream = File.Create(peakFileName))
{
WaveHeader.WriteHeader(stream, peaksPerSecond, 2, 16, peaks.Count);
byte[] buffer = new byte[4];
var buffer = new byte[4];
foreach (var peak in peaks)
{
WriteValue16Bit(buffer, 0, peak.Max);
@ -619,31 +592,31 @@ namespace Nikse.SubtitleEdit.Core.Common
}
// load data
byte[] data = new byte[_header.DataChunkSize];
var data = new byte[_header.DataChunkSize];
_stream.Position = _header.DataStartPosition;
_stream.Read(data, 0, data.Length);
// read peak values
WavePeak[] peaks = new WavePeak[_header.LengthInSamples];
int peakIndex = 0;
var peakIndex = 0;
if (_header.NumberOfChannels == 2)
{
// max value in left channel, min value in right channel
int byteIndex = 0;
while (byteIndex < data.Length)
{
short max = (short)ReadValue16Bit(data, ref byteIndex);
short min = (short)ReadValue16Bit(data, ref byteIndex);
var max = (short)ReadValue16Bit(data, ref byteIndex);
var min = (short)ReadValue16Bit(data, ref byteIndex);
peaks[peakIndex++] = new WavePeak(max, min);
}
}
else
{
// single sample value (for backwards compatibility)
int byteIndex = 0;
var byteIndex = 0;
while (byteIndex < data.Length)
{
short value = (short)ReadValue16Bit(data, ref byteIndex);
var value = (short)ReadValue16Bit(data, ref byteIndex);
if (value == short.MinValue)
{
value = -short.MaxValue;
@ -659,23 +632,21 @@ namespace Nikse.SubtitleEdit.Core.Common
private static int ReadValue8Bit(byte[] data, ref int index)
{
int result = sbyte.MinValue + data[index];
index += 1;
var result = sbyte.MinValue + data[index];
index ++;
return result;
}
private static int ReadValue16Bit(byte[] data, ref int index)
{
int result = (short)
((data[index]) |
(data[index + 1] << 8));
var result = data[index] | (data[index + 1] << 8);
index += 2;
return result;
}
private static int ReadValue24Bit(byte[] data, ref int index)
{
int result =
var result =
((data[index] << 8) |
(data[index + 1] << 16) |
(data[index + 2] << 24)) >> 8;
@ -685,7 +656,7 @@ namespace Nikse.SubtitleEdit.Core.Common
private static int ReadValue32Bit(byte[] data, ref int index)
{
int result =
var result =
(data[index]) |
(data[index + 1] << 8) |
(data[index + 2] << 16) |
@ -722,7 +693,7 @@ namespace Nikse.SubtitleEdit.Core.Common
private double GetSampleScale()
{
return (1.0 / Math.Pow(2.0, _header.BytesPerSample * 8 - 1));
return 1.0 / Math.Pow(2.0, _header.BytesPerSample * 8 - 1);
}
private double GetSampleAndChannelScale()
@ -771,10 +742,7 @@ namespace Nikse.SubtitleEdit.Core.Common
public void Close()
{
if (_stream != null)
{
_stream.Close();
}
_stream?.Close();
}
//////////////////////////////////////// SPECTRUM ///////////////////////////////////////////////////////////

View File

@ -28297,14 +28297,14 @@ namespace Nikse.SubtitleEdit.Forms
if (mediaPlayer.VideoPlayer is LibVlcDynamic libVlc)
{
var item = sender as ToolStripItem;
int number = int.Parse(item.Tag.ToString());
var number = int.Parse(item.Tag.ToString());
libVlc.AudioTrackNumber = number;
VideoAudioTrackNumber = number;
}
else if (mediaPlayer.VideoPlayer is LibMpvDynamic libMpv)
{
var item = sender as ToolStripItem;
int number = int.Parse(item.Tag.ToString());
var number = int.Parse(item.Tag.ToString());
number--;
libMpv.AudioTrackNumber = number;
VideoAudioTrackNumber = number;