Chris@1: /******************************************************************** Chris@1: * * Chris@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * Chris@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * Chris@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * Chris@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * Chris@1: * * Chris@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 * Chris@1: * by the Xiph.Org Foundation http://www.xiph.org/ * Chris@1: * * Chris@1: ******************************************************************** Chris@1: Chris@1: function: utility functions for vorbis codec test suite. Chris@1: last mod: $Id: util.c 13293 2007-07-24 00:09:47Z erikd $ Chris@1: Chris@1: ********************************************************************/ Chris@1: Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: Chris@1: #include Chris@1: #include Chris@1: Chris@1: #include "write_read.h" Chris@1: Chris@1: /* The following function is basically a hacked version of the code in Chris@1: * examples/encoder_example.c */ Chris@1: void Chris@1: write_vorbis_data_or_die (const char *filename, int srate, float q, const float * data, int count, int ch) Chris@1: { Chris@1: FILE * file ; Chris@1: ogg_stream_state os; Chris@1: ogg_page og; Chris@1: ogg_packet op; Chris@1: vorbis_info vi; Chris@1: vorbis_comment vc; Chris@1: vorbis_dsp_state vd; Chris@1: vorbis_block vb; Chris@1: Chris@1: int eos = 0, ret; Chris@1: Chris@1: if ((file = fopen (filename, "wb")) == NULL) { Chris@1: printf("\n\nError : fopen failed : %s\n", strerror (errno)) ; Chris@1: exit (1) ; Chris@1: } Chris@1: Chris@1: /********** Encode setup ************/ Chris@1: Chris@1: vorbis_info_init (&vi); Chris@1: Chris@1: ret = vorbis_encode_init_vbr (&vi,ch,srate,q); Chris@1: if (ret) { Chris@1: printf ("vorbis_encode_init_vbr return %d\n", ret) ; Chris@1: exit (1) ; Chris@1: } Chris@1: Chris@1: vorbis_comment_init (&vc); Chris@1: vorbis_comment_add_tag (&vc,"ENCODER","test/util.c"); Chris@1: vorbis_analysis_init (&vd,&vi); Chris@1: vorbis_block_init (&vd,&vb); Chris@1: Chris@1: ogg_stream_init (&os,12345678); Chris@1: Chris@1: { Chris@1: ogg_packet header; Chris@1: ogg_packet header_comm; Chris@1: ogg_packet header_code; Chris@1: Chris@1: vorbis_analysis_headerout (&vd,&vc,&header,&header_comm,&header_code); Chris@1: ogg_stream_packetin (&os,&header); Chris@1: ogg_stream_packetin (&os,&header_comm); Chris@1: ogg_stream_packetin (&os,&header_code); Chris@1: Chris@1: /* Ensures the audio data will start on a new page. */ Chris@1: while (!eos){ Chris@1: int result = ogg_stream_flush (&os,&og); Chris@1: if (result == 0) Chris@1: break; Chris@1: fwrite (og.header,1,og.header_len,file); Chris@1: fwrite (og.body,1,og.body_len,file); Chris@1: } Chris@1: Chris@1: } Chris@1: Chris@1: { Chris@1: /* expose the buffer to submit data */ Chris@1: float **buffer = vorbis_analysis_buffer (&vd,count); Chris@1: int i; Chris@1: Chris@1: for(i=0;i 0 && read_total < count) { Chris@1: int bout = samples < count ? samples : count; Chris@1: bout = read_total + bout > count ? count - read_total : bout; Chris@1: Chris@1: memcpy (data + read_total, pcm[0], bout * sizeof (float)) ; Chris@1: Chris@1: vorbis_synthesis_read (&vd,bout); Chris@1: read_total += bout ; Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: if (ogg_page_eos (&og)) eos = 1; Chris@1: } Chris@1: } Chris@1: Chris@1: if (!eos) { Chris@1: buffer = ogg_sync_buffer (&oy,4096); Chris@1: bytes = fread (buffer,1,4096,file); Chris@1: ogg_sync_wrote (&oy,bytes); Chris@1: if (bytes == 0) eos = 1; Chris@1: } Chris@1: } Chris@1: Chris@1: ogg_stream_clear (&os); Chris@1: Chris@1: vorbis_block_clear (&vb); Chris@1: vorbis_dsp_clear (&vd); Chris@1: vorbis_comment_clear (&vc); Chris@1: vorbis_info_clear (&vi); Chris@1: } Chris@1: done_decode: Chris@1: Chris@1: /* OK, clean up the framer */ Chris@1: ogg_sync_clear (&oy); Chris@1: Chris@1: fclose (file) ; Chris@1: } Chris@1: