From 74f4b1bc3b627ba4c7e03498234d88cacdfbe97b Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 29 Sep 2021 22:52:49 +0200 Subject: Squashed 'vorbis/' content from commit d22c3ab5f git-subtree-dir: vorbis git-subtree-split: d22c3ab5f633460abc2532feee60ca0892134cbf --- test/write_read.c | 297 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 test/write_read.c (limited to 'test/write_read.c') diff --git a/test/write_read.c b/test/write_read.c new file mode 100644 index 0000000..f1190dc --- /dev/null +++ b/test/write_read.c @@ -0,0 +1,297 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 * + * by the Xiph.Org Foundation http://www.xiph.org/ * + * * + ******************************************************************** + + function: utility functions for vorbis codec test suite. + + ********************************************************************/ + +#include +#include +#include +#include +#include + +#include +#include + +#include "write_read.h" + +/* The following function is basically a hacked version of the code in + * examples/encoder_example.c */ +void +write_vorbis_data_or_die (const char *filename, int srate, float q, const float * data, int count, int ch) +{ + FILE * file ; + ogg_stream_state os; + ogg_page og; + ogg_packet op; + vorbis_info vi; + vorbis_comment vc; + vorbis_dsp_state vd; + vorbis_block vb; + + int eos = 0, ret; + + if ((file = fopen (filename, "wb")) == NULL) { + printf("\n\nError : fopen failed : %s\n", strerror (errno)) ; + exit (1) ; + } + + /********** Encode setup ************/ + + vorbis_info_init (&vi); + + ret = vorbis_encode_init_vbr (&vi,ch,srate,q); + if (ret) { + printf ("vorbis_encode_init_vbr return %d\n", ret) ; + exit (1) ; + } + + vorbis_comment_init (&vc); + vorbis_comment_add_tag (&vc,"ENCODER","test/util.c"); + vorbis_analysis_init (&vd,&vi); + vorbis_block_init (&vd,&vb); + + ogg_stream_init (&os,12345678); + + { + ogg_packet header; + ogg_packet header_comm; + ogg_packet header_code; + + vorbis_analysis_headerout (&vd,&vc,&header,&header_comm,&header_code); + ogg_stream_packetin (&os,&header); + ogg_stream_packetin (&os,&header_comm); + ogg_stream_packetin (&os,&header_code); + + /* Ensures the audio data will start on a new page. */ + while (!eos){ + int result = ogg_stream_flush (&os,&og); + if (result == 0) + break; + fwrite (og.header,1,og.header_len,file); + fwrite (og.body,1,og.body_len,file); + } + + } + + { + /* expose the buffer to submit data */ + float **buffer = vorbis_analysis_buffer (&vd,count); + int i; + + for(i=0;i 0 && read_total < count) { + int bout = samples < count ? samples : count; + bout = read_total + bout > count ? count - read_total : bout; + + memcpy (data + read_total, pcm[0], bout * sizeof (float)) ; + + vorbis_synthesis_read (&vd,bout); + read_total += bout ; + } + } + } + + if (ogg_page_eos (&og)) eos = 1; + } + } + + if (!eos) { + buffer = ogg_sync_buffer (&oy,4096); + bytes = fread (buffer,1,4096,file); + ogg_sync_wrote (&oy,bytes); + if (bytes == 0) eos = 1; + } + } + + ogg_stream_clear (&os); + + vorbis_block_clear (&vb); + vorbis_dsp_clear (&vd); + vorbis_comment_clear (&vc); + vorbis_info_clear (&vi); + } +done_decode: + + /* OK, clean up the framer */ + ogg_sync_clear (&oy); + + fclose (file) ; +} + -- cgit v1.1