annotate src/libvorbis-1.3.3/test/write_read.c @ 22:b07fe9e906dc

Portaudio: add missed file
author Chris Cannam
date Tue, 26 Mar 2013 12:14:11 +0000
parents 05aa0afa9217
children
rev   line source
Chris@1 1 /********************************************************************
Chris@1 2 * *
Chris@1 3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
Chris@1 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
Chris@1 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
Chris@1 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
Chris@1 7 * *
Chris@1 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
Chris@1 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
Chris@1 10 * *
Chris@1 11 ********************************************************************
Chris@1 12
Chris@1 13 function: utility functions for vorbis codec test suite.
Chris@1 14 last mod: $Id: util.c 13293 2007-07-24 00:09:47Z erikd $
Chris@1 15
Chris@1 16 ********************************************************************/
Chris@1 17
Chris@1 18 #include <stdio.h>
Chris@1 19 #include <stdlib.h>
Chris@1 20 #include <math.h>
Chris@1 21 #include <string.h>
Chris@1 22 #include <errno.h>
Chris@1 23
Chris@1 24 #include <vorbis/codec.h>
Chris@1 25 #include <vorbis/vorbisenc.h>
Chris@1 26
Chris@1 27 #include "write_read.h"
Chris@1 28
Chris@1 29 /* The following function is basically a hacked version of the code in
Chris@1 30 * examples/encoder_example.c */
Chris@1 31 void
Chris@1 32 write_vorbis_data_or_die (const char *filename, int srate, float q, const float * data, int count, int ch)
Chris@1 33 {
Chris@1 34 FILE * file ;
Chris@1 35 ogg_stream_state os;
Chris@1 36 ogg_page og;
Chris@1 37 ogg_packet op;
Chris@1 38 vorbis_info vi;
Chris@1 39 vorbis_comment vc;
Chris@1 40 vorbis_dsp_state vd;
Chris@1 41 vorbis_block vb;
Chris@1 42
Chris@1 43 int eos = 0, ret;
Chris@1 44
Chris@1 45 if ((file = fopen (filename, "wb")) == NULL) {
Chris@1 46 printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
Chris@1 47 exit (1) ;
Chris@1 48 }
Chris@1 49
Chris@1 50 /********** Encode setup ************/
Chris@1 51
Chris@1 52 vorbis_info_init (&vi);
Chris@1 53
Chris@1 54 ret = vorbis_encode_init_vbr (&vi,ch,srate,q);
Chris@1 55 if (ret) {
Chris@1 56 printf ("vorbis_encode_init_vbr return %d\n", ret) ;
Chris@1 57 exit (1) ;
Chris@1 58 }
Chris@1 59
Chris@1 60 vorbis_comment_init (&vc);
Chris@1 61 vorbis_comment_add_tag (&vc,"ENCODER","test/util.c");
Chris@1 62 vorbis_analysis_init (&vd,&vi);
Chris@1 63 vorbis_block_init (&vd,&vb);
Chris@1 64
Chris@1 65 ogg_stream_init (&os,12345678);
Chris@1 66
Chris@1 67 {
Chris@1 68 ogg_packet header;
Chris@1 69 ogg_packet header_comm;
Chris@1 70 ogg_packet header_code;
Chris@1 71
Chris@1 72 vorbis_analysis_headerout (&vd,&vc,&header,&header_comm,&header_code);
Chris@1 73 ogg_stream_packetin (&os,&header);
Chris@1 74 ogg_stream_packetin (&os,&header_comm);
Chris@1 75 ogg_stream_packetin (&os,&header_code);
Chris@1 76
Chris@1 77 /* Ensures the audio data will start on a new page. */
Chris@1 78 while (!eos){
Chris@1 79 int result = ogg_stream_flush (&os,&og);
Chris@1 80 if (result == 0)
Chris@1 81 break;
Chris@1 82 fwrite (og.header,1,og.header_len,file);
Chris@1 83 fwrite (og.body,1,og.body_len,file);
Chris@1 84 }
Chris@1 85
Chris@1 86 }
Chris@1 87
Chris@1 88 {
Chris@1 89 /* expose the buffer to submit data */
Chris@1 90 float **buffer = vorbis_analysis_buffer (&vd,count);
Chris@1 91 int i;
Chris@1 92
Chris@1 93 for(i=0;i<ch;i++)
Chris@1 94 memcpy (buffer [i], data, count * sizeof (float)) ;
Chris@1 95
Chris@1 96 /* tell the library how much we actually submitted */
Chris@1 97 vorbis_analysis_wrote (&vd,count);
Chris@1 98 vorbis_analysis_wrote (&vd,0);
Chris@1 99 }
Chris@1 100
Chris@1 101 while (vorbis_analysis_blockout (&vd,&vb) == 1) {
Chris@1 102 vorbis_analysis (&vb,NULL);
Chris@1 103 vorbis_bitrate_addblock (&vb);
Chris@1 104
Chris@1 105 while (vorbis_bitrate_flushpacket (&vd,&op)) {
Chris@1 106 ogg_stream_packetin (&os,&op);
Chris@1 107
Chris@1 108 while (!eos) {
Chris@1 109 int result = ogg_stream_pageout (&os,&og);
Chris@1 110 if (result == 0)
Chris@1 111 break;
Chris@1 112 fwrite (og.header,1,og.header_len,file);
Chris@1 113 fwrite (og.body,1,og.body_len,file);
Chris@1 114
Chris@1 115 if (ogg_page_eos (&og))
Chris@1 116 eos = 1;
Chris@1 117 }
Chris@1 118 }
Chris@1 119 }
Chris@1 120
Chris@1 121 ogg_stream_clear (&os);
Chris@1 122 vorbis_block_clear (&vb);
Chris@1 123 vorbis_dsp_clear (&vd);
Chris@1 124 vorbis_comment_clear (&vc);
Chris@1 125 vorbis_info_clear (&vi);
Chris@1 126
Chris@1 127 fclose (file) ;
Chris@1 128 }
Chris@1 129
Chris@1 130 /* The following function is basically a hacked version of the code in
Chris@1 131 * examples/decoder_example.c */
Chris@1 132 void
Chris@1 133 read_vorbis_data_or_die (const char *filename, int srate, float * data, int count)
Chris@1 134 {
Chris@1 135 ogg_sync_state oy;
Chris@1 136 ogg_stream_state os;
Chris@1 137 ogg_page og;
Chris@1 138 ogg_packet op;
Chris@1 139
Chris@1 140 vorbis_info vi;
Chris@1 141 vorbis_comment vc;
Chris@1 142 vorbis_dsp_state vd;
Chris@1 143 vorbis_block vb;
Chris@1 144
Chris@1 145 FILE *file;
Chris@1 146 char *buffer;
Chris@1 147 int bytes;
Chris@1 148 int eos = 0;
Chris@1 149 int i;
Chris@1 150 int read_total = 0 ;
Chris@1 151
Chris@1 152 if ((file = fopen (filename, "rb")) == NULL) {
Chris@1 153 printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
Chris@1 154 exit (1) ;
Chris@1 155 }
Chris@1 156
Chris@1 157 ogg_sync_init (&oy);
Chris@1 158
Chris@1 159 {
Chris@1 160 /* fragile! Assumes all of our headers will fit in the first 8kB,
Chris@1 161 which currently they will */
Chris@1 162 buffer = ogg_sync_buffer (&oy,8192);
Chris@1 163 bytes = fread (buffer,1,8192,file);
Chris@1 164 ogg_sync_wrote (&oy,bytes);
Chris@1 165
Chris@1 166 if(ogg_sync_pageout (&oy,&og) != 1) {
Chris@1 167 if(bytes < 8192) {
Chris@1 168 printf ("Out of data.\n") ;
Chris@1 169 goto done_decode ;
Chris@1 170 }
Chris@1 171
Chris@1 172 fprintf (stderr,"Input does not appear to be an Ogg bitstream.\n");
Chris@1 173 exit (1);
Chris@1 174 }
Chris@1 175
Chris@1 176 ogg_stream_init (&os,ogg_page_serialno(&og));
Chris@1 177
Chris@1 178 vorbis_info_init (&vi);
Chris@1 179 vorbis_comment_init (&vc);
Chris@1 180 if (ogg_stream_pagein (&os,&og) < 0) {
Chris@1 181 fprintf (stderr,"Error reading first page of Ogg bitstream data.\n");
Chris@1 182 exit (1);
Chris@1 183 }
Chris@1 184
Chris@1 185 if (ogg_stream_packetout(&os,&op) != 1) {
Chris@1 186 fprintf (stderr,"Error reading initial header packet.\n");
Chris@1 187 exit (1);
Chris@1 188 }
Chris@1 189
Chris@1 190 if (vorbis_synthesis_headerin (&vi,&vc,&op) < 0) {
Chris@1 191 fprintf (stderr,"This Ogg bitstream does not contain Vorbis "
Chris@1 192 "audio data.\n");
Chris@1 193 exit (1);
Chris@1 194 }
Chris@1 195
Chris@1 196 i = 0;
Chris@1 197 while ( i < 2) {
Chris@1 198 while (i < 2) {
Chris@1 199
Chris@1 200 int result = ogg_sync_pageout (&oy,&og);
Chris@1 201 if(result == 0)
Chris@1 202 break;
Chris@1 203 if(result==1) {
Chris@1 204 ogg_stream_pagein(&os,&og);
Chris@1 205
Chris@1 206 while (i < 2) {
Chris@1 207 result = ogg_stream_packetout (&os,&op);
Chris@1 208 if (result == 0) break;
Chris@1 209 if (result < 0) {
Chris@1 210 fprintf (stderr,"Corrupt secondary header. Exiting.\n");
Chris@1 211 exit(1);
Chris@1 212 }
Chris@1 213 vorbis_synthesis_headerin (&vi,&vc,&op);
Chris@1 214 i++;
Chris@1 215 }
Chris@1 216 }
Chris@1 217 }
Chris@1 218
Chris@1 219 buffer = ogg_sync_buffer (&oy,4096);
Chris@1 220 bytes = fread (buffer,1,4096,file);
Chris@1 221 if (bytes == 0 && i < 2) {
Chris@1 222 fprintf (stderr,"End of file before finding all Vorbis headers!\n");
Chris@1 223 exit (1);
Chris@1 224 }
Chris@1 225
Chris@1 226 ogg_sync_wrote (&oy,bytes);
Chris@1 227 }
Chris@1 228
Chris@1 229 if (vi.rate != srate) {
Chris@1 230 printf ("\n\nError : File '%s' has sample rate of %ld when it should be %d.\n\n", filename, vi.rate, srate);
Chris@1 231 exit (1) ;
Chris@1 232 }
Chris@1 233
Chris@1 234 vorbis_synthesis_init (&vd,&vi);
Chris@1 235 vorbis_block_init (&vd,&vb);
Chris@1 236
Chris@1 237 while(!eos) {
Chris@1 238 while (!eos) {
Chris@1 239 int result = ogg_sync_pageout (&oy,&og);
Chris@1 240 if (result == 0)
Chris@1 241 break;
Chris@1 242 if (result < 0) {
Chris@1 243 fprintf (stderr,"Corrupt or missing data in bitstream; "
Chris@1 244 "continuing...\n");
Chris@1 245 } else {
Chris@1 246 ogg_stream_pagein (&os,&og);
Chris@1 247 while (1) {
Chris@1 248 result = ogg_stream_packetout (&os,&op);
Chris@1 249
Chris@1 250 if (result == 0)
Chris@1 251 break;
Chris@1 252 if (result < 0) {
Chris@1 253 /* no reason to complain; already complained above */
Chris@1 254 } else {
Chris@1 255 float **pcm;
Chris@1 256 int samples;
Chris@1 257
Chris@1 258 if (vorbis_synthesis (&vb,&op) == 0)
Chris@1 259 vorbis_synthesis_blockin(&vd,&vb);
Chris@1 260 while ((samples = vorbis_synthesis_pcmout (&vd,&pcm)) > 0 && read_total < count) {
Chris@1 261 int bout = samples < count ? samples : count;
Chris@1 262 bout = read_total + bout > count ? count - read_total : bout;
Chris@1 263
Chris@1 264 memcpy (data + read_total, pcm[0], bout * sizeof (float)) ;
Chris@1 265
Chris@1 266 vorbis_synthesis_read (&vd,bout);
Chris@1 267 read_total += bout ;
Chris@1 268 }
Chris@1 269 }
Chris@1 270 }
Chris@1 271
Chris@1 272 if (ogg_page_eos (&og)) eos = 1;
Chris@1 273 }
Chris@1 274 }
Chris@1 275
Chris@1 276 if (!eos) {
Chris@1 277 buffer = ogg_sync_buffer (&oy,4096);
Chris@1 278 bytes = fread (buffer,1,4096,file);
Chris@1 279 ogg_sync_wrote (&oy,bytes);
Chris@1 280 if (bytes == 0) eos = 1;
Chris@1 281 }
Chris@1 282 }
Chris@1 283
Chris@1 284 ogg_stream_clear (&os);
Chris@1 285
Chris@1 286 vorbis_block_clear (&vb);
Chris@1 287 vorbis_dsp_clear (&vd);
Chris@1 288 vorbis_comment_clear (&vc);
Chris@1 289 vorbis_info_clear (&vi);
Chris@1 290 }
Chris@1 291 done_decode:
Chris@1 292
Chris@1 293 /* OK, clean up the framer */
Chris@1 294 ogg_sync_clear (&oy);
Chris@1 295
Chris@1 296 fclose (file) ;
Chris@1 297 }
Chris@1 298