Skip to content

Commit

Permalink
some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
markheath committed Oct 9, 2013
1 parent 8ba34d6 commit bfe826e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
16 changes: 4 additions & 12 deletions VoiceRecorder.Audio/SampleAggregator.cs
@@ -1,25 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace VoiceRecorder.Audio
{
public class SampleAggregator
{
// volume
public event EventHandler<MaxSampleEventArgs> MaximumCalculated;
public event EventHandler Restart = delegate { };
public float maxValue;
public float minValue;
private float maxValue;
private float minValue;
public int NotificationCount { get; set; }
int count;

public SampleAggregator()
{
}

public void RaiseRestart()
{
Restart(this, EventArgs.Empty);
Expand Down Expand Up @@ -52,8 +44,8 @@ public class MaxSampleEventArgs : EventArgs
[DebuggerStepThrough]
public MaxSampleEventArgs(float minValue, float maxValue)
{
this.MaxSample = maxValue;
this.MinSample = minValue;
MaxSample = maxValue;
MinSample = minValue;
}
public float MaxSample { get; private set; }
public float MinSample { get; private set; }
Expand Down
30 changes: 15 additions & 15 deletions VoiceRecorder.Core/PolygonWaveFormControl.xaml.cs
Expand Up @@ -25,24 +25,24 @@ public partial class PolygonWaveFormControl : UserControl, IWaveFormRenderer

public SampleAggregator SampleAggregator
{
get { return (SampleAggregator)this.GetValue(SampleAggregatorProperty); }
set { this.SetValue(SampleAggregatorProperty, value); }
get { return (SampleAggregator)GetValue(SampleAggregatorProperty); }
set { SetValue(SampleAggregatorProperty, value); }
}

private static void OnSampleAggregatorChanged(object sender, DependencyPropertyChangedEventArgs e)
{
PolygonWaveFormControl control = (PolygonWaveFormControl)sender;
var control = (PolygonWaveFormControl)sender;
control.Subscribe();
}

public void Subscribe()
{
SampleAggregator.MaximumCalculated += SampleAggregator_MaximumCalculated;
SampleAggregator.MaximumCalculated += OnMaximumCalculated;
}

void SampleAggregator_MaximumCalculated(object sender, MaxSampleEventArgs e)
void OnMaximumCalculated(object sender, MaxSampleEventArgs e)
{
if (this.IsEnabled)
if (IsEnabled)
{
this.AddValue(e.MaxSample, e.MinSample);
}
Expand All @@ -54,13 +54,13 @@ void SampleAggregator_MaximumCalculated(object sender, MaxSampleEventArgs e)
private double xScale = 2;
private int blankZone = 10;

Polygon waveForm = new Polygon();
readonly Polygon waveForm = new Polygon();

public PolygonWaveFormControl()
{
this.SizeChanged += OnSizeChanged;
SizeChanged += OnSizeChanged;
InitializeComponent();
waveForm.Stroke = this.Foreground;
waveForm.Stroke = Foreground;
waveForm.StrokeThickness = 1;
waveForm.Fill = new SolidColorBrush(Colors.Bisque);
mainCanvas.Children.Add(waveForm);
Expand All @@ -72,8 +72,8 @@ void OnSizeChanged(object sender, SizeChangedEventArgs e)
renderPosition = 0;
ClearAllPoints();

this.yTranslate = this.ActualHeight / 2;
this.yScale = this.ActualHeight / 2;
yTranslate = ActualHeight / 2;
yScale = ActualHeight / 2;
}

private void ClearAllPoints()
Expand All @@ -88,7 +88,7 @@ private int Points

public void AddValue(float maxValue, float minValue)
{
int visiblePixels = (int)(ActualWidth / xScale);
var visiblePixels = (int)(ActualWidth / xScale);
if (visiblePixels > 0)
{
CreatePoint(maxValue, minValue);
Expand Down Expand Up @@ -119,9 +119,9 @@ private double SampleToYPosition(float value)

private void CreatePoint(float topValue, float bottomValue)
{
double topYPos = SampleToYPosition(topValue);
double bottomYPos = SampleToYPosition(bottomValue);
double xPos = renderPosition * xScale;
var topYPos = SampleToYPosition(topValue);
var bottomYPos = SampleToYPosition(bottomValue);
var xPos = renderPosition * xScale;
if (renderPosition >= Points)
{
int insertPos = Points;
Expand Down
3 changes: 2 additions & 1 deletion VoiceRecorder/RecorderView.xaml
Expand Up @@ -32,7 +32,8 @@
<StackPanel Background="Khaki">
<Label Margin="5" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20">.NET Voice Recorder</Label>
<Label Margin="5">Check the levels before starting recording:</Label>
<ProgressBar Margin="5" Orientation="Horizontal" Value="{Binding CurrentInputLevel, Mode=OneWay}" Height="20" />
<ProgressBar Margin="5" Orientation="Horizontal"
Value="{Binding CurrentInputLevel, Mode=OneWay}" Height="20" />
<Label Margin="5">Microphone Level:</Label>
<Slider Margin="5" Orientation="Horizontal" Value="{Binding MicrophoneLevel, Mode=TwoWay}" Maximum="100" />
<StackPanel Orientation="Horizontal">
Expand Down
37 changes: 17 additions & 20 deletions VoiceRecorder/RecorderViewModel.cs
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Threading;
using VoiceRecorder.Core;
using System.IO;
using VoiceRecorder.Audio;
using GalaSoft.MvvmLight.Messaging;
Expand All @@ -15,32 +10,33 @@ namespace VoiceRecorder
{
class RecorderViewModel : ViewModelBase, IView
{
private RelayCommand beginRecordingCommand;
private RelayCommand stopCommand;
private IAudioRecorder recorder;
private readonly RelayCommand beginRecordingCommand;
private readonly RelayCommand stopCommand;
private readonly IAudioRecorder recorder;
private float lastPeak;
private string waveFileName;
public const string ViewName = "RecorderView";

public RecorderViewModel(IAudioRecorder recorder)
{
this.recorder = recorder;
this.recorder.Stopped += new EventHandler(recorder_Stopped);
this.beginRecordingCommand = new RelayCommand(() => BeginRecording(),
this.recorder.Stopped += OnRecorderStopped;
beginRecordingCommand = new RelayCommand(BeginRecording,
() => recorder.RecordingState == RecordingState.Stopped ||
recorder.RecordingState == RecordingState.Monitoring);
this.stopCommand = new RelayCommand(() => Stop(),
stopCommand = new RelayCommand(Stop,
() => recorder.RecordingState == RecordingState.Recording);
recorder.SampleAggregator.MaximumCalculated += new EventHandler<MaxSampleEventArgs>(recorder_MaximumCalculated);
Messenger.Default.Register<ShuttingDownMessage>(this, (message) => OnShuttingDown(message));
recorder.SampleAggregator.MaximumCalculated += OnRecorderMaximumCalculated;
Messenger.Default.Register<ShuttingDownMessage>(this, OnShuttingDown);
}

void recorder_Stopped(object sender, EventArgs e)
void OnRecorderStopped(object sender, EventArgs e)
{
Messenger.Default.Send(new NavigateMessage(SaveViewModel.ViewName, new VoiceRecorderState(waveFileName, null)));
Messenger.Default.Send(new NavigateMessage(SaveViewModel.ViewName,
new VoiceRecorderState(waveFileName, null)));
}

void recorder_MaximumCalculated(object sender, MaxSampleEventArgs e)
void OnRecorderMaximumCalculated(object sender, MaxSampleEventArgs e)
{
lastPeak = Math.Max(e.MaxSample, Math.Abs(e.MinSample));
RaisePropertyChanged("CurrentInputLevel");
Expand All @@ -57,7 +53,7 @@ public void Activated(object state)

private void OnShuttingDown(ShuttingDownMessage message)
{
if (message.CurrentViewName == RecorderViewModel.ViewName)
if (message.CurrentViewName == ViewName)
{
recorder.Stop();
}
Expand All @@ -67,8 +63,9 @@ public string RecordedTime
{
get
{
TimeSpan current = recorder.RecordedTime;
return String.Format("{0:D2}:{1:D2}.{2:D3}", current.Minutes, current.Seconds, current.Milliseconds);
var current = recorder.RecordedTime;
return String.Format("{0:D2}:{1:D2}.{2:D3}",
current.Minutes, current.Seconds, current.Milliseconds);
}
}

Expand All @@ -80,7 +77,7 @@ private void BeginMonitoring(int recordingDevice)

private void BeginRecording()
{
this.waveFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".wav");
waveFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".wav");
recorder.BeginRecording(waveFileName);
RaisePropertyChanged("MicrophoneLevel");
RaisePropertyChanged("ShowWaveForm");
Expand Down

0 comments on commit bfe826e

Please sign in to comment.