Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created November 1, 2013 04:59
Show Gist options
  • Select an option

  • Save DevJohnC/7261126 to your computer and use it in GitHub Desktop.

Select an option

Save DevJohnC/7261126 to your computer and use it in GitHub Desktop.
//
// Author: John Carruthers (johnc@frag-labs.com)
//
// Copyright (C) 2013 John Carruthers
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Threading;
namespace FragLabs.Audio.Locator
{
/// <summary>
/// A basic emitter that plays a single frequency.
/// </summary>
public class BasicEmitter : IEmitter
{
private readonly Stream _outputAudioStream;
private readonly TimeSpan _repeatDelay;
private readonly int _frequency;
private readonly int _amplitude;
private readonly int _channels;
private readonly int _sampleRate;
private byte[] _emitSoundData;
private Thread _thread;
public BasicEmitter(Stream outputAudioStream, TimeSpan repeatDelay, int frequency,
int amplitude, int channels, int sampleRate)
{
_outputAudioStream = outputAudioStream;
_repeatDelay = repeatDelay;
_frequency = frequency;
_amplitude = amplitude;
_channels = channels;
_sampleRate = sampleRate;
if (outputAudioStream == null) throw new ArgumentNullException("outputAudioStream");
ProduceEmitSoundData();
// todo: replace this with a better mechanism for transmitting
_thread = new Thread(() =>
{
while (true)
{
_outputAudioStream.Write(_emitSoundData, 0, _emitSoundData.Length);
Thread.Sleep(_repeatDelay);
}
});
_thread.Start();
}
private void ProduceEmitSoundData()
{
var duration = TimeSpan.FromMilliseconds(10);
var sampleCount = Convert.ToInt32(duration.TotalSeconds *
_sampleRate);
var samples = new short[sampleCount * _channels];
var angle = (Math.PI * 2 * _frequency) /
(_sampleRate * _channels);
for (var i = 0; i < sampleCount; i++)
{
var offset = i * 2;
samples[offset] = Convert.ToInt16(_amplitude *
Math.Sin(angle * i));
samples[offset + 1] = Convert.ToInt16(_amplitude *
Math.Sin(angle * i));
}
_emitSoundData = new byte[sampleCount * 2 * _channels];
for (var i = 0; i < samples.Length; i++)
{
var offset = i * 2;
var sample = BitConverter.GetBytes(samples[i]);
Buffer.BlockCopy(sample, 0, _emitSoundData, offset, 2);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment