resampler.h revision b3184d71bc6cee9fcbb36343e379143329be00ce
1b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent/* 2b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** Copyright 2008, The Android Open-Source Project 3b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** 4b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** Licensed under the Apache License, Version 2.0 (the "License"); 5b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** you may not use this file except in compliance with the License. 6b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** You may obtain a copy of the License at 7b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** 8b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** http://www.apache.org/licenses/LICENSE-2.0 9b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** 10b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** Unless required by applicable law or agreed to in writing, software 11b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** distributed under the License is distributed on an "AS IS" BASIS, 12b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** See the License for the specific language governing permissions and 14b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent** limitations under the License. 15b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent*/ 16b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 17b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#ifndef ANDROID_RESAMPLER_H 18b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define ANDROID_RESAMPLER_H 19b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 20b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <stdint.h> 21b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#include <sys/time.h> 22b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 23b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent__BEGIN_DECLS 24b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 25b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 26b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define RESAMPLER_QUALITY_MAX 10 27b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define RESAMPLER_QUALITY_MIN 0 28b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define RESAMPLER_QUALITY_DEFAULT 4 29b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define RESAMPLER_QUALITY_VOIP 3 30b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#define RESAMPLER_QUALITY_DESKTOP 5 31b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 32b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstruct resampler_buffer { 33b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent union { 34b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent void* raw; 35b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent short* i16; 36b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int8_t* i8; 37b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent }; 38b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t frame_count; 39b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}; 40b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 41b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent/* call back interface used by the resampler to get new data */ 42b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstruct resampler_buffer_provider 43b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent{ 44b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent /** 45b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * get a new buffer of data: 46b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * as input: buffer->frame_count is the number of frames requested 47b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * as output: buffer->frame_count is the number of frames returned 48b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * buffer->raw points to data returned 49b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent */ 50b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int (*get_next_buffer)(struct resampler_buffer_provider *provider, 51b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct resampler_buffer *buffer); 52b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent /** 53b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * release a consumed buffer of data: 54b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * as input: buffer->frame_count is the number of frames released 55b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * buffer->raw points to data released 56b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent */ 57b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent void (*release_buffer)(struct resampler_buffer_provider *provider, 58b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct resampler_buffer *buffer); 59b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}; 60b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 61b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent/* resampler interface */ 62b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentstruct resampler_itfe { 63b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent /** 64b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * reset resampler state 65b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent */ 66b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent void (*reset)(struct resampler_itfe *resampler); 67b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent /** 68b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * resample input from buffer provider and output at most *outFrameCount to out buffer. 69b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * *outFrameCount is updated with the actual number of frames produced. 70b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent */ 71b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int (*resample_from_provider)(struct resampler_itfe *resampler, 72b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int16_t *out, 73b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t *outFrameCount); 74b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent /** 75b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * resample at most *inFrameCount frames from in buffer and output at most 76b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * *outFrameCount to out buffer. *inFrameCount and *outFrameCount are updated respectively 77b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * with the number of frames remaining in input and written to output. 78b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent */ 79b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int (*resample_from_input)(struct resampler_itfe *resampler, 80b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int16_t *in, 81b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t *inFrameCount, 82b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int16_t *out, 83b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent size_t *outFrameCount); 84b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent /** 85b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * return the latency introduced by the resampler in ns. 86b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent */ 87b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent int32_t (*delay_ns)(struct resampler_itfe *resampler); 88b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent}; 89b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 90b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent/** 91b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * create a resampler according to input parameters passed. 92b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * If resampler_buffer_provider is not NULL only resample_from_provider() can be called. 93b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * If resampler_buffer_provider is NULL only resample_from_input() can be called. 94b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent */ 95b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentint create_resampler(uint32_t inSampleRate, 96b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t outSampleRate, 97b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t channelCount, 98b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent uint32_t quality, 99b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct resampler_buffer_provider *provider, 100b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent struct resampler_itfe **); 101b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 102b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent/** 103b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent * release resampler resources. 104b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent */ 105b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurentvoid release_resampler(struct resampler_itfe *); 106b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 107b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent__END_DECLS 108b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent 109b3184d71bc6cee9fcbb36343e379143329be00ceEric Laurent#endif // ANDROID_RESAMPLER_H 110