Implemented audio player base

This commit is contained in:
kolardavid 2018-01-10 06:50:35 +01:00
parent 31b49d2df3
commit 7dbaa8dd82
3 changed files with 211 additions and 125 deletions

View File

@ -28,12 +28,14 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AwcForm)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AwcForm));
this.MainTabControl = new System.Windows.Forms.TabControl(); this.MainTabControl = new System.Windows.Forms.TabControl();
this.PlayerTabPage = new System.Windows.Forms.TabPage(); this.PlayerTabPage = new System.Windows.Forms.TabPage();
this.VolumeLabel = new System.Windows.Forms.Label();
this.chbAutoJump = new System.Windows.Forms.CheckBox();
this.PrevButton = new System.Windows.Forms.Button(); this.PrevButton = new System.Windows.Forms.Button();
this.NextButton = new System.Windows.Forms.Button(); this.NextButton = new System.Windows.Forms.Button();
this.VolumeButton = new System.Windows.Forms.Button();
this.PlayButton = new System.Windows.Forms.Button(); this.PlayButton = new System.Windows.Forms.Button();
this.PlayListView = new System.Windows.Forms.ListView(); this.PlayListView = new System.Windows.Forms.ListView();
this.PlaylistNameHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.PlaylistNameHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
@ -43,7 +45,7 @@
this.PositionTrackBar = new System.Windows.Forms.TrackBar(); this.PositionTrackBar = new System.Windows.Forms.TrackBar();
this.DetailsTabPage = new System.Windows.Forms.TabPage(); this.DetailsTabPage = new System.Windows.Forms.TabPage();
this.DetailsPropertyGrid = new CodeWalker.WinForms.PropertyGridFix(); this.DetailsPropertyGrid = new CodeWalker.WinForms.PropertyGridFix();
this.label1 = new System.Windows.Forms.Label(); this.Timer = new System.Windows.Forms.Timer(this.components);
this.MainTabControl.SuspendLayout(); this.MainTabControl.SuspendLayout();
this.PlayerTabPage.SuspendLayout(); this.PlayerTabPage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.VolumeTrackBar)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.VolumeTrackBar)).BeginInit();
@ -64,10 +66,10 @@
// //
// PlayerTabPage // PlayerTabPage
// //
this.PlayerTabPage.Controls.Add(this.label1); this.PlayerTabPage.Controls.Add(this.VolumeLabel);
this.PlayerTabPage.Controls.Add(this.chbAutoJump);
this.PlayerTabPage.Controls.Add(this.PrevButton); this.PlayerTabPage.Controls.Add(this.PrevButton);
this.PlayerTabPage.Controls.Add(this.NextButton); this.PlayerTabPage.Controls.Add(this.NextButton);
this.PlayerTabPage.Controls.Add(this.VolumeButton);
this.PlayerTabPage.Controls.Add(this.PlayButton); this.PlayerTabPage.Controls.Add(this.PlayButton);
this.PlayerTabPage.Controls.Add(this.PlayListView); this.PlayerTabPage.Controls.Add(this.PlayListView);
this.PlayerTabPage.Controls.Add(this.VolumeTrackBar); this.PlayerTabPage.Controls.Add(this.VolumeTrackBar);
@ -80,6 +82,26 @@
this.PlayerTabPage.Text = "Player"; this.PlayerTabPage.Text = "Player";
this.PlayerTabPage.UseVisualStyleBackColor = true; this.PlayerTabPage.UseVisualStyleBackColor = true;
// //
// VolumeLabel
//
this.VolumeLabel.AutoSize = true;
this.VolumeLabel.Location = new System.Drawing.Point(414, 305);
this.VolumeLabel.Name = "VolumeLabel";
this.VolumeLabel.Size = new System.Drawing.Size(42, 13);
this.VolumeLabel.TabIndex = 9;
this.VolumeLabel.Text = "Volume";
//
// chbAutoJump
//
this.chbAutoJump.AutoSize = true;
this.chbAutoJump.Enabled = false;
this.chbAutoJump.Location = new System.Drawing.Point(17, 305);
this.chbAutoJump.Name = "chbAutoJump";
this.chbAutoJump.Size = new System.Drawing.Size(108, 17);
this.chbAutoJump.TabIndex = 8;
this.chbAutoJump.Text = "Auto-jump to next";
this.chbAutoJump.UseVisualStyleBackColor = true;
//
// PrevButton // PrevButton
// //
this.PrevButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom; this.PrevButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
@ -102,17 +124,6 @@
this.NextButton.UseVisualStyleBackColor = true; this.NextButton.UseVisualStyleBackColor = true;
this.NextButton.Click += new System.EventHandler(this.NextButton_Click); this.NextButton.Click += new System.EventHandler(this.NextButton_Click);
// //
// VolumeButton
//
this.VolumeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.VolumeButton.Location = new System.Drawing.Point(426, 301);
this.VolumeButton.Name = "VolumeButton";
this.VolumeButton.Size = new System.Drawing.Size(33, 23);
this.VolumeButton.TabIndex = 5;
this.VolumeButton.Text = "Vol";
this.VolumeButton.UseVisualStyleBackColor = true;
this.VolumeButton.Click += new System.EventHandler(this.VolumeButton_Click);
//
// PlayButton // PlayButton
// //
this.PlayButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom; this.PlayButton.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
@ -136,11 +147,13 @@
this.PlayListView.FullRowSelect = true; this.PlayListView.FullRowSelect = true;
this.PlayListView.HideSelection = false; this.PlayListView.HideSelection = false;
this.PlayListView.Location = new System.Drawing.Point(6, 6); this.PlayListView.Location = new System.Drawing.Point(6, 6);
this.PlayListView.MultiSelect = false;
this.PlayListView.Name = "PlayListView"; this.PlayListView.Name = "PlayListView";
this.PlayListView.Size = new System.Drawing.Size(556, 216); this.PlayListView.Size = new System.Drawing.Size(556, 235);
this.PlayListView.TabIndex = 0; this.PlayListView.TabIndex = 0;
this.PlayListView.UseCompatibleStateImageBehavior = false; this.PlayListView.UseCompatibleStateImageBehavior = false;
this.PlayListView.View = System.Windows.Forms.View.Details; this.PlayListView.View = System.Windows.Forms.View.Details;
this.PlayListView.DoubleClick += new System.EventHandler(this.PlayListView_DoubleClick);
// //
// PlaylistNameHeader // PlaylistNameHeader
// //
@ -176,7 +189,7 @@
this.PositionTrackBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) this.PositionTrackBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.PositionTrackBar.BackColor = System.Drawing.SystemColors.ControlLightLight; this.PositionTrackBar.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.PositionTrackBar.LargeChange = 50; this.PositionTrackBar.LargeChange = 1000;
this.PositionTrackBar.Location = new System.Drawing.Point(6, 263); this.PositionTrackBar.Location = new System.Drawing.Point(6, 263);
this.PositionTrackBar.Maximum = 1000; this.PositionTrackBar.Maximum = 1000;
this.PositionTrackBar.Name = "PositionTrackBar"; this.PositionTrackBar.Name = "PositionTrackBar";
@ -205,14 +218,10 @@
this.DetailsPropertyGrid.Size = new System.Drawing.Size(562, 326); this.DetailsPropertyGrid.Size = new System.Drawing.Size(562, 326);
this.DetailsPropertyGrid.TabIndex = 0; this.DetailsPropertyGrid.TabIndex = 0;
// //
// label1 // Timer
// //
this.label1.AutoSize = true; this.Timer.Enabled = true;
this.label1.Location = new System.Drawing.Point(44, 238); this.Timer.Tick += new System.EventHandler(this.Timer_Tick);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(248, 13);
this.label1.TabIndex = 7;
this.label1.Text = "NOTE: Work in progress... Audio does not play yet!";
// //
// AwcForm // AwcForm
// //
@ -223,6 +232,7 @@
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "AwcForm"; this.Name = "AwcForm";
this.Text = "AWC Player - CodeWalker by dexyfex"; this.Text = "AWC Player - CodeWalker by dexyfex";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.AwcForm_FormClosing);
this.MainTabControl.ResumeLayout(false); this.MainTabControl.ResumeLayout(false);
this.PlayerTabPage.ResumeLayout(false); this.PlayerTabPage.ResumeLayout(false);
this.PlayerTabPage.PerformLayout(); this.PlayerTabPage.PerformLayout();
@ -245,10 +255,11 @@
private System.Windows.Forms.ColumnHeader PlaylistLengthHeader; private System.Windows.Forms.ColumnHeader PlaylistLengthHeader;
private System.Windows.Forms.Button PrevButton; private System.Windows.Forms.Button PrevButton;
private System.Windows.Forms.Button NextButton; private System.Windows.Forms.Button NextButton;
private System.Windows.Forms.Button VolumeButton;
private System.Windows.Forms.Button PlayButton; private System.Windows.Forms.Button PlayButton;
private System.Windows.Forms.TrackBar VolumeTrackBar; private System.Windows.Forms.TrackBar VolumeTrackBar;
private System.Windows.Forms.TrackBar PositionTrackBar; private System.Windows.Forms.TrackBar PositionTrackBar;
private System.Windows.Forms.Label label1; private System.Windows.Forms.Timer Timer;
private System.Windows.Forms.CheckBox chbAutoJump;
private System.Windows.Forms.Label VolumeLabel;
} }
} }

View File

@ -2,15 +2,8 @@
using SharpDX.Multimedia; using SharpDX.Multimedia;
using SharpDX.XAudio2; using SharpDX.XAudio2;
using System; using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace CodeWalker.Forms namespace CodeWalker.Forms
@ -19,6 +12,11 @@ namespace CodeWalker.Forms
{ {
public AwcFile Awc { get; set; } public AwcFile Awc { get; set; }
private XAudio2 xAudio2;
private MasteringVoice masteringVoice;
private AudioBuffer audioBuffer;
private SourceVoice sourceVoice;
private string fileName; private string fileName;
public string FileName public string FileName
{ {
@ -31,36 +29,35 @@ namespace CodeWalker.Forms
} }
public string FilePath { get; set; } public string FilePath { get; set; }
private bool Playing = false; private enum PlayerState { Stopped, Playing, Paused };
private PlayerState playerState = PlayerState.Stopped;
private Stopwatch playtime;
private float trackLength;
public AwcForm() public AwcForm()
{ {
InitializeComponent(); InitializeComponent();
playtime = new Stopwatch();
} }
private void UpdateFormTitle() private void UpdateFormTitle()
{ {
Text = fileName + " - AWC Player - CodeWalker by dexyfex"; Text = fileName + " - AWC Player - CodeWalker by dexyfex";
} }
public void LoadAwc(AwcFile awc) public void LoadAwc(AwcFile awc)
{ {
Awc = awc; Awc = awc;
DetailsPropertyGrid.SelectedObject = awc; DetailsPropertyGrid.SelectedObject = awc;
//MainTabControl.SelectedTab = DetailsTabPage;//remove this
fileName = awc?.Name; fileName = awc?.Name;
if (string.IsNullOrEmpty(fileName)) if (string.IsNullOrEmpty(fileName))
{ {
fileName = awc?.FileEntry?.Name; fileName = awc?.FileEntry?.Name;
} }
PlayListView.Items.Clear(); PlayListView.Items.Clear();
if (awc.Audios != null) if (awc.Audios != null)
{ {
@ -76,79 +73,129 @@ namespace CodeWalker.Forms
UpdateFormTitle(); UpdateFormTitle();
} }
private void Stop()
{
if (playerState != PlayerState.Stopped)
{
sourceVoice.DestroyVoice();
sourceVoice.Dispose();
audioBuffer.Stream.Dispose();
SetPlayerState(PlayerState.Stopped);
}
}
private void SetPlayerState(PlayerState newState)
{
if (playerState != newState)
{
switch (newState)
{
case PlayerState.Playing:
if (playerState == PlayerState.Stopped)
playtime.Reset();
playtime.Start();
break;
default:
playtime.Stop();
break;
}
playerState = newState;
}
}
private void InitializeAudio(AwcAudio audio)
{
trackLength = audio.Length;
if (xAudio2 == null)
{
xAudio2 = new XAudio2();
masteringVoice = new MasteringVoice(xAudio2);
}
Stream wavStream = audio.GetWavStream();
SoundStream soundStream = new SoundStream(wavStream);
audioBuffer = new AudioBuffer
{
Stream = soundStream.ToDataStream(),
AudioBytes = (int)soundStream.Length,
Flags = BufferFlags.EndOfStream
};
soundStream.Close();
wavStream.Close();
sourceVoice = new SourceVoice(xAudio2, soundStream.Format, true);
sourceVoice.SubmitSourceBuffer(audioBuffer, soundStream.DecodedPacketsInfo);
sourceVoice.SetVolume((float)VolumeTrackBar.Value / 100);
}
private void Play() private void Play()
{ {
if (PlayListView.SelectedItems.Count != 1) return; Stop();
var item = PlayListView.SelectedItems[0]; if (PlayListView.SelectedItems.Count == 1)
var audio = item.Tag as AwcAudio; {
var item = PlayListView.SelectedItems[0];
var audio = item.Tag as AwcAudio;
if (audio == null) return; if (audio != null)
{
InitializeAudio(audio);
sourceVoice.Start();
SetPlayerState(PlayerState.Playing);
}
}
}
private void PlayPrevious()
{
Stop();
if (PlayListView.SelectedIndices.Count > 0)
{
var nextIndex = PlayListView.SelectedIndices[0] - 1;
if (nextIndex >= 0)
{
PlayListView.Items[nextIndex].Selected = true;
PlayListView.Items[nextIndex].Focused = true;
Play();
}
}
}
private void PlayNext()
//see https://github.com/sharpdx/SharpDX-Samples/blob/master/Desktop/XAudio2/PlaySound/Program.cs {
//see https://github.com/sharpdx/SharpDX-Samples/blob/master/Desktop/XAudio2/AudioPlayerApp/AudioPlayer.cs Stop();
if (PlayListView.SelectedIndices.Count > 0)
//var mstrm = new MemoryStream(audio.Data); {
//var sstrm = new SoundStream(mstrm); var nextIndex = PlayListView.SelectedIndices[0] + 1;
//SourceVoice sv=new SourceVoice() if (nextIndex < PlayListView.Items.Count)
var mstrm = audio.GetWavStream(); {
PlayListView.Items[nextIndex].Selected = true;
////var mdata = ((MemoryStream)mstrm).GetBuffer(); PlayListView.Items[nextIndex].Focused = true;
////File.WriteAllBytes("C:\\test2.wav", mdata); Play();
////return; }
}
//var sstrm = new SoundStream(mstrm);
//var waveFormat = sstrm.Format;
//var buffer = new AudioBuffer
//{
// Stream = sstrm.ToDataStream(),
// AudioBytes = (int)sstrm.Length,
// Flags = BufferFlags.EndOfStream
//};
//sstrm.Close();
//var xaudio2 = new XAudio2();//cache this...
//var masteringVoice = new MasteringVoice(xaudio2);//cache this...
//var sourceVoice = new SourceVoice(xaudio2, waveFormat, true);
////sourceVoice.BufferEnd += (context) => Console.WriteLine(" => event received: end of buffer");
//sourceVoice.SubmitSourceBuffer(buffer, sstrm.DecodedPacketsInfo);
//sourceVoice.Start();
//while (sourceVoice.State.BuffersQueued > 0) // && !IsKeyPressed(ConsoleKey.Escape))
//{
// Thread.Sleep(10);
//}
//sourceVoice.DestroyVoice();
//sourceVoice.Dispose();
//buffer.Stream.Dispose();
//masteringVoice.Dispose();//on form exit?
//xaudio2.Dispose();//on form exit?
Playing = true;
} }
private void Pause() private void Pause()
{ {
if (playerState == PlayerState.Playing)
Playing = false; {
sourceVoice.Stop();
SetPlayerState(PlayerState.Paused);
}
} }
private void Prev() private void Resume()
{ {
if (playerState == PlayerState.Paused)
{
sourceVoice.Start();
SetPlayerState(PlayerState.Playing);
}
} }
private void Next()
{
}
private void PositionTrackBar_Scroll(object sender, EventArgs e) private void PositionTrackBar_Scroll(object sender, EventArgs e)
{ {
@ -156,41 +203,66 @@ namespace CodeWalker.Forms
private void PlayButton_Click(object sender, EventArgs e) private void PlayButton_Click(object sender, EventArgs e)
{ {
if (Playing) Pause(); switch (playerState)
else Play(); {
case PlayerState.Stopped:
Play();
break;
case PlayerState.Playing:
Pause();
break;
case PlayerState.Paused:
Resume();
break;
}
} }
private void PrevButton_Click(object sender, EventArgs e) private void PrevButton_Click(object sender, EventArgs e)
{ {
Prev(); PlayPrevious();
} }
private void NextButton_Click(object sender, EventArgs e) private void NextButton_Click(object sender, EventArgs e)
{ {
Next(); PlayNext();
}
private void VolumeButton_Click(object sender, EventArgs e)
{
} }
private void VolumeTrackBar_Scroll(object sender, EventArgs e) private void VolumeTrackBar_Scroll(object sender, EventArgs e)
{ {
if (playerState == PlayerState.Playing)
sourceVoice.SetVolume((float)VolumeTrackBar.Value / 100);
}
private void Timer_Tick(object sender, EventArgs e)
{
if (playerState != PlayerState.Stopped)
{
int playedMs = (int)playtime.Elapsed.TotalMilliseconds;
int totalMs = (int)(trackLength * 1000);
PositionTrackBar.Maximum = totalMs;
PositionTrackBar.Value = playedMs < totalMs ? playedMs : totalMs;
}
else
{
PositionTrackBar.Value = 0;
}
}
private void PlayListView_DoubleClick(object sender, EventArgs e)
{
if (playerState == PlayerState.Playing)
Stop();
Play();
}
private void AwcForm_FormClosing(object sender, FormClosingEventArgs e)
{
Stop();
if (xAudio2 != null)
{
masteringVoice.Dispose();
xAudio2.Dispose();
}
} }
} }
public class AudioPlayer
{
}
} }

View File

@ -117,6 +117,9 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="Timer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>