Fixed massive memory leak caused by recreating data buffers.

Code cleanup.
This commit is contained in:
Kjell Kernen
2014-03-21 11:32:52 +01:00
parent ababec79f9
commit 054ff2dccf
3 changed files with 55 additions and 67 deletions
+9 -10
View File
@@ -1,13 +1,15 @@
<Window x:Class="OpenTXrecorder.MainWindow"
<Window x:Class="OpenTXrecorder.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="OpenTX Recorder" Title="OpenTX Recorder"
Height="340" Height="440"
MinWidth="840" MinWidth="840"
MaxWidth="840" MaxWidth="840"
ResizeMode="CanResizeWithGrip" SizeToContent="Width" Icon="/OpenTXrecorder;component/icon.ico" MinHeight="340"> ResizeMode="CanResizeWithGrip" SizeToContent="Width" Icon="/OpenTXrecorder;component/icon.ico" MinHeight="450">
<Window.Resources>
<Window.Resources>
<DataTemplate x:Key="image"> <DataTemplate x:Key="image">
<Image x:Name="TheImage" /> <Image x:Name="TheImage" />
<DataTemplate.Triggers> <DataTemplate.Triggers>
@@ -23,16 +25,13 @@
<ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch"> <Grid Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch">
<ListView Name="lvSentences" <ListView Name="lvSentences"
SelectionMode="Single" SelectionMode="Single"
MouseDoubleClick="sentenceRowDblClk" MouseDoubleClick="play"
VerticalAlignment="Stretch" > VerticalAlignment="Stretch" >
<ListView.View> <ListView.View>
<GridView> <GridView>
<GridView.Columns> <GridView.Columns>
@@ -98,9 +97,9 @@
</Button> </Button>
</StackPanel> </StackPanel>
<Separator Height="5" /> <Separator Height="5" />
<Label Content="RECORDING" Name="lblRecording" HorizontalAlignment="Center" Foreground="Red" FontSize="24" FontWeight="Black" Visibility="Hidden" /> <Label Content="RECORDING" Name="lblRecording" HorizontalAlignment="Center" Foreground="Red" FontSize="24" FontWeight="Black" Visibility="Hidden" />
</StackPanel> </StackPanel>
<Image Grid.Row="0" Grid.Column="1" Name="openTXLogo" Height="48" MouseLeftButtonDown="about" HorizontalAlignment="Right" Cursor="Hand" Margin="0,0,0,0" Source="/OpenTXrecorder;component/recorder_logo.png" Stretch="Uniform" VerticalAlignment="Bottom" /> <Image Grid.Row="0" Grid.Column="1" Name="openTXLogo" Height="48" MouseLeftButtonDown="about" HorizontalAlignment="Right" Cursor="Hand" Margin="0,0,0,0" Source="/OpenTXrecorder;component/recorder_logo.png" Stretch="Uniform" VerticalAlignment="Bottom" />
</Grid> </Grid>
+44 -55
View File
@@ -41,8 +41,13 @@ namespace OpenTXrecorder
WavFileWriter filewriter; WavFileWriter filewriter;
WaveInRecorder recorder; WaveInRecorder recorder;
byte[] tmparray; byte[] recordingBuffer;
bool isRecording = false; static int recSamplerate = 16000; // 8000, 16000 or 32000
static int recBits = 16; // 8 or 16
static int recChannels = 1; // 1 or 2
static int recBuffersize = 1024 * 2;
public bool isRecording { get; set; }
public MainWindow() public MainWindow()
{ {
@@ -51,6 +56,7 @@ namespace OpenTXrecorder
Thread.Sleep(1500); Thread.Sleep(1500);
InitializeComponent(); InitializeComponent();
recordingBuffer = new byte[recBuffersize];
lvSentences.ItemsSource = sentences; lvSentences.ItemsSource = sentences;
cbLanguages.ItemsSource = languages; cbLanguages.ItemsSource = languages;
@@ -75,8 +81,8 @@ namespace OpenTXrecorder
try try
{ {
system_strings = System.IO.File.ReadAllLines(env.systemSounds()); system_strings = System.IO.File.ReadAllLines(env.systemSounds);
other_strings = System.IO.File.ReadAllLines(env.otherSounds()); other_strings = System.IO.File.ReadAllLines(env.otherSounds);
} }
catch (IOException) catch (IOException)
{ {
@@ -86,27 +92,27 @@ namespace OpenTXrecorder
sentences.Clear(); sentences.Clear();
foreach (string str in system_strings) foreach (string str in system_strings)
sentences.Add(str, env.sysDir()); sentences.Add(str, env.sysDir);
sentences.Add(@";^ System Sounds ^;", ""); sentences.Add(@";^ System Sounds ^;", "");
foreach (string str in other_strings) foreach (string str in other_strings)
sentences.Add(str, env.baseDir()); sentences.Add(str, env.baseDir);
lvSentences.Items.Refresh(); // Workaround - Two way binding is better lvSentences.Items.Refresh(); // Workaround - Two way binding is better
} }
private void saveLanguage() private void saveLanguage()
{ {
System.IO.Directory.CreateDirectory(env.sysDir()); System.IO.Directory.CreateDirectory(env.sysDir);
StreamWriter sw = File.CreateText(env.systemSounds()); StreamWriter sw = File.CreateText(env.systemSounds);
int i; int i;
for (i = 0; sentences[i].fileName != ""; i++) for (i = 0; sentences[i].fileName != ""; i++)
{ {
sw.WriteLine(sentences[i].toRaw()); sw.WriteLine(sentences[i].toRaw());
} }
sw.Close(); sw.Close();
sw = File.CreateText(env.otherSounds()); sw = File.CreateText(env.otherSounds);
for (i++; i < sentences.Count; i++) for (i++; i < sentences.Count; i++)
{ {
sw.WriteLine(sentences[i].toRaw()); sw.WriteLine(sentences[i].toRaw());
@@ -120,14 +126,29 @@ namespace OpenTXrecorder
loadLanguage(); loadLanguage();
} }
private void about(object sender, MouseButtonEventArgs e) private void about(object sender, MouseButtonEventArgs e)
{ {
AboutWindow aboutWindow = new AboutWindow(); AboutWindow aboutWindow = new AboutWindow();
aboutWindow.ShowDialog(); aboutWindow.ShowDialog();
} }
private void sentenceRowDblClk(object sender, MouseButtonEventArgs e) private void addSentence(object sender, RoutedEventArgs e)
{
int i = 0;
string newFile;
do
{
newFile = "new_file_" + i.ToString();
i++;
}
while (env.fileExists(newFile));
sentences.Add(new Sentence(newFile + ";New Description;New Voice Message", env.baseDir));
this.lvSentences.Items.Refresh();
this.lvSentences.SelectedIndex = this.lvSentences.Items.Count - 1;
this.lvSentences.ScrollIntoView(this.lvSentences.SelectedItem);
}
private void play(object sender, EventArgs e)
{ {
if (this.lvSentences.SelectedItems.Count < 1) if (this.lvSentences.SelectedItems.Count < 1)
return; return;
@@ -147,27 +168,6 @@ namespace OpenTXrecorder
} }
} }
private void play(object sender, RoutedEventArgs e)
{
sentenceRowDblClk(null, null);
}
private void addSentence(object sender, RoutedEventArgs e)
{
int i = 0;
string newFile;
do
{
newFile = "new_file_" + i.ToString();
i++;
}
while (env.fileExists(newFile));
sentences.Add(new Sentence(newFile + ";New Description;New Voice Message", env.baseDir()));
this.lvSentences.Items.Refresh();
this.lvSentences.SelectedIndex = this.lvSentences.Items.Count - 1;
this.lvSentences.ScrollIntoView(this.lvSentences.SelectedItem);
}
private void record(object sender, RoutedEventArgs e) private void record(object sender, RoutedEventArgs e)
{ {
if (this.lvSentences.SelectedItems.Count < 1) return; if (this.lvSentences.SelectedItems.Count < 1) return;
@@ -182,19 +182,11 @@ namespace OpenTXrecorder
Sentence sentence = (Sentence)this.lvSentences.SelectedItem; Sentence sentence = (Sentence)this.lvSentences.SelectedItem;
string path = sentence.fullPath(); string path = sentence.fullPath();
Directory.CreateDirectory(Path.GetDirectoryName(path)); // Create directory if it doesn't exist Directory.CreateDirectory(Path.GetDirectoryName(path)); // Create directory if it doesn't exist
System.IO.File.WriteAllText(path, ""); // Creates and flushes a file if it does not exist System.IO.File.WriteAllText(path, ""); // Create and flush the file
int samplerate = 16000; filewriter = new WavFileWriter(path, recSamplerate, recBits, recChannels);
int bits = 16; // 8 or 16 WaveFormat fmt = new WaveFormat(recSamplerate, recBits, recChannels);
int channels = 1; // 1 or 2 recorder = new WaveInRecorder(-1, fmt, recBuffersize, this.DataArrived);
filewriter = new WavFileWriter(path, samplerate, bits, channels);
WaveFormat fmt = new WaveFormat(samplerate, bits, channels);
// devicenumber, wavformat, buffersize, callback
int buffersize = 1024 * 2;
tmparray = new byte[buffersize];
recorder = new WaveInRecorder(-1, fmt, buffersize, this.DataArrived);
} }
} }
@@ -216,27 +208,24 @@ namespace OpenTXrecorder
private void DataArrived(IntPtr data, int size) private void DataArrived(IntPtr data, int size)
{ {
if (!isRecording) Marshal.Copy(data, recordingBuffer, 0, size);
return; filewriter.Write(recordingBuffer);
Marshal.Copy(data, tmparray, 0, size);
filewriter.Write(tmparray);
} }
} }
public class Environment public class Environment
{ {
public string shortLanguage { get; set; } public string shortLanguage { get; set; }
public string baseDir() { return @"SOUNDS\" + shortLanguage + @"\"; } public string baseDir { get { return @"SOUNDS\" + shortLanguage + @"\"; } }
public string sysDir() { return @"SOUNDS\" + shortLanguage + @"\SYSTEM\"; } public string sysDir { get { return @"SOUNDS\" + shortLanguage + @"\SYSTEM\"; } }
public string otherSounds() { return baseDir() + "other_sounds.txt"; } public string otherSounds { get { return baseDir + "other_sounds.txt"; } }
public string systemSounds() { return sysDir() + "system_sounds.txt"; } public string systemSounds { get { return sysDir + "system_sounds.txt"; } }
public bool fileExists(string fName) public bool fileExists(string fName)
{ {
if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + baseDir() + fName + ".wav")) if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + baseDir + fName + ".wav"))
return true; return true;
if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + sysDir() + fName + ".wav")) if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + sysDir + fName + ".wav"))
return true; return true;
return false; return false;
} }
+2 -2
View File
@@ -67,5 +67,5 @@ using System.Windows;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1")] [assembly: AssemblyVersion("0.2")]
[assembly: AssemblyFileVersion("0.1")] [assembly: AssemblyFileVersion("0.2")]