2008-01-31 07:04:26 +01:00
/*
* vgmstream . h - definitions for VGMSTREAM , encapsulating a multi - channel , looped audio stream
*/
# ifndef _VGMSTREAM_H
# define _VGMSTREAM_H
2008-07-05 13:49:29 +02:00
/* Vorbis and MPEG decoding are done by external libraries.
* If someone wants to do a standalone build , they can do it by simply
* removing these defines ( and the references to the libraries in the
* Makefile ) */
2008-06-15 06:01:03 +02:00
# define VGM_USE_VORBIS
2008-07-05 13:49:29 +02:00
# define VGM_USE_MPEG
2008-06-15 06:01:03 +02:00
2008-05-06 05:35:37 +02:00
# include "streamfile.h"
# include "coding/g72x_state.h"
2008-06-15 06:01:03 +02:00
# ifdef VGM_USE_VORBIS
# include <vorbis/vorbisfile.h>
# endif
2008-07-05 13:49:29 +02:00
# ifdef VGM_USE_MPEG
# include <mpg123.h>
# endif
2008-05-06 05:35:37 +02:00
2008-01-31 07:04:26 +01:00
/* The encoding type specifies the format the sound data itself takes */
typedef enum {
/* 16-bit PCM */
coding_PCM16BE , /* big endian 16-bit PCM */
coding_PCM16LE , /* little endian 16-bit PCM */
/* 8-bit PCM */
coding_PCM8 , /* 8-bit PCM */
/* 4-bit ADPCM */
coding_NDS_IMA , /* IMA ADPCM w/ NDS layout */
coding_CRI_ADX , /* CRI ADX */
coding_NGC_DSP , /* NGC ADPCM, called DSP */
2008-02-13 15:31:21 +01:00
coding_NGC_DTK , /* NGC hardware disc ADPCM, called DTK, TRK or ADP */
2008-02-14 23:10:08 +01:00
coding_G721 , /* CCITT G.721 ADPCM */
2008-03-03 22:38:11 +01:00
coding_NGC_AFC , /* NGC ADPCM, called AFC */
2008-05-04 22:36:40 +02:00
coding_PSX , /* PSX & PS2 ADPCM */
2008-06-25 22:39:15 +02:00
coding_invert_PSX , /* PSX ADPCM with first byte of frame inverted */
2008-05-10 21:59:29 +02:00
coding_XA , /* PSX CD-XA */
2008-05-24 00:52:02 +02:00
coding_XBOX , /* XBOX IMA */
2008-06-02 19:58:08 +02:00
coding_EAXA , /* EA/XA ADPCM */
2008-06-15 06:01:03 +02:00
# ifdef VGM_USE_VORBIS
coding_ogg_vorbis , /* vorbis */
# endif
2008-07-01 05:23:44 +02:00
coding_SDX2 , /* SDX2 2:1 Squareroot-Delta-Exact compression */
2008-07-03 04:20:52 +02:00
coding_DVI_IMA , /* DVI (bare IMA, high nibble first), aka ADP4 */
coding_IMA , /* bare IMA, low nibble first */
2008-07-03 23:21:01 +02:00
coding_WS , /* Westwood Studios' custom VBR ADPCM */
2008-07-05 13:49:29 +02:00
# ifdef VGM_USE_MPEG
2008-07-06 17:33:38 +02:00
coding_fake_MPEG2_L2 , /* MPEG-2 Layer 2 (AHX), with lying headers */
/* I don't even know offhand if all these combinations exist... */
coding_MPEG1_L1 ,
coding_MPEG1_L2 ,
coding_MPEG1_L3 , /* good ol' MPEG-1 Layer 3 (MP3) */
coding_MPEG2_L1 ,
coding_MPEG2_L2 ,
coding_MPEG2_L3 ,
coding_MPEG25_L1 ,
coding_MPEG25_L2 ,
coding_MPEG25_L3 ,
2008-07-05 13:49:29 +02:00
# endif
2008-01-31 07:04:26 +01:00
} coding_t ;
/* The layout type specifies how the sound data is laid out in the file */
typedef enum {
/* generic */
layout_none , /* straight data */
/* interleave */
layout_interleave , /* equal interleave throughout the stream */
layout_interleave_shortblock , /* interleave with a short last block */
2008-02-15 17:26:29 +01:00
#if 0
2008-01-31 07:04:26 +01:00
layout_interleave_byte , /* full byte interleave */
2008-03-04 01:46:55 +01:00
# endif
2008-01-31 07:04:26 +01:00
/* headered blocks */
2008-03-04 01:46:55 +01:00
layout_ast_blocked , /* .ast STRM with BLCK blocks*/
2008-03-04 08:15:25 +01:00
layout_halpst_blocked , /* blocks with HALPST-format header */
2008-05-10 21:59:29 +02:00
layout_xa_blocked ,
2008-06-02 19:58:08 +02:00
layout_ea_blocked ,
2008-06-03 20:41:26 +02:00
layout_caf_blocked ,
2008-06-29 03:30:13 +02:00
layout_wsi_blocked ,
2008-07-02 15:39:51 +02:00
layout_str_snds_blocked ,
2008-07-03 04:20:52 +02:00
layout_ws_aud_blocked ,
2008-03-04 01:46:55 +01:00
#if 0
2008-01-31 07:04:26 +01:00
layout_strm_blocked , /* */
2008-02-15 17:26:29 +01:00
# endif
2008-02-13 15:31:21 +01:00
/* otherwise odd */
layout_dtk_interleave , /* dtk interleaves channels by nibble */
2008-06-15 06:01:03 +02:00
# ifdef VGM_USE_VORBIS
layout_ogg_vorbis , /* ogg vorbis file */
# endif
2008-07-05 13:49:29 +02:00
# ifdef VGM_USE_MPEG
layout_fake_mpeg , /* MPEG audio stream with bad frame headers (AHX) */
2008-07-06 17:33:38 +02:00
layout_mpeg , /* proper MPEG audio stream */
2008-07-05 13:49:29 +02:00
# endif
2008-01-31 07:04:26 +01:00
} layout_t ;
/* The meta type specifies how we know what we know about the file. We may know because of a header we read, some of it may have been guessed from filenames, etc. */
typedef enum {
/* DSP-specific */
meta_DSP_STD , /* standard GC ADPCM (DSP) header */
meta_DSP_CSTR , /* Star Fox Assault "Cstr" */
meta_DSP_RS03 , /* Metroid Prime 2 "RS03" */
meta_DSP_STM , /* Paper Mario 2 STM */
meta_DSP_HALP , /* SSB:M "HALPST" */
2008-05-15 00:26:44 +02:00
meta_DSP_AGSC , /* Metroid Prime 2 title */
meta_DSP_MPDSP , /* Monopoly Party single header stereo */
2008-05-16 00:06:33 +02:00
meta_DSP_JETTERS , /* Bomberman Jetters .dsp */
meta_DSP_MSS ,
meta_DSP_GCM ,
2008-06-15 11:23:34 +02:00
meta_DSP_STR , /* Conan .str files */
meta_DSP_SADB , /* .sad */
2008-06-29 03:30:13 +02:00
meta_DSP_WSI , /* .wsi */
2008-07-06 13:24:14 +02:00
meta_DSP_AMTS , /* .amts */
2008-05-28 13:36:17 +02:00
2008-01-31 07:04:26 +01:00
/* Nintendo */
meta_STRM , /* STRM */
meta_RSTM , /* RSTM (similar to STRM) */
2008-03-03 22:38:11 +01:00
meta_AFC , /* AFC */
2008-03-04 01:46:55 +01:00
meta_AST , /* AST */
2008-05-06 03:01:06 +02:00
meta_RWSD , /* single-stream RWSD */
2008-05-17 01:14:47 +02:00
meta_RSTM_SPM , /* RSTM with 44->22khz hack */
2008-01-31 07:04:26 +01:00
/* CRI ADX */
meta_ADX_03 , /* ADX "type 03" */
meta_ADX_04 , /* ADX "type 04" */
2008-05-03 21:44:49 +02:00
meta_ADX_05 , /* ADX "type 05" */
2008-01-31 07:04:26 +01:00
/* etc */
2008-02-14 23:10:08 +01:00
meta_NGC_ADPDTK , /* NGC DTK/ADP, no header (.adp) */
2008-01-31 07:04:26 +01:00
meta_kRAW , /* almost headerless PCM */
2008-02-14 23:10:08 +01:00
meta_RSF , /* Retro Studios RSF, no header (.rsf) */
2008-03-04 08:15:25 +01:00
meta_HALPST , /* HAL Labs HALPST */
2008-04-02 19:50:50 +02:00
meta_GCSW , /* GCSW (PCM) */
2008-06-03 20:41:26 +02:00
meta_CFN , /* Namco CAF Audio File */
2008-05-04 22:36:40 +02:00
2008-05-06 00:45:21 +02:00
meta_PS2_SShd , /* .ADS with SShd header */
meta_PS2_NPSF , /* Namco Production Sound File */
2008-05-11 20:11:55 +02:00
meta_PS2_RXW , /* Sony Arc The Lad Sound File */
2008-05-11 23:27:10 +02:00
meta_PS2_RAW , /* RAW Interleaved Format */
2008-05-13 13:47:51 +02:00
meta_PS2_EXST , /* Shadow of Colossus EXST */
2008-05-13 21:53:31 +02:00
meta_PS2_SVAG , /* Konami SVAG */
2008-05-14 22:44:19 +02:00
meta_PS2_MIB , /* MIB File */
meta_PS2_MIB_MIH , /* MIB File + MIH Header*/
2008-05-15 20:57:03 +02:00
meta_PS2_MIC , /* KOEI MIC File */
2008-05-17 23:52:40 +02:00
meta_PS2_VAGi , /* VAGi Interleaved File */
meta_PS2_VAGp , /* VAGp Mono File */
2008-05-20 20:09:05 +02:00
meta_PS2_VAGm , /* VAGp Mono File */
2008-05-17 23:52:40 +02:00
meta_PS2_pGAV , /* VAGp with Little Endian Header */
2008-05-19 13:39:30 +02:00
meta_PSX_GMS , /* GMS File (used in PS1 & PS2) */
2008-05-20 20:09:05 +02:00
meta_PS2_STR , /* Pacman STR+STH files */
2008-05-21 23:26:52 +02:00
meta_PS2_ILD , /* ILD File */
2008-05-22 21:08:49 +02:00
meta_PS2_PNB , /* PsychoNauts Bgm File */
2008-05-11 20:11:55 +02:00
meta_PSX_XA , /* CD-XA with RIFF header */
2008-05-28 13:36:17 +02:00
meta_PS2_VAGs , /* VAG Stereo from Kingdom Hearts */
2008-06-07 23:11:33 +02:00
meta_PS2_VPK , /* VPK Audio File */
2008-06-25 18:42:13 +02:00
meta_PS2_BMDX , /* Beatmania thing */
2008-07-06 14:23:37 +02:00
meta_PS2_IVB , /* Langrisser 3 IVB */
meta_PS2_SVS , /* Square SVS */
2008-07-12 17:20:39 +02:00
meta_XSS , /* Dino Crisis 3 */
meta_SL3 , /* Test Drive Unlimited */
meta_HGC1 , /* Knights of the Temple 2 */
meta_AUS , /* Variuos Capcom Games */
meta_RWS , /* Variuos Konami Games */
2008-05-10 21:59:29 +02:00
2008-05-24 00:52:02 +02:00
meta_XBOX_WAVM , /* XBOX WAVM File */
2008-05-24 17:11:32 +02:00
meta_XBOX_RIFF , /* XBOX RIFF/WAVE File */
2008-05-24 00:52:02 +02:00
2008-06-02 19:58:08 +02:00
meta_EAXA_R2 , /* EA XA Release 2 */
meta_EAXA_R3 , /* EA XA Release 3 */
meta_EAXA_PSX , /* EA with PSX ADPCM */
2008-05-17 19:26:20 +02:00
meta_RAW , /* RAW PCM file */
2008-06-10 03:20:54 +02:00
meta_GENH , /* generic header */
2008-06-15 06:01:03 +02:00
# ifdef VGM_USE_VORBIS
meta_ogg_vorbis , /* ogg vorbis */
# endif
2008-07-01 05:23:44 +02:00
meta_AIFC , /* Audio Interchange File Format AIFF-C */
2008-07-01 18:11:59 +02:00
meta_AIFF , /* Audio Interchange File Format */
2008-07-02 15:39:51 +02:00
meta_STR_SNDS , /* .str with SNDS blocks and SHDR header */
2008-07-03 04:20:52 +02:00
meta_WS_AUD , /* Westwood Studios .aud */
meta_WS_AUD_old , /* Westwood Studios .aud, old style */
2008-07-05 13:49:29 +02:00
# ifdef VGM_USE_MPEG
meta_AHX , /* CRI AHX header (same structure as ADX) */
# endif
2008-07-06 20:05:04 +02:00
meta_RIFF_WAVE , /* RIFF, for WAVs */
meta_RIFF_WAVE_POS , /* .wav + .pos for looping */
2008-07-11 02:41:21 +02:00
meta_NWA , /* Visual Art's NWA */
2008-07-11 08:34:09 +02:00
meta_NWA_NWAINFOINI , /* NWA w/ NWAINFO.INI for looping */
meta_NWA_GAMEEXEINI , /* NWA w/ Gameexe.ini for looping */
2008-01-31 07:04:26 +01:00
} meta_t ;
typedef struct {
STREAMFILE * streamfile ; /* file used by this channel */
off_t channel_start_offset ; /* where data for this channel begins */
off_t offset ; /* current location in the file */
2008-07-04 02:06:51 +02:00
off_t frame_header_offset ; /* offset of the current frame header (for WS) */
int samples_left_in_frame ; /* for WS */
2008-01-31 07:04:26 +01:00
/* format specific */
/* adpcm */
int16_t adpcm_coef [ 16 ] ; /* for formats with decode coefficients built in */
union {
int16_t adpcm_history1_16 ; /* previous sample */
int32_t adpcm_history1_32 ;
} ;
union {
int16_t adpcm_history2_16 ; /* previous previous sample */
int32_t adpcm_history2_32 ;
} ;
2008-02-05 10:21:20 +01:00
int adpcm_step_index ; /* for IMA */
2008-02-14 23:10:08 +01:00
struct g72x_state g72x_state ; /* state for G.721 decoder, sort of big but we
might as well keep it around */
2008-04-02 18:11:53 +02:00
# ifdef DEBUG
int samples_done ;
int16_t loop_history1 , loop_history2 ;
# endif
2008-01-31 07:04:26 +01:00
} VGMSTREAMCHANNEL ;
typedef struct {
/* basics */
int32_t num_samples ; /* the actual number of samples in this stream */
int32_t sample_rate ; /* sample rate in Hz */
int channels ; /* number of channels */
coding_t coding_type ; /* type of encoding */
layout_t layout_type ; /* type of layout for data */
meta_t meta_type ; /* how we know the metadata */
/* looping */
int loop_flag ; /* is this stream looped? */
int32_t loop_start_sample ; /* first sample of the loop (included in the loop) */
int32_t loop_end_sample ; /* last sample of the loop (not included in the loop) */
/* channels */
VGMSTREAMCHANNEL * ch ; /* pointer to array of channels */
2008-05-20 22:19:46 +02:00
/* channel copies */
2008-03-25 08:30:04 +01:00
VGMSTREAMCHANNEL * start_ch ; /* copies of channel status as they were at the beginning of the stream */
2008-01-31 07:04:26 +01:00
VGMSTREAMCHANNEL * loop_ch ; /* copies of channel status as they were at the loop point */
/* layout-specific */
int32_t current_sample ; /* number of samples we've passed */
int32_t samples_into_block ; /* number of samples into the current block */
/* interleave */
size_t interleave_block_size ; /* interleave for this file */
size_t interleave_smallblock_size ; /* smaller interleave for last block */
/* headered blocks */
off_t current_block_offset ; /* start of this block (offset of block header) */
size_t current_block_size ; /* size of the block we're in now */
off_t next_block_offset ; /* offset of header of the next block */
int hit_loop ; /* have we seen the loop yet? */
/* loop layout (saved values) */
int32_t loop_sample ; /* saved from current_sample, should be loop_start_sample... */
int32_t loop_samples_into_block ; /* saved from samples_into_block */
off_t loop_block_offset ; /* saved from current_block_offset */
size_t loop_block_size ; /* saved from current_block_size */
off_t loop_next_block_offset ; /* saved from next_block_offset */
2008-05-10 21:59:29 +02:00
uint8_t xa_channel ; /* Selected XA Channel */
2008-05-11 20:11:55 +02:00
int32_t xa_sector_length ; /* XA block */
2008-05-19 05:58:15 +02:00
2008-06-02 19:58:08 +02:00
uint8_t ea_big_endian ; /* Big Endian ? */
uint8_t ea_compression_type ;
uint8_t ea_compression_version ;
uint8_t ea_platform ;
2008-07-04 02:06:51 +02:00
int32_t ws_output_size ; /* output bytes for this block */
2008-07-03 23:21:01 +02:00
2008-05-19 05:58:15 +02:00
void * start_vgmstream ; /* a copy of the VGMSTREAM as it was at the beginning of the stream */
2008-06-15 06:01:03 +02:00
/* Data the codec needs for the whole stream. This is for codecs too
* different from vgmstream ' s structure to be reasonably shoehorned into
* using the ch structures .
* Note also that support must be added for resetting , looping and
* closing for every codec that uses this , as it will not be handled . */
void * codec_data ;
2008-01-31 07:04:26 +01:00
} VGMSTREAM ;
2008-06-15 06:01:03 +02:00
# ifdef VGM_USE_VORBIS
typedef struct {
STREAMFILE * streamfile ;
ogg_int64_t offset ;
ogg_int64_t size ;
} ogg_vorbis_streamfile ;
typedef struct {
OggVorbis_File ogg_vorbis_file ;
int bitstream ;
ogg_vorbis_streamfile ov_streamfile ;
} ogg_vorbis_codec_data ;
# endif
2008-07-05 13:49:29 +02:00
# ifdef VGM_USE_MPEG
# define AHX_EXPECTED_FRAME_SIZE 0x414
2008-07-06 17:33:38 +02:00
/* MPEG_BUFFER_SIZE should be >= AHX_EXPECTED_FRAME_SIZE */
# define MPEG_BUFFER_SIZE 0x1000
2008-07-05 13:49:29 +02:00
typedef struct {
2008-07-06 17:33:38 +02:00
uint8_t buffer [ MPEG_BUFFER_SIZE ] ;
2008-07-05 13:49:29 +02:00
int buffer_used ;
int buffer_full ;
2008-07-06 17:33:38 +02:00
size_t bytes_in_buffer ;
2008-07-05 13:49:29 +02:00
mpg123_handle * m ;
2008-07-06 17:33:38 +02:00
} mpeg_codec_data ;
2008-07-05 13:49:29 +02:00
# endif
2008-06-15 06:01:03 +02:00
2008-01-31 07:04:26 +01:00
/* do format detection, return pointer to a usable VGMSTREAM, or NULL on failure */
VGMSTREAM * init_vgmstream ( const char * const filename ) ;
2008-05-20 17:18:38 +02:00
VGMSTREAM * init_vgmstream_from_STREAMFILE ( STREAMFILE * streamFile ) ;
2008-03-25 08:30:04 +01:00
2008-05-19 05:58:15 +02:00
/* reset a VGMSTREAM to start of stream */
void reset_vgmstream ( VGMSTREAM * vgmstream ) ;
2008-01-31 07:04:26 +01:00
/* allocate a VGMSTREAM and channel stuff */
VGMSTREAM * allocate_vgmstream ( int channel_count , int looped ) ;
/* deallocate, close, etc. */
void close_vgmstream ( VGMSTREAM * vgmstream ) ;
2008-02-05 07:21:57 +01:00
/* calculate the number of samples to be played based on looping parameters */
2008-05-16 22:28:36 +02:00
int32_t get_vgmstream_play_samples ( double looptimes , double fadeseconds , double fadedelayseconds , VGMSTREAM * vgmstream ) ;
2008-01-31 07:04:26 +01:00
/* render! */
void render_vgmstream ( sample * buffer , int32_t sample_count , VGMSTREAM * vgmstream ) ;
2008-02-05 07:21:57 +01:00
/* smallest self-contained group of samples is a frame */
2008-02-05 03:17:35 +01:00
int get_vgmstream_samples_per_frame ( VGMSTREAM * vgmstream ) ;
2008-02-05 07:21:57 +01:00
/* number of bytes per frame */
2008-02-05 03:17:35 +01:00
int get_vgmstream_frame_size ( VGMSTREAM * vgmstream ) ;
2008-02-05 10:21:20 +01:00
/* in NDS IMA the frame size is the block size, so the last one is short */
int get_vgmstream_samples_per_shortframe ( VGMSTREAM * vgmstream ) ;
int get_vgmstream_shortframe_size ( VGMSTREAM * vgmstream ) ;
2008-02-05 03:17:35 +01:00
2008-02-05 07:21:57 +01:00
/* Assume that we have written samples_written into the buffer already, and we have samples_to_do consecutive
* samples ahead of us . Decode those samples into the buffer . */
2008-02-05 03:17:35 +01:00
void decode_vgmstream ( VGMSTREAM * vgmstream , int samples_written , int samples_to_do , sample * buffer ) ;
2008-02-05 07:21:57 +01:00
/* calculate number of consecutive samples to do (taking into account stopping for loop start and end) */
int vgmstream_samples_to_do ( int samples_this_block , int samples_per_frame , VGMSTREAM * vgmstream ) ;
/* Detect start and save values, also detect end and restore values. Only works on exact sample values.
* Returns 1 if loop was done . */
int vgmstream_do_loop ( VGMSTREAM * vgmstream ) ;
2008-03-25 08:30:04 +01:00
/* Write a description of the stream into array pointed by desc,
* which must be length bytes long . Will always be null - terminated if length > 0
*/
2008-03-11 02:27:59 +01:00
void describe_vgmstream ( VGMSTREAM * vgmstream , char * desc , int length ) ;
2008-02-05 07:44:44 +01:00
2008-03-25 08:30:04 +01:00
/* See if there is a second file which may be the second channel, given
* already opened mono opened_stream which was opened from filename .
* If a suitable file is found , open it and change opened_stream to a
* stereo stream . */
2008-05-20 17:18:38 +02:00
void try_dual_file_stereo ( VGMSTREAM * opened_stream , STREAMFILE * streamFile ) ;
2008-03-17 03:11:18 +01:00
2008-01-31 07:04:26 +01:00
# endif