oggfile.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright (C) 2009 Id Software, Inc.
  3. Copyright (C) 2004 Michael Liebscher
  4. Copyright (C) 1997-2001 Id Software, Inc.
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include "../wolfiphone.h"
  18. filehandle_t *fh;
  19. PRIVATE size_t ovc_read( void *ptr, size_t size, size_t nmemb, void *dataSource )
  20. {
  21. if( ! size || ! nmemb )
  22. {
  23. return 0;
  24. }
  25. return FS_ReadFile( ptr, size, nmemb, fh );
  26. }
  27. PRIVATE int ovc_seek( void *dataSource, ogg_int64_t offset, int whence )
  28. {
  29. return FS_FileSeek( fh, offset, whence );
  30. }
  31. PRIVATE int ovc_close( void *dataSource )
  32. {
  33. return 0;
  34. }
  35. PRIVATE long ovc_tell( void *dataSource )
  36. {
  37. return FS_FileTell( fh );
  38. }
  39. /*
  40. -----------------------------------------------------------------------------
  41. Function: LoadOggInfo -Load ogg file.
  42. Parameters: filename -[in] Name of wav file to load.
  43. wav -[out] wav data.
  44. info -[out] wav sound info.
  45. Returns: True if file loaded, otherwise false.
  46. Notes: Caller is responsible for freeing wav data by calling Z_Free.
  47. -----------------------------------------------------------------------------
  48. */
  49. PUBLIC _boolean LoadOggInfo( const char *filename, W8 **wav, soundInfo_t *info )
  50. {
  51. W8 *data;
  52. int size;
  53. int dummy;
  54. char *newFilename;
  55. int len;
  56. OggVorbis_File vorbisFile;
  57. vorbis_info vorbisInfo;
  58. ov_callbacks vorbisCallbacks = {ovc_read, ovc_seek, ovc_close, ovc_tell};
  59. int ret;
  60. newFilename = strdup( filename );
  61. len = strlen( newFilename );
  62. if ( len < 5 || strcmp( newFilename + len - 4, ".wav" ) ) {
  63. free( newFilename );
  64. return false;
  65. }
  66. newFilename[ len - 3 ] = 'o';
  67. newFilename[ len - 2 ] = 'g';
  68. newFilename[ len - 1 ] = 'g';
  69. fh = FS_OpenFile( newFilename, 0 );
  70. if( ! fh )
  71. {
  72. free( newFilename );
  73. return false;
  74. }
  75. if( (ret = ov_open_callbacks( fh, &vorbisFile, NULL, 0, vorbisCallbacks )) < 0 ) {
  76. free( newFilename );
  77. return false;
  78. }
  79. vorbisInfo = *ov_info( &vorbisFile, -1 );
  80. if( vorbisInfo.channels != 1 && vorbisInfo.channels != 2 )
  81. {
  82. Com_Printf( "Only mono and stereo OGG files supported (%s)\n", newFilename );
  83. free( newFilename );
  84. return false;
  85. }
  86. info->channels = vorbisInfo.channels;
  87. info->sample_rate = vorbisInfo.rate;
  88. info->sample_size = 2;
  89. #define BUFFER_SIZE ( 128 * 1024 )
  90. data = (W8 *)malloc( BUFFER_SIZE );
  91. size = 0;
  92. while( size < BUFFER_SIZE )
  93. {
  94. int read = 0;
  95. read = ov_read( &vorbisFile, (char *)data + size, BUFFER_SIZE - size, &dummy );
  96. if( read == 0 )
  97. {
  98. break;
  99. }
  100. if( read <= 0 )
  101. {
  102. Com_Printf( "Only mono and stereo OGG files supported (%s)\n", newFilename );
  103. free( newFilename );
  104. return false;
  105. }
  106. size += read;
  107. }
  108. info->samples = size / ( info->channels * info->sample_size );
  109. Com_Printf("Loaded %s: channels=%d, sample_rate=%d, sample_size=%d, samples=%d. \n", newFilename, info->channels, info->sample_rate, info->sample_size, info->samples );
  110. free( newFilename );
  111. *wav = (W8 *)Z_Malloc( size );
  112. memcpy(*wav, data, size );
  113. free( data );
  114. FS_CloseFile( fh );
  115. return true;
  116. }