1 /******************************************************************************* 2 / 3 / File: SoundUtils.cpp 4 / 5 / Description: Utility functions for handling audio data. 6 / 7 / Copyright 1998-1999, Be Incorporated, All Rights Reserved 8 / 9 *******************************************************************************/ 10 11 #include "SoundUtils.h" 12 13 #include <cmath> 14 15 // These two conversions seem to pop up all the time in media code. 16 // I guess it's the curse of microsecond resolution... ;-) 17 double 18 us_to_s(bigtime_t usecs) 19 { 20 return (usecs / 1000000.0); 21 } 22 23 bigtime_t 24 s_to_us(double secs) 25 { 26 return (bigtime_t) (secs * 1000000.0); 27 } 28 29 int 30 bytes_per_frame( 31 const media_raw_audio_format & format) 32 { 33 // The media_raw_audio_format format constants encode the 34 // bytes-per-sample value in the low nybble. Having a fixed 35 // number of bytes-per-sample, and no inter-sample relationships, 36 // is what makes a format "raw". 37 int bytesPerSample = format.format & 0xf; 38 return bytesPerSample * format.channel_count; 39 } 40 41 int 42 frames_per_buffer( 43 const media_raw_audio_format & format) 44 { 45 // This will give us the number of full-sized frames that will fit 46 // in a buffer. (Remember, integer division automatically rounds 47 // down.) 48 int frames = 0; 49 if (bytes_per_frame(format) > 0) { 50 frames = format.buffer_size / bytes_per_frame(format); 51 } 52 return frames; 53 } 54 55 bigtime_t 56 buffer_duration( 57 const media_raw_audio_format & format) 58 { 59 // Figuring out duration is easy. We take extra precaution to 60 // not divide by zero or return irrelevant results. 61 bigtime_t duration = 0; 62 if (format.buffer_size > 0 && format.frame_rate > 0 && bytes_per_frame(format) > 0) { 63 // In these kinds of calculations, it's always useful to double-check 64 // the unit conversions. (Anyone remember high school physics?) 65 // bytes/(bytes/frame) / frames/sec 66 // = frames * sec/frames 67 // = secs which is what we want. 68 duration = s_to_us((format.buffer_size / bytes_per_frame(format)) / format.frame_rate); 69 } 70 return duration; 71 } 72 73 bigtime_t 74 frames_duration( 75 const media_raw_audio_format & format, int64 num_frames) 76 { 77 // Tells us how long in us it will take to produce num_frames, 78 // with the given format. 79 bigtime_t duration = 0; 80 if (format.frame_rate > 0) { 81 duration = s_to_us(num_frames/format.frame_rate); 82 } 83 return duration; 84 } 85 86 int 87 buffers_for_duration( 88 const media_raw_audio_format & format, bigtime_t duration) 89 { 90 // Double-checking those unit conversions again: 91 // secs * ( (frames/sec) / (frames/buffer) ) = secs * (buffers/sec) = buffers 92 int buffers = 0; 93 if (frames_per_buffer(format) > 0) { 94 buffers = (int) ceil(us_to_s(duration)*(format.frame_rate/frames_per_buffer(format))); 95 } 96 return buffers; 97 } 98 99 int64 100 frames_for_duration( 101 const media_raw_audio_format & format, bigtime_t duration) 102 { 103 return (int64) ceil(format.frame_rate*us_to_s(duration)); 104 } 105