⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 softmodem1.h

📁 软件实现(模拟)Modem操作. 使用C++ buider实现. 简单的例子.
💻 H
字号:
//---------------------------------------------------------------------------
//
//      Title : Software Modem Transmitter
//
//      Author :  Matthew Edmonds      2108209G
//      Date : 29 May 2003
//---------------------------------------------------------------------------
#ifndef Softmodem1H
#define Softmodem1H
//---------------------------------------------------------------------------
#define MAX_CHARACTERS 15
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
#include <ComCtrls.hpp>
#include <stdio.h>
#include <ctype.h>
#include <Menus.hpp>
#include <dsound.h>
#include <math.h>
#include <fstream.h>

using std::ifstream;
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TButton *Button1;
    TButton *Button2;
    TLabel *Label1;
    TLabel *Label2;
    TLabel *Label3;
    TRichEdit *RichEdit1;
  TMainMenu *MainMenu1;
  TMenuItem *File1;
  TMenuItem *Exit1;
  TMenuItem *Help1;
  TMenuItem *About1;
  TRichEdit *RichEdit2;
    TLabel *Label4;
    void __fastcall Button1Click(TObject *Sender);
  void __fastcall Exit1Click(TObject *Sender);
  void __fastcall About1Click(TObject *Sender);
  void __fastcall Button2Click(TObject *Sender);
  void __fastcall FormActivate(TObject *Sender);
private:	// User declarations
public:		// User declarations
      int chars;  // Used to store the character count
    __fastcall TForm1(TComponent* Owner);
      short int structdata[MAX_CHARACTERS];
      int count;
      //This struct holds the data that is typed into the
      //Editbox, as well as the converted binary data
      typedef struct
        {
          char chr;   //For storing typed characters
          short int mc[8]; //For storing binary conversion of typed characters
        }FD;
      FD filedata[MAX_CHARACTERS];
      int LoadFile(FD filedata[]);
      void readfile(int chars, FD filedata[]);
      int Direct_sound(int chars, FD filedata[]);
      HWND Main_Form_Handle;  //Holds the main window handle
};
      LPDIRECTSOUND         lpds;      // Direct Sound object
      LPDIRECTSOUNDBUFFER   lpdsbuffer;   //sound buffer
      DSBUFFERDESC          dsbd;   //DirectSound buffer description
      WAVEFORMATEX          pcmwf;    // Holds format description


//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
int TForm1::LoadFile(FD filedata[])
{
    //Opens the named file, reads in the characters one at a time into
    //the filedata structure

    int i=0; //Used for filedata element count
    char next;
    ifstream instream;  //reading from file

    instream.open("transmit.txt");
    if(instream.fail())
      {
      Application->MessageBoxA("Error - File not opened", "File Error", MB_OK);
      exit(1); //Error opening file
      }
    instream.get(next);
    while((!instream.eof())&&(i<MAX_CHARACTERS)&&(next!='\n'))  // Not at the end of file
    {
      filedata[i].chr = next;
      instream.get(next);
      i++;
    }

    instream.close();
    return i;
}

void TForm1::readfile(int chars, FD filedata[])
{
        //Reads the characters from the structure and
        //converts them to binary
        int i;
        int temp;
        int n;


        i=0;
        temp=0;

        for(i=0; i < chars; i++)
        {
          fflush(stdout);
          n=0;
          temp=0;
          temp=toascii(filedata[i].chr);

          do
          {
          // get the first binary digit (back to front)
          filedata[i].mc[n]=temp%2;
          temp=temp/2;
          n++;
          // next binary digit
          }while((temp!=0)&&(n<8));
        // next character
        }
}

