make_sine.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. ** Copyright (C) 1999-2012 Erik de Castro Lopo <[email protected]>
  3. **
  4. ** All rights reserved.
  5. **
  6. ** Redistribution and use in source and binary forms, with or without
  7. ** modification, are permitted provided that the following conditions are
  8. ** met:
  9. **
  10. ** * Redistributions of source code must retain the above copyright
  11. ** notice, this list of conditions and the following disclaimer.
  12. ** * Redistributions in binary form must reproduce the above copyright
  13. ** notice, this list of conditions and the following disclaimer in
  14. ** the documentation and/or other materials provided with the
  15. ** distribution.
  16. ** * Neither the author nor the names of any contributors may be used
  17. ** to endorse or promote products derived from this software without
  18. ** specific prior written permission.
  19. **
  20. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <math.h>
  36. #include <sndfile.h>
  37. #ifndef M_PI
  38. #define M_PI 3.14159265358979323846264338
  39. #endif
  40. #define SAMPLE_RATE 44100
  41. #define SAMPLE_COUNT (SAMPLE_RATE * 4) /* 4 seconds */
  42. #define AMPLITUDE (1.0 * 0x7F000000)
  43. #define LEFT_FREQ (344.0 / SAMPLE_RATE)
  44. #define RIGHT_FREQ (466.0 / SAMPLE_RATE)
  45. int
  46. main (void)
  47. { SNDFILE *file ;
  48. SF_INFO sfinfo ;
  49. int k ;
  50. int *buffer ;
  51. if (! (buffer = malloc (2 * SAMPLE_COUNT * sizeof (int))))
  52. { printf ("Error : Malloc failed.\n") ;
  53. return 1 ;
  54. } ;
  55. memset (&sfinfo, 0, sizeof (sfinfo)) ;
  56. sfinfo.samplerate = SAMPLE_RATE ;
  57. sfinfo.frames = SAMPLE_COUNT ;
  58. sfinfo.channels = 2 ;
  59. sfinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_24) ;
  60. if (! (file = sf_open ("sine.wav", SFM_WRITE, &sfinfo)))
  61. { printf ("Error : Not able to open output file.\n") ;
  62. free (buffer) ;
  63. return 1 ;
  64. } ;
  65. if (sfinfo.channels == 1)
  66. { for (k = 0 ; k < SAMPLE_COUNT ; k++)
  67. buffer [k] = (int) (AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI)) ;
  68. }
  69. else if (sfinfo.channels == 2)
  70. { for (k = 0 ; k < SAMPLE_COUNT ; k++)
  71. { buffer [2 * k] = (int) (AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI)) ;
  72. buffer [2 * k + 1] = (int) (AMPLITUDE * sin (RIGHT_FREQ * 2 * k * M_PI)) ;
  73. } ;
  74. }
  75. else
  76. { printf ("Error : make_sine can only generate mono or stereo files.\n") ;
  77. sf_close (file) ;
  78. free (buffer) ;
  79. return 1 ;
  80. } ;
  81. if (sf_write_int (file, buffer, sfinfo.channels * SAMPLE_COUNT) !=
  82. sfinfo.channels * SAMPLE_COUNT)
  83. puts (sf_strerror (file)) ;
  84. sf_close (file) ;
  85. free (buffer) ;
  86. return 0 ;
  87. } /* main */