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

📄 pcsound.c

📁 iar workbrench for scr16c
💻 C
字号:
/*
 *-----------------------------------------------------------------------------
 *                                                                             
 *               @@@@@@@      *   @@@@@@@@@           *                                     
 *              @       @             @               *                            
 *              @             *       @      ****     *                                 
 *               @@@@@@@      *       @     *    *    *                              
 *        ___---        @     *       @     ******    *                                 
 *  ___---      @       @     *       @     *         *                             
 *   -_          @@@@@@@  _   *       @      ****     *                               
 *     -_                 _ -                                                     
 *       -_          _ -                                                       
 *         -_   _ -        s   e   m   i   c   o   n   d   u   c   t   o   r 
 *           -                                                                    
 *                                                                               
 * (C) Copyright SiTel Semiconductor BV, unpublished work.
 * This computer program includes Confidential, Proprietary Information and
 * is a Trade Secret of SiTel Semiconductor BV.
 * All use, disclosure, and/or reproduction is prohibited unless authorized
 * in writing. All Rights Reserved.
 * This source-code is solely intended as example code. The code may only be 
 * used for study and demo purposes. SiTel Semiconductor can not be held liable 
 * for damages or project-delays caused by bugs in this code.
 *----------------------------------------------------------------------------
********************************************************************************
**
** Project:      Midi Sequencer
**
** Description   : PC sound driver
**
** Limitations   : Ansi-C
**
** Errors        : none
**
** Author        : Jose Mortensen 
**
********************************************************************************
**
** $Author: helsloot $
** $Date: Wed Apr 26 14:21:38 2006 $
** $Source: /services/syncdata/hazelaar/8500/server_vault/applab/14430/sw/midiplayer_combined/applications/src/pcsound.c.rca $
** $Revision: 1.2 $
** $State: Experimental $
**
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#include "pcsound.h"

#ifdef __NOSOUND__

   /*
    * Empty stubs when compiled with no sound
    */

   int init_sound(int fs) {
      return -1;
   }

   void playsnd(int audio_fd, short * buffer, size_t len){
   }

   void closesnd(int audio_fd)
   {
   }

#else // __NOSOUND__

#ifdef WIN32


   /* 
   * Sound subroutines under windows
   */

   WAVEHDR __mms_header1;
   WAVEHDR __mms_header2;
   short __mms_buffer1[PLAY_BUFFER_SIZE];
   short __mms_buffer2[PLAY_BUFFER_SIZE];

   /*
   * Initialize sound buffers 
   */
   HWAVEOUT init_sound(int fs) {

       HWAVEOUT hWaveOut; /* device handle */
       WAVEFORMATEX wfx; /* look this up in your documentation */


       /*
       * first we need to set up the WAVEFORMATEX structure. 
       * the structure describes the format of the audio.
       */
       wfx.nSamplesPerSec = fs; /* sample rate */
       wfx.wBitsPerSample = 16; /* sample size */
       wfx.nChannels = 1; /* channels*/
       /*
       * WAVEFORMATEX also has other fields which need filling.
       * as long as the three fields above are filled this should
       * work for any PCM (pulse code modulation) format.
       */
       wfx.cbSize = 0; /* size of _extra_ info */
       wfx.wFormatTag = WAVE_FORMAT_PCM;
       wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
       wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
       /*
       * try to open the default wave device. WAVE_MAPPER is
       * a constant defined in mmsystem.h, it always points to the
       * default wave device on the system (some people have 2 or
       * more sound cards).
       */
       if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, 
                      CALLBACK_NULL) != MMSYSERR_NOERROR) 
       {
           printf("unable to open WAVE_MAPPER device\n");
           return 0;
       }

       /*
       * device is now open so print the success message
       * and then close the device again.
       */
       printf("The Wave Mapper device was opened successfully!\n");

      /*
       * initialise the block header with the size
       * and pointer.
       */
       ZeroMemory(&__mms_header1, sizeof(WAVEHDR));
       __mms_header1.dwBufferLength = 2*PLAY_BUFFER_SIZE;
       __mms_header1.lpData = (LPSTR)__mms_buffer1;

       ZeroMemory(&__mms_header2, sizeof(WAVEHDR));
       __mms_header2.dwBufferLength = 2*PLAY_BUFFER_SIZE;
       __mms_header2.lpData = (LPSTR)__mms_buffer2;
       /*
       * prepare the block for playback
       */
       waveOutPrepareHeader(hWaveOut, &__mms_header1, sizeof(WAVEHDR));
       waveOutPrepareHeader(hWaveOut, &__mms_header2, sizeof(WAVEHDR));
    
       return hWaveOut;

   }

   /*
    * Play sound buffer
    */
   void playsnd(HWAVEOUT hWaveOut, short * buffer, size_t dummy)
   {
   
      static int buffptr = 1;

      if (hWaveOut != 0) {

      int i;
      /*
       * Use double buffer to store audio samples 
       * while the last buffer is being played.
       */
      if (buffptr == 1)
      {

         while(__mms_header1.dwFlags & WHDR_INQUEUE) Sleep(1);

         for (i=0; i<PLAY_BUFFER_SIZE; i++)
            __mms_buffer1[i] = buffer[i];

         waveOutWrite(hWaveOut, &__mms_header1, sizeof(WAVEHDR));
         buffptr = 2;

      } else
      {
         while(__mms_header2.dwFlags & WHDR_INQUEUE) Sleep(1);

         for (i=0; i<PLAY_BUFFER_SIZE; i++)
            __mms_buffer2[i] = buffer[i];

         waveOutWrite(hWaveOut, &__mms_header2, sizeof(WAVEHDR));
         buffptr = 1;
      }

      /*
       * write the block to the device. waveOutWrite returns immediately
       * unless a synchronous driver is used (not often).
       */
      }
   }

   /*
    * close sound
    */
   void closesound(HWAVEOUT hWaveOut)
   {
       /*
       * wait a while for the block to play then start trying
       * to unprepare the header. this will fail until the block has
       * played.
       */

      while(waveOutUnprepareHeader( hWaveOut, &__mms_header1, 
         sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);

      while(waveOutUnprepareHeader( hWaveOut, &__mms_header2, 
          sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
       
       
      waveOutClose(hWaveOut);
   }

#else // not WIN32


   /* 
    * LINUX sound handling routines
    * Setup device_name to the system's dsp 
    */
   HWAVEOUT init_sound(int fs)
   {
      const char * device_name="/dev/dsp";
      int format =  AFMT_S16_LE; 
      int stereo = 0;
      int audio_fd;
   
     // Open audio device 
     if ((audio_fd = open(device_name, O_WRONLY, 0)) == -1)
     {
       printf("Could not open device.\n");
       exit(1);
     }
     printf("Sound: Device opened.\n");

     if (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format) == -1)
     {
       printf("Could not set format.\n");
       exit(1);
     }
     printf("Sound: Format set.\n");

     if (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) == -1)
     {
       printf("Could not set Mono/stereo.\n");
       exit(1);
     }
     printf("Sound: Mono/Stereo set.\n");

     if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &fs) == -1)
     {
       printf("Could not set speed.\n");
       exit(1);
     }
     printf("Sound: Speed set. %d Hz\n", fs);
  
     return audio_fd;
  
   }

   /*
    * Play sound samples
    */
   void playsnd(HWAVEOUT audio_fd, short * buffer, size_t len)
   {
      if (write(audio_fd, buffer, 2*len) == -1) 
      {
         printf("Could not play sound\n");
         exit(1);
      }
   }

   /*
    * Close device
    */
   void closesound(HWAVEOUT audio_fd)
   {
          close(audio_fd);
   }

#endif //__WIN32__
#endif // __NOSOUND__

⌨️ 快捷键说明

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