int TForm1::Direct_sound(int chars, FD filedata[])
{

  int i; // variable used for count to display binary
  int n; // variable used to count elements in 8 bit increments
  int m=0;
  // this function will initialise the direct sound object
  // and fill the sound buffer with the synthesized sound
  // wave determined by the binary data read

  // The audio_ptr is used to lock the memory
  void *audio_ptr_1 = NULL;  //play pointer
  void *audio_ptr_2 = NULL;  //write pointer

  HRESULT hresult = 0;
  DWORD dsbstatus = 0;    // Status of the sound buffer

  DWORD audio_length_1 = 0;   // length of locked memory
  DWORD audio_length_2 = 0;
  LONG snd_buffer_length = 80000; //working buffer

  // allocate memory for buffer
  UCHAR *snd_buffer_ptr = (UCHAR *)malloc(snd_buffer_length);

  short int test[255];
  for(int k =0; k < 8; k++)
  {
    test[k] = 0;
  }

  for(i=0;i < chars; i++)  // chars is a character count of the text
  {
    for(n=7;n >= 0;n--)
      {
      // The array holds a string of binary bits
      // The bits are taken from the filedata structure
      // which holds the converted character data
      // Using a string speeds up the process of writing to
      // the secondary sound buffer
      test[m] = filedata[i].mc[n];
      m++;
      }
   }

   int index = 0;
   int index2 = 0;
   int a = 0;
   int b = 0;

   for (b=0; b<(chars*8); b++)
   {
        if (test[b]==0)
        {
            for(a = index; index < (500 + a); index++)
          {
           snd_buffer_ptr[index] = 127*sin(6.28*((float)(index%22))/(float)22);
          }
          index2 = index;
         }
         else
         {
            for(a = index2; index2 < (500 + a); index2++)
          {
           snd_buffer_ptr[index2] = 127*sin(6.28*((float)(index2%3))/(float)3);
          }
          index = index2;
         }
   }

//create the direct sound object
  hresult=DirectSoundCreate(NULL,&lpds,NULL);
  if(hresult!=DS_OK)
  {
    return(0);
  }

  //set cooperation level
  hresult=lpds->SetCooperativeLevel(Main_Form_Handle,DSSCL_NORMAL);
  if(hresult!=DS_OK)
  {
    return(0);
  }

  //set up the format data structure
  memset(&pcmwf, 0, sizeof(WAVEFORMATEX));

  pcmwf.wFormatTag = WAVE_FORMAT_PCM;
  pcmwf.nChannels  = 1;
  pcmwf.nSamplesPerSec = 11025;
  pcmwf.nBlockAlign = 1;
  pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
  pcmwf.wBitsPerSample = 8;
  pcmwf.cbSize = 0;

  //create the secondary buffer
  memset(&dsbd, 0, sizeof(DSBUFFERDESC));
  dsbd.dwSize = sizeof(DSBUFFERDESC);
  dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE | DSBCAPS_GLOBALFOCUS;
  dsbd.dwBufferBytes = snd_buffer_length + 1;
  dsbd.lpwfxFormat = &pcmwf;

  hresult=lpds->CreateSoundBuffer(&dsbd,&lpdsbuffer,NULL);
  if(hresult!=DS_OK)
  {
    return(0);
  }

  //copy data into soundbuffer
  hresult=lpdsbuffer->Lock(0, 80000,
                      &audio_ptr_1,
                      &audio_length_1,
                      &audio_ptr_2,
                      &audio_length_2,
                      DSBLOCK_FROMWRITECURSOR);
  if(hresult!=DS_OK)
  {
  return(0);
  }

  //copy first section of circular buffer
  CopyMemory(audio_ptr_1, snd_buffer_ptr, audio_length_1);

  //copy last section of circular buffer
  CopyMemory(audio_ptr_2, (snd_buffer_ptr+audio_length_1), audio_length_2);

  //unlock the buffer
  hresult=lpdsbuffer->Unlock(audio_ptr_1, audio_length_1, audio_ptr_2, audio_length_2);
  if(hresult!=DS_OK)
  {
    return(0);
  }

  //play the sound in once only mode for testing
  hresult=lpdsbuffer->Play(0,0,0); //Play Once mode set
  if(hresult!=DS_OK)
    {
    lpdsbuffer->GetStatus(&dsbstatus);
    return(0);
    }

  //release the memory since DirectSound has copied it
  free(snd_buffer_ptr);

  //return success
  return(1);

}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -