Skip to content

Commit

Permalink
Add partially working dig sounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Nov 27, 2015
1 parent f864d0e commit 4d7ca25
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 47 deletions.
5 changes: 3 additions & 2 deletions ClassicalSharp/2D/Screens/Menu/OptionsScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public override void Init() {
// Column 1
Make( -140, -150, "Use sound (WIP)", Anchor.Centre, OnWidgetClick,
g => g.UseSound ? "yes" : "no",
(g, v) => g.UseSound = v == "yes" ),
(g, v) => { g.UseSound = v == "yes";
g.AudioManager.SetSound( g.UseSound ); } ),

Make( -140, -100, "Show hover names", Anchor.Centre, OnWidgetClick,
g => g.Players.ShowHoveredNames ? "yes" : "no",
Expand Down Expand Up @@ -51,7 +52,7 @@ public override void Init() {
Make( 140, -150, "Use music (WIP)", Anchor.Centre, OnWidgetClick,
g => g.UseMusic ? "yes" : "no",
(g, v) => { g.UseMusic = v == "yes";
g.AudioManager.SetState( g.UseMusic ); } ),
g.AudioManager.SetMusic( g.UseMusic ); } ),

Make( 140, -100, "View bobbing", Anchor.Centre, OnWidgetClick,
g => g.ViewBobbing ? "yes" : "no",
Expand Down
104 changes: 104 additions & 0 deletions ClassicalSharp/Audio/AudioManager.Sounds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.IO;
using System.Threading;
using OpenTK;
using SharpWave;
using SharpWave.Codecs;
using SharpWave.Codecs.Vorbis;

namespace ClassicalSharp.Audio {

public sealed partial class AudioManager {

Soundboard digBoard, stepBoard;

public void SetSound( bool enabled ) {
if( enabled )
InitSound();
else
DisposeSound();
}

void InitSound() {
disposingSound = false;
if( digBoard == null ) InitSoundboards();
soundThread = MakeThread( DoSoundThread, ref soundOut,
"ClassicalSharp.DoSound" );
}

AudioChunk soundChunk = new AudioChunk();
object soundLock = new object();
volatile int soundsCount = 0;
const int maxSounds = 5;
Sound[] sounds = new Sound[maxSounds];
byte[][] soundDatas = new byte[maxSounds][];

void DoSoundThread() {
while( !disposingSound ) {
bool playSound = false;
lock( soundLock ) {
if( soundsCount > 0 ) {
soundChunk.Data = soundDatas[0];
Sound meta = sounds[0];
playSound = true;
RemoveOldestSound();

soundChunk.Frequency = meta.SampleRate;
soundChunk.BitsPerSample = meta.BitsPerSample;
soundChunk.Channels = meta.Channels;
soundChunk.BytesOffset = meta.Offset;
soundChunk.BytesUsed = meta.Length;
}
}
if( playSound )
soundOut.PlayRaw( soundChunk );
Thread.Sleep( 1 );
}
}

public void PlayDigSound( SoundType type ) {
PlaySound( type, digBoard );
}

public void PlayStepSound( SoundType type ) {
PlaySound( type, stepBoard );
}

void PlaySound( SoundType type, Soundboard board ) {
if( type == SoundType.None || soundOut == null )
return;
Sound sound = board.PlayRandomSound( type );

lock( soundLock ) {
if( soundsCount == maxSounds )
RemoveOldestSound();
sounds[soundsCount] = sound;
soundDatas[soundsCount] = board.Data;
soundsCount++;
}
}

void RemoveOldestSound() {
for( int i = 0; i < maxSounds - 1; i++ ) {
sounds[i] = sounds[i + 1];
soundDatas[i] = soundDatas[i + 1];
}

sounds[maxSounds - 1] = null;
soundDatas[maxSounds - 1] = null;
soundsCount--;
}

void DisposeSound() {
disposingSound = true;
DisposeOf( ref soundOut, ref soundThread );
}

void InitSoundboards() {
digBoard = new Soundboard();
digBoard.Init( "dig" );
stepBoard = new Soundboard();
stepBoard.Init( "step" );
}
}
}
72 changes: 32 additions & 40 deletions ClassicalSharp/Audio/AudioManager.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,32 @@
using System;
using System.IO;
using System.Threading;
using OpenTK;
using SharpWave;
using SharpWave.Codecs.Vorbis;

namespace ClassicalSharp.Audio {

public sealed class AudioManager {
public sealed partial class AudioManager {

IAudioOutput musicOut, soundOut;
Soundboard digBoard, stepBoard;
string[] musicFiles;
Thread musicThread, soundThread;

public AudioManager() {
digBoard = new Soundboard();
digBoard.Init( "dig" );
stepBoard = new Soundboard();
stepBoard.Init( "step" );
musicFiles = Directory.GetFiles( "audio", "*.ogg" );
}

public void SetState( bool state ) {
if( state )
Init();
public void SetMusic( bool enabled ) {
if( enabled )
InitMusic();
else
Dispose();
}

public void Init() {
// TODO: why is waveOut crashing?
if( false/*Configuration.RunningOnWindows*/ ) {
musicOut = new WinMmOut();
soundOut = new WinMmOut();
} else {
musicOut = new OpenALOut();
soundOut = new OpenALOut();
}

musicOut.Create( 5 );
soundOut.Create( 5, musicOut );
InitThreads();
DisposeMusic();
}

Thread musicThread, soundThread;
void InitThreads() {
void InitMusic() {
disposingMusic = false;
musicThread = new Thread( DoMusicThread );
musicThread.Name = "ClassicalSharp.DoMusicThread";
musicThread.IsBackground = true;
musicThread.Start();
musicThread = MakeThread( DoMusicThread, ref musicOut,
"ClassicalSharp.DoMusic" );
}

void DoMusicThread() {
Expand All @@ -68,22 +45,37 @@ void DoMusicThread() {
}
}

bool disposingMusic;
bool disposingMusic, disposingSound;
public void Dispose() {
DisposeMusic();
DisposeSound();
}

public void DisposeMusic() {
void DisposeMusic() {
disposingMusic = true;
musicOut.Stop();
musicThread.Join();
musicOut.Dispose();
DisposeOf( ref musicOut, ref musicThread );
}

Thread MakeThread( ThreadStart func, ref IAudioOutput output, string name ) {
// TODO: why is waveOut crashing?
output = new OpenALOut();
output.Create( 5 );

Thread thread = new Thread( func );
thread.Name = name;
thread.IsBackground = true;
thread.Start();
return thread;
}

public void DisposeSound() {
// TODO: stop playing current sound and/or music
soundOut.Dispose();
void DisposeOf( ref IAudioOutput output, ref Thread thread ) {
if( output == null ) return;
output.Stop();
thread.Join();

output.Dispose();
output = null;
thread = null;
}
}
}
15 changes: 11 additions & 4 deletions ClassicalSharp/Audio/Soundboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Soundboard {
static string[] soundNames;
static Soundboard() {
soundNames = Enum.GetNames( typeof( SoundType ) );
for( int i = 0; i < soundNames.Length; i++ )
soundNames[i] = soundNames[i].ToLower();
}

public void Init( string group ) {
Expand Down Expand Up @@ -43,7 +45,7 @@ void ReadMetadata( string path ) {
if( line.Length == 0 || line[0] == '#' ) continue;
string[] parts = line.Split( ',' );
if( parts.Length < 6 ) continue;
string name = parts[0];
string name = parts[0].ToLower();
int sampleRate, bitsPerSample, channels;
int offset, length;

Expand All @@ -63,19 +65,24 @@ void ReadMetadata( string path ) {
}

void GetGroups() {
string last = rawSounds[0].Name;
string last = Group( rawSounds[0].Name );
int offset = 0, count = 0;
for( int i = 0; i < rawSounds.Count; i++ ) {
if( rawSounds[i].Name != last ) {
string group = Group( rawSounds[i].Name );
if( group != last ) {
groupFlags[last] = (count << 12) | offset;
offset = i;
last = rawSounds[i].Name;
last = group;
count = 0;
}
count++;
}
groupFlags[last] = (count << 12) | offset;
}

string Group( string name ) {
return name.Substring( 0, name.Length - 1 );
}
}

public class Sound {
Expand Down
3 changes: 2 additions & 1 deletion ClassicalSharp/ClassicalSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ProjectGuid>{BEB1C785-5CAD-48FF-A886-876BF0A318D4}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<RootNamespace>ClassicalSharp</RootNamespace>
<AssemblyName>ClassicalSharp</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
Expand Down Expand Up @@ -125,6 +125,7 @@
<Compile Include="2D\Widgets\TextWidget.cs" />
<Compile Include="2D\Widgets\Widget.cs" />
<Compile Include="Audio\AudioManager.cs" />
<Compile Include="Audio\AudioManager.Sounds.cs" />
<Compile Include="Audio\Soundboard.cs" />
<Compile Include="Blocks\Block.cs" />
<Compile Include="Blocks\BlockInfo.BoundingBox.cs" />
Expand Down
1 change: 1 addition & 0 deletions ClassicalSharp/Game/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public void PickBlocks( bool cooldown, bool left, bool middle, bool right ) {
if( game.Map.IsValidPos( pos ) && (block = game.Map.GetBlock( pos )) != 0
&& inv.CanDelete[block] ) {
game.ParticleManager.BreakBlockEffect( pos, block );
game.AudioManager.PlayDigSound( game.BlockInfo.DigSounds[block] );
game.UpdateBlock( pos.X, pos.Y, pos.Z, 0 );
game.Network.SendSetBlock( pos.X, pos.Y, pos.Z, false, (byte)inv.HeldBlock );
}
Expand Down

0 comments on commit 4d7ca25

Please sign in to comment.