using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MediaControlInterface
{
public partial class Form1 : Form
{
private string fn;
private string msg;
public Form1()
{
InitializeComponent();
ExtinguishAll();
button1.Enabled = true;
button7.Enabled = true;
}
private void EnableButtonF()
{
}
bool bPlayMode = false;
private void ExtinguishAll()
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
}
private void button1_Click(object sender, EventArgs e) // NEW
{
string returnStatus = mciOpenNew();
textBox1.Text = "NEW: " + returnStatus;
ExtinguishAll();
button2.Enabled = true;
bPlayMode = false;
}
private void button2_Click(object sender, EventArgs e) // RECORD
{
string returnStatus = mciRecord();
textBox1.Text = "RECORD: " + returnStatus;
ExtinguishAll();
button3.Enabled = true;
}
private void button3_Click(object sender, EventArgs e) // STOP
{
string returnStatus = mciStop();
textBox1.Text = "STOP: " + returnStatus;
ExtinguishAll();
if (bPlayMode)
{
button5.Enabled = true;
button6.Enabled = true;
button8.Enabled = true;
}
else
{
button2.Enabled = true;
button4.Enabled = true;
}
}
private void button4_Click(object sender, EventArgs e) // SAVE
{
string returnStatus = mciSave();
textBox1.Text = "SAVE: " + returnStatus;
ExtinguishAll();
button5.Enabled = true;
}
private void button5_Click(object sender, EventArgs e) // CLOSE
{
string returnStatus = mciClose();
textBox1.Text = "CLOSE: " + returnStatus;
ExtinguishAll();
button1.Enabled = true;
button7.Enabled = true;
if (!bPlayMode) // IN RECORD MODE THE FILE JUST CREATED MUST BE SAVED IN THE DIRECTORY
{
string filename = RunSave();
try
{
if (filename.Length > 0)
{
if (System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
System.IO.File.Move(GetTemporaryFilename("orca.wav"), filename);
}
}
catch (Exception exc1)
{
textBox1.Text = "CLOSE: " + exc1.Message;
return;
}
}
}
private void button6_Click(object sender, EventArgs e) // PLAY
{
string returnStatus = mciPlay();
textBox1.Text = "PLAY: " + returnStatus;
ExtinguishAll();
button3.Enabled = true;
bPlayMode = true;
}
private void button8_Click(object sender, EventArgs e) // PLAY ALL
{
string returnStatus = mciPlayAll();
textBox1.Text = "PLAY ALL: " + returnStatus;
ExtinguishAll();
button3.Enabled = true;
bPlayMode = true;
}
private void button7_Click(object sender, EventArgs e) // OLD
{
string filename = RunOpen();
fn = filename;
textBox1.Text = "Copying input file to a scratch file.";
ExtinguishAll();
try
{
if (fn.Length > 0)
{
if (System.IO.File.Exists(GetTemporaryFilename("orca.wav")))
System.IO.File.Delete(GetTemporaryFilename("orca.wav"));
System.IO.File.Copy(fn, GetTemporaryFilename("orca.wav"));
}
}
catch (Exception exc1)
{
textBox1.Text = "OLD: " + exc1.Message;
return;
}
string returnStatus = mciOpenOld();
msg = "OLD: " + returnStatus;
ExtinguishAll();
button6.Enabled = true;
button8.Enabled = true;
bPlayMode = false;
textBox1.Text = msg;
}
public string RunSave()
{
SaveFileDialog dialogbox = new SaveFileDialog();
dialogbox.InitialDirectory = "c:\\";
dialogbox.Filter = "Wave files (*.wav)|*.wav";
dialogbox.FilterIndex = 1;
dialogbox.RestoreDirectory = true;
if (dialogbox.ShowDialog() == DialogResult.OK)
{
return dialogbox.FileName;
}
return "";
}
public string RunOpen()
{
OpenFileDialog dialogbox = new OpenFileDialog();
dialogbox.InitialDirectory = "c:\\";
dialogbox.Filter = "Wave files (*.wav)|*.wav";
dialogbox.FilterIndex = 1;
dialogbox.RestoreDirectory = true;
if (dialogbox.ShowDialog() == DialogResult.OK)
{
try
{
System.IO.Stream stream = dialogbox.OpenFile();
stream.Close();
return dialogbox.FileName;
}
catch
{
return "";
}
}
return "";
}
private string GetTemporaryFilename(string arg)
{
if (!System.IO.Directory.Exists(Environment.GetEnvironmentVariable("TEMP")))
System.IO.Directory.CreateDirectory(Environment.GetEnvironmentVariable("TEMP"));
string filename = Environment.GetEnvironmentVariable("TEMP") + @"\" + arg;
return filename;
}
[System.Runtime.InteropServices.DllImport(@".\CrossLanguageMCISendString.dll")]
public static extern string mciOpenNew();
[System.Runtime.InteropServices.DllImport(@".\CrossLanguageMCISendString.dll")]
public static extern string mciOpenOld();
[System.Runtime.InteropServices.DllImport(@".\CrossLanguageMCISendString.dll")]
public static extern string mciRecord();
[System.Runtime.InteropServices.DllImport(@".\CrossLanguageMCISendString.dll")]
public static extern string mciStop();
[System.Runtime.InteropServices.DllImport(@".\CrossLanguageMCISendString.dll")]
public static extern string mciSave();
[System.Runtime.InteropServices.DllImport(@".\CrossLanguageMCISendString.dll")]
public static extern string mciClose();
[System.Runtime.InteropServices.DllImport(@".\CrossLanguageMCISendString.dll")]
public static extern string mciPlay();
[System.Runtime.InteropServices.DllImport(@".\CrossLanguageMCISendString.dll")]
public static extern string mciPlayAll();
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
#include <Windows.h>
#include <stdio.h>
#pragma comment(lib,"winmm.lib")
#define BUFSIZE 512
#define VARNAME TEXT("TEMP")
char ErrorText[9999];
char TemporaryFilesDirectory[BUFSIZE];
void SetTemporaryFilename()
{
LPTSTR pszOldVal;
DWORD dwRet;
DWORD dwErr;
TemporaryFilesDirectory[0] = 0;
pszOldVal = &TemporaryFilesDirectory[0];
dwRet = GetEnvironmentVariable(VARNAME, pszOldVal, dwRet);
if (0 == dwRet)
{
dwErr = GetLastError();
if( ERROR_ENVVAR_NOT_FOUND == dwErr )
{
TemporaryFilesDirectory[0] = 0; // "Environment variable does not exist."
}
}
else if (dwRet > BUFSIZE)
{
abort();
}
else
{
if (strlen(TemporaryFilesDirectory) < (BUFSIZE-2))
strcat(TemporaryFilesDirectory,"\\");
}
}
extern "C" _declspec(dllexport) char* _stdcall WINAPI mciOpenNew()
{
char szReturnString[9999];
char* lpszReturnString = &szReturnString[0];
SetTemporaryFilename();
MCIERROR status;
BOOL status2;
status = mciSendString("open new type waveaudio alias capture", lpszReturnString, lstrlen(lpszReturnString), NULL);
status2 = mciGetErrorString(status, &ErrorText[0], 9999);
return ErrorText;
}
extern "C" _declspec(dllexport) char* _stdcall WINAPI mciOpenOld()
{
char szReturnString[9999];
char* lpszReturnString = &szReturnString[0];
SetTemporaryFilename();
MCIERROR status;
BOOL status2;
char constructString[1024];
strcpy(constructString, "open ");
strcat(constructString, TemporaryFilesDirectory);
strcat(constructString, "orca.wav");
strcat(constructString, " type waveaudio alias capture");
status = mciSendString(constructString, lpszReturnString, lstrlen(lpszReturnString), NULL);
status2 = mciGetErrorString(status, &ErrorText[0], 9999);
return ErrorText;
}
extern "C" _declspec(dllexport) char* _stdcall WINAPI mciRecord()
{
char szReturnString[9999];
char* lpszReturnString = &szReturnString[0];
SetTemporaryFilename();
MCIERROR status;
BOOL status2;
status = mciSendString("record capture", lpszReturnString, lstrlen(lpszReturnString), NULL);
status2 = mciGetErrorString(status, &ErrorText[0], 9999);
return ErrorText;
}
extern "C" _declspec(dllexport) char* _stdcall WINAPI mciPlay()
{
char szReturnString[9999];
char* lpszReturnString = &szReturnString[0];
SetTemporaryFilename();
MCIERROR status;
BOOL status2;
status = mciSendString("play capture", lpszReturnString, lstrlen(lpszReturnString), NULL);
status2 = mciGetErrorString(status, &ErrorText[0], 9999);
return ErrorText;
}
extern "C" _declspec(dllexport) char* _stdcall WINAPI mciPlayAll()
{
char szReturnString[9999];
char* lpszReturnString = &szReturnString[0];
SetTemporaryFilename();
MCIERROR status;
BOOL status2;
status = mciSendString("play capture from 0 to 100", lpszReturnString, lstrlen(lpszReturnString), NULL);
status2 = mciGetErrorString(status, &ErrorText[0], 9999);
return ErrorText;
}
extern "C" _declspec(dllexport) char* _stdcall WINAPI mciStop()
{
char szReturnString[9999];
char* lpszReturnString = &szReturnString[0];
SetTemporaryFilename();
MCIERROR status;
BOOL status2;
status = mciSendString("stop capture", lpszReturnString, lstrlen(lpszReturnString), NULL);
status2 = mciGetErrorString(status, &ErrorText[0], 9999);
return ErrorText;
}
extern "C" _declspec(dllexport) char* _stdcall WINAPI mciSave()
{
char szReturnString[9999];
char* lpszReturnString = &szReturnString[0];
SetTemporaryFilename();
MCIERROR status;
BOOL status2;
char constructString[1024];
strcpy(constructString, "save capture ");
strcat(constructString, TemporaryFilesDirectory);
strcat(constructString, "orca.wav");
status = mciSendString(constructString, lpszReturnString, lstrlen(lpszReturnString), NULL);
status2 = mciGetErrorString(status, &ErrorText[0], 9999);
return ErrorText;
}
extern "C" _declspec(dllexport) char* _stdcall WINAPI mciClose()
{
char szReturnString[9999];
char* lpszReturnString = &szReturnString[0];
SetTemporaryFilename();
MCIERROR status;
BOOL status2;
status = mciSendString("close capture", lpszReturnString, lstrlen(lpszReturnString), NULL);
status2 = mciGetErrorString(status, &ErrorText[0], 9999);
return ErrorText;
}
MARK D ROCKMAN