wavep.c
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:5k
源码类别:

Symbian

开发平台:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK ***** 
  2.  * Version: RCSL 1.0/RPSL 1.0 
  3.  *  
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
  5.  *      
  6.  * The contents of this file, and the files included with this file, are 
  7.  * subject to the current version of the RealNetworks Public Source License 
  8.  * Version 1.0 (the "RPSL") available at 
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
  10.  * the file under the RealNetworks Community Source License Version 1.0 
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
  12.  * in which case the RCSL will apply. You may also obtain the license terms 
  13.  * directly from RealNetworks.  You may not use this file except in 
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
  16.  * RCSL for the rights, obligations and limitations governing use of the 
  17.  * contents of the file.  
  18.  *  
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the 
  20.  * developer of the Original Code and owns the copyrights in the portions 
  21.  * it created. 
  22.  *  
  23.  * This file, and the files included with this file, is distributed and made 
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  28.  * 
  29.  * Technology Compatibility Kit Test Suite(s) Location: 
  30.  *    http://www.helixcommunity.org/content/tck 
  31.  * 
  32.  * Contributor(s): 
  33.  *  
  34.  * ***** END LICENSE BLOCK ***** */ 
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include "hlxclib/float.h"
  38. #include <math.h>
  39. #include <io.h>
  40. #include "port.h"
  41. typedef struct {
  42. unsigned char riff[4]; /* Flawfinder: ignore */
  43. unsigned char size[4]; /* Flawfinder: ignore */
  44. unsigned char wave[4]; /* Flawfinder: ignore */
  45. unsigned char fmt[4]; /* Flawfinder: ignore */
  46. unsigned char fmtsize[4]; /* Flawfinder: ignore */
  47. unsigned char tag[2]; /* Flawfinder: ignore */
  48. unsigned char nChannels[2]; /* Flawfinder: ignore */
  49. unsigned char nSamplesPerSec[4]; /* Flawfinder: ignore */
  50. unsigned char nAvgBytesPerSec[4]; /* Flawfinder: ignore */
  51. unsigned char nBlockAlign[2];
  52. unsigned char nBitsPerSample[2];
  53. unsigned char data[4]; /* Flawfinder: ignore */
  54. unsigned char pcm_bytes[4]; /* Flawfinder: ignore */
  55. } BYTE_WAVE;
  56. static BYTE_WAVE wave = {
  57. "RIFF",
  58. sizeof(BYTE_WAVE) - 8, 0, 0, 0,
  59. "WAVE",
  60. "fmt ",
  61. 16, 0, 0, 0,
  62. 1, 0,
  63. 1, 0,
  64. 34, 86, 0, 0,   /* 86 * 256 + 34 = 22050 */
  65. 172, 68, 0, 0,  /* 172 * 256 + 68 = 44100 */
  66. 2, 0,
  67. 16, 0,
  68. "data",
  69. 0, 0, 0, 0
  70. };
  71. /*---------------------------------------------------------*/
  72. static void set_wave( unsigned char w[], int n, long x)
  73. {
  74. int i;
  75. for(i=0;i<n;i++) {
  76.   w[i] = (unsigned char)(x & 0xff);
  77.   x >>= 8;
  78. }
  79. }
  80. /*---------------------------------------------------------*/
  81. int write_pcm_header_wave( int handout,
  82.         long samprate, int channels, int bits, int type)
  83. {
  84. int nwrite;
  85.      if( type == 0 )  set_wave(wave.tag, sizeof(wave.tag), 1);
  86. else if( type == 10 ) set_wave(wave.tag, sizeof(wave.tag), 7);
  87. else return 0;
  88. set_wave(wave.size, sizeof(wave.size), sizeof(wave) - 8);
  89. set_wave(wave.nChannels, sizeof(wave.nChannels), channels);
  90. set_wave(wave.nSamplesPerSec, sizeof(wave.nSamplesPerSec), samprate);
  91. set_wave(wave.nAvgBytesPerSec, sizeof(wave.nAvgBytesPerSec),
  92.                                    (channels*samprate*bits + 7)/8);
  93. set_wave(wave.nBlockAlign, sizeof(wave.nBlockAlign), (channels*bits+7)/8);
  94. set_wave(wave.nBitsPerSample, sizeof(wave.nBitsPerSample), bits);
  95. set_wave(wave.pcm_bytes, sizeof(wave.pcm_bytes), 0);
  96. nwrite = write(handout, &wave, sizeof(wave));
  97. if( nwrite != sizeof(wave) )  return 0;
  98. return 1;
  99. }
  100. /*-----------------------------------------------*/
  101. int write_pcm_tailer_wave( int handout, unsigned long pcm_bytes)
  102. {
  103. unsigned long pos;
  104. int nwrite;
  105. set_wave(wave.size, sizeof(wave.size), sizeof(wave) - 8 + pcm_bytes);
  106. set_wave(wave.pcm_bytes, sizeof(wave.pcm_bytes), pcm_bytes);
  107. pos = lseek(handout, 0L, 2);   /*-- save current position */
  108. lseek(handout, 0L, 0);         /*-- pos to header --*/
  109. nwrite = write(handout, &wave, sizeof(wave));
  110. lseek(handout, pos, 0);        /*-- restore pos --*/
  111. if( nwrite != sizeof(wave) )  return 0;
  112. return 1;
  113. }
  114. /*-----------------------------------------------*/
  115. /*----------------------------------------------------------------
  116.   pcm conversion to wave format
  117.   This conversion code required for big endian machine, or,
  118.   if sizeof(short) != 16 bits.
  119.   Conversion routines may be used on any machine, but if
  120.   not required, the do nothing macros in port.h can be used instead
  121.   to reduce overhead.
  122. -----------------------------------------------------------------*/
  123. #ifndef LITTLE_SHORT16
  124. #include "wcvt.c"
  125. #endif
  126. /*-----------------------------------------------*/
  127. int cvt_to_wave_test()
  128. {
  129. /*-- test for valid compile ---*/
  130.   return sizeof(short) - 2;
  131. }
  132. /*-----------------------------------------------*/