1
0
mirror of https://github.com/ioacademy-jikim/multimedia synced 2026-08-11 16:33:04 +00:00

멀티미디어 예제

This commit is contained in:
ioacademy-jikim
2015-08-04 19:14:13 +09:00
commit d3b375295e
59 changed files with 8232 additions and 0 deletions
BIN
View File
Binary file not shown.
+20
View File
@@ -0,0 +1,20 @@
#include "main.h"
int my_atoi(char *buff)
{
int i, sum=0;
for(i=0; buff[i]; i++ )
sum = sum*10 + buff[i] - '0';
return sum;
}
int my_add(int a, int b )
{
return a+b;
}
struct _HMI HMI = { my_atoi, my_add, 10 };
+26
View File
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <dlfcn.h>
int main()
{
int *global;
int (*func)(char*);
int (*add)(int,int);
int data;
void *handle = dlopen("libatoi.so", RTLD_LAZY);
func = dlsym( handle, "my_atoi" );
data = func("123");
printf("data=%d\n", data );
add = dlsym( handle, "my_add" );
data = add(1,2);
printf("data=%d\n", data );
*global = dlsym( handle, "global" );
data = add(1,2);
printf("data=%d\n", data );
dlclose(handle);
}
+10
View File
@@ -0,0 +1,10 @@
int my_atoi(char *buff);
int my_add(int a, int b);
struct _HMI
{
int (*atoi)(char *buff);
int (*add)(int , int );
int global;
};
+14
View File
@@ -0,0 +1,14 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= server.cpp
LOCAL_MODULE := my_server
LOCAL_SHARED_LIBRARIES:= libcutils libutils libbinder
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= client.cpp
LOCAL_MODULE := my_client
LOCAL_SHARED_LIBRARIES:= libcutils libutils libbinder
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
+17
View File
@@ -0,0 +1,17 @@
#include <binder/IServiceManager.h>
#include <utils/StrongPointer.h>
#include <binder/MemoryHeapBase.h>
#include <binder/IPCThreadState.h>
#include <stdio.h>
using namespace android;
int main()
{
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> binder = sm->getService( String16("my.ashmem1") );
sp<IMemoryHeap> heap = interface_cast<IMemoryHeap>(binder);
char *p = (char*)heap->getBase();
printf("[%s]\n", p );
return 0;
}
+18
View File
@@ -0,0 +1,18 @@
#include <binder/IServiceManager.h>
#include <utils/StrongPointer.h>
#include <binder/MemoryHeapBase.h>
#include <binder/IPCThreadState.h>
using namespace android;
int main()
{
sp<IServiceManager> sm = defaultServiceManager();
sp<MemoryHeapBase> heap = new MemoryHeapBase(4096);
sm->addService( String16("my.ashmem1") , heap );
char *p = (char*)heap->getBase();
sprintf(p, "Hello Client!!\n" );
IPCThreadState::self()->joinThreadPool();
return 0;
}
Binary file not shown.
+14
View File
@@ -0,0 +1,14 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= server.cpp
LOCAL_MODULE := my_server
LOCAL_SHARED_LIBRARIES:= libcutils libutils libbinder
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= client.cpp
LOCAL_MODULE := my_client
LOCAL_SHARED_LIBRARIES:= libcutils libutils libbinder
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
+21
View File
@@ -0,0 +1,21 @@
#include <binder/IServiceManager.h>
#include <utils/StrongPointer.h>
#include <binder/MemoryHeapBase.h>
#include <binder/IPCThreadState.h>
#include <stdio.h>
using namespace android;
int main()
{
sp<IServiceManager> sm = defaultServiceManager();
sp<IBinder> binder = sm->getService( String16("my.ashmem1") );
sp<IMemory> memory = interface_cast<IMemory>(binder);
ssize_t offset=0;
size_t size=0;
sp<IMemoryHeap> heap = memory->getMemory(&offset, &size);
char *p = (char*)heap->getBase();
printf("[%s]\n", p+offset );
return 0;
}
+19
View File
@@ -0,0 +1,19 @@
#include <binder/IServiceManager.h>
#include <utils/StrongPointer.h>
#include <binder/MemoryHeapBase.h>
#include <binder/MemoryBase.h>
#include <binder/IPCThreadState.h>
using namespace android;
int main()
{
sp<IServiceManager> sm = defaultServiceManager();
sp<MemoryHeapBase> heap = new MemoryHeapBase(4096);
sm->addService( String16("my.ashmem1") , new MemoryBase(heap, 100, 100) );
char *p = (char*)heap->getBase();
sprintf(p+100, "Hello Client!!\n" );
IPCThreadState::self()->joinThreadPool();
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
test-mixer.cpp \
AudioMixer.cpp.arm \
LOCAL_C_INCLUDES := \
bionic \
bionic/libstdc++/include \
external/stlport/stlport \
$(call include-path-for, audio-effects) \
$(call include-path-for, audio-utils) \
frameworks/av/services/audioflinger
LOCAL_STATIC_LIBRARIES := \
libsndfile
LOCAL_SHARED_LIBRARIES := \
libstlport \
libeffects \
libnbaio \
libcommon_time_client \
libaudioresampler \
libaudioutils \
libdl \
libcutils \
libutils \
liblog
LOCAL_MODULE:= test-mixer
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
File diff suppressed because it is too large Load Diff
+470
View File
@@ -0,0 +1,470 @@
/*
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#ifndef ANDROID_AUDIO_MIXER_H
#define ANDROID_AUDIO_MIXER_H
#include <stdint.h>
#include <sys/types.h>
#include <utils/threads.h>
#include <media/AudioBufferProvider.h>
#include "AudioResampler.h"
#include <hardware/audio_effect.h>
#include <system/audio.h>
#include <media/nbaio/NBLog.h>
// FIXME This is actually unity gain, which might not be max in future, expressed in U.12
#define MAX_GAIN_INT AudioMixer::UNITY_GAIN_INT
namespace android {
// ----------------------------------------------------------------------------
class AudioMixer
{
public:
AudioMixer(size_t frameCount, uint32_t sampleRate,
uint32_t maxNumTracks = MAX_NUM_TRACKS);
/*virtual*/ ~AudioMixer(); // non-virtual saves a v-table, restore if sub-classed
// This mixer has a hard-coded upper limit of 32 active track inputs.
// Adding support for > 32 tracks would require more than simply changing this value.
static const uint32_t MAX_NUM_TRACKS = 32;
// maximum number of channels supported by the mixer
// This mixer has a hard-coded upper limit of 8 channels for output.
static const uint32_t MAX_NUM_CHANNELS = 8;
static const uint32_t MAX_NUM_VOLUMES = 2; // stereo volume only
// maximum number of channels supported for the content
static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = AUDIO_CHANNEL_COUNT_MAX;
static const uint16_t UNITY_GAIN_INT = 0x1000;
static const float UNITY_GAIN_FLOAT = 1.0f;
enum { // names
// track names (MAX_NUM_TRACKS units)
TRACK0 = 0x1000,
// 0x2000 is unused
// setParameter targets
TRACK = 0x3000,
RESAMPLE = 0x3001,
RAMP_VOLUME = 0x3002, // ramp to new volume
VOLUME = 0x3003, // don't ramp
// set Parameter names
// for target TRACK
CHANNEL_MASK = 0x4000,
FORMAT = 0x4001,
MAIN_BUFFER = 0x4002,
AUX_BUFFER = 0x4003,
DOWNMIX_TYPE = 0X4004,
MIXER_FORMAT = 0x4005, // AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
MIXER_CHANNEL_MASK = 0x4006, // Channel mask for mixer output
// for target RESAMPLE
SAMPLE_RATE = 0x4100, // Configure sample rate conversion on this track name;
// parameter 'value' is the new sample rate in Hz.
// Only creates a sample rate converter the first time that
// the track sample rate is different from the mix sample rate.
// If the new sample rate is the same as the mix sample rate,
// and a sample rate converter already exists,
// then the sample rate converter remains present but is a no-op.
RESET = 0x4101, // Reset sample rate converter without changing sample rate.
// This clears out the resampler's input buffer.
REMOVE = 0x4102, // Remove the sample rate converter on this track name;
// the track is restored to the mix sample rate.
// for target RAMP_VOLUME and VOLUME (8 channels max)
// FIXME use float for these 3 to improve the dynamic range
VOLUME0 = 0x4200,
VOLUME1 = 0x4201,
AUXLEVEL = 0x4210,
};
// For all APIs with "name": TRACK0 <= name < TRACK0 + MAX_NUM_TRACKS
// Allocate a track name. Returns new track name if successful, -1 on failure.
// The failure could be because of an invalid channelMask or format, or that
// the track capacity of the mixer is exceeded.
int getTrackName(audio_channel_mask_t channelMask,
audio_format_t format, int sessionId);
// Free an allocated track by name
void deleteTrackName(int name);
// Enable or disable an allocated track by name
void enable(int name);
void disable(int name);
void setParameter(int name, int target, int param, void *value);
void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
void process(int64_t pts);
uint32_t trackNames() const { return mTrackNames; }
size_t getUnreleasedFrames(int name) const;
static inline bool isValidPcmTrackFormat(audio_format_t format) {
return format == AUDIO_FORMAT_PCM_16_BIT ||
format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
format == AUDIO_FORMAT_PCM_32_BIT ||
format == AUDIO_FORMAT_PCM_FLOAT;
}
private:
enum {
// FIXME this representation permits up to 8 channels
NEEDS_CHANNEL_COUNT__MASK = 0x00000007,
};
enum {
NEEDS_CHANNEL_1 = 0x00000000, // mono
NEEDS_CHANNEL_2 = 0x00000001, // stereo
// sample format is not explicitly specified, and is assumed to be AUDIO_FORMAT_PCM_16_BIT
NEEDS_MUTE = 0x00000100,
NEEDS_RESAMPLE = 0x00001000,
NEEDS_AUX = 0x00010000,
};
struct state_t;
struct track_t;
class CopyBufferProvider;
typedef void (*hook_t)(track_t* t, int32_t* output, size_t numOutFrames, int32_t* temp,
int32_t* aux);
static const int BLOCKSIZE = 16; // 4 cache lines
struct track_t {
uint32_t needs;
// TODO: Eventually remove legacy integer volume settings
union {
int16_t volume[MAX_NUM_VOLUMES]; // U4.12 fixed point (top bit should be zero)
int32_t volumeRL;
};
int32_t prevVolume[MAX_NUM_VOLUMES];
// 16-byte boundary
int32_t volumeInc[MAX_NUM_VOLUMES];
int32_t auxInc;
int32_t prevAuxLevel;
// 16-byte boundary
int16_t auxLevel; // 0 <= auxLevel <= MAX_GAIN_INT, but signed for mul performance
uint16_t frameCount;
uint8_t channelCount; // 1 or 2, redundant with (needs & NEEDS_CHANNEL_COUNT__MASK)
uint8_t unused_padding; // formerly format, was always 16
uint16_t enabled; // actually bool
audio_channel_mask_t channelMask;
// actual buffer provider used by the track hooks, see DownmixerBufferProvider below
// for how the Track buffer provider is wrapped by another one when dowmixing is required
AudioBufferProvider* bufferProvider;
// 16-byte boundary
mutable AudioBufferProvider::Buffer buffer; // 8 bytes
hook_t hook;
const void* in; // current location in buffer
// 16-byte boundary
AudioResampler* resampler;
uint32_t sampleRate;
int32_t* mainBuffer;
int32_t* auxBuffer;
// 16-byte boundary
AudioBufferProvider* mInputBufferProvider; // externally provided buffer provider.
CopyBufferProvider* mReformatBufferProvider; // provider wrapper for reformatting.
CopyBufferProvider* downmixerBufferProvider; // wrapper for channel conversion.
int32_t sessionId;
// 16-byte boundary
audio_format_t mMixerFormat; // output mix format: AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
audio_format_t mFormat; // input track format
audio_format_t mMixerInFormat; // mix internal format AUDIO_FORMAT_PCM_(FLOAT|16_BIT)
// each track must be converted to this format.
float mVolume[MAX_NUM_VOLUMES]; // floating point set volume
float mPrevVolume[MAX_NUM_VOLUMES]; // floating point previous volume
float mVolumeInc[MAX_NUM_VOLUMES]; // floating point volume increment
float mAuxLevel; // floating point set aux level
float mPrevAuxLevel; // floating point prev aux level
float mAuxInc; // floating point aux increment
// 16-byte boundary
audio_channel_mask_t mMixerChannelMask;
uint32_t mMixerChannelCount;
bool needsRamp() { return (volumeInc[0] | volumeInc[1] | auxInc) != 0; }
bool setResampler(uint32_t trackSampleRate, uint32_t devSampleRate);
bool doesResample() const { return resampler != NULL; }
void resetResampler() { if (resampler != NULL) resampler->reset(); }
void adjustVolumeRamp(bool aux, bool useFloat = false);
size_t getUnreleasedFrames() const { return resampler != NULL ?
resampler->getUnreleasedFrames() : 0; };
};
typedef void (*process_hook_t)(state_t* state, int64_t pts);
// pad to 32-bytes to fill cache line
struct state_t {
uint32_t enabledTracks;
uint32_t needsChanged;
size_t frameCount;
process_hook_t hook; // one of process__*, never NULL
int32_t *outputTemp;
int32_t *resampleTemp;
NBLog::Writer* mLog;
int32_t reserved[1];
// FIXME allocate dynamically to save some memory when maxNumTracks < MAX_NUM_TRACKS
track_t tracks[MAX_NUM_TRACKS] __attribute__((aligned(32)));
};
// Base AudioBufferProvider class used for DownMixerBufferProvider, RemixBufferProvider,
// and ReformatBufferProvider.
// It handles a private buffer for use in converting format or channel masks from the
// input data to a form acceptable by the mixer.
// TODO: Make a ResamplerBufferProvider when integers are entirely removed from the
// processing pipeline.
class CopyBufferProvider : public AudioBufferProvider {
public:
// Use a private buffer of bufferFrameCount frames (each frame is outputFrameSize bytes).
// If bufferFrameCount is 0, no private buffer is created and in-place modification of
// the upstream buffer provider's buffers is performed by copyFrames().
CopyBufferProvider(size_t inputFrameSize, size_t outputFrameSize,
size_t bufferFrameCount);
virtual ~CopyBufferProvider();
// Overrides AudioBufferProvider methods
virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
virtual void releaseBuffer(Buffer* buffer);
// Other public methods
// call this to release the buffer to the upstream provider.
// treat it as an audio discontinuity for future samples.
virtual void reset();
// this function should be supplied by the derived class. It converts
// #frames in the *src pointer to the *dst pointer. It is public because
// some providers will allow this to work on arbitrary buffers outside
// of the internal buffers.
virtual void copyFrames(void *dst, const void *src, size_t frames) = 0;
// set the upstream buffer provider. Consider calling "reset" before this function.
void setBufferProvider(AudioBufferProvider *p) {
mTrackBufferProvider = p;
}
protected:
AudioBufferProvider* mTrackBufferProvider;
const size_t mInputFrameSize;
const size_t mOutputFrameSize;
private:
AudioBufferProvider::Buffer mBuffer;
const size_t mLocalBufferFrameCount;
void* mLocalBufferData;
size_t mConsumed;
};
// DownmixerBufferProvider wraps a track AudioBufferProvider to provide
// position dependent downmixing by an Audio Effect.
class DownmixerBufferProvider : public CopyBufferProvider {
public:
DownmixerBufferProvider(audio_channel_mask_t inputChannelMask,
audio_channel_mask_t outputChannelMask, audio_format_t format,
uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount);
virtual ~DownmixerBufferProvider();
virtual void copyFrames(void *dst, const void *src, size_t frames);
bool isValid() const { return mDownmixHandle != NULL; }
static status_t init();
static bool isMultichannelCapable() { return sIsMultichannelCapable; }
protected:
effect_handle_t mDownmixHandle;
effect_config_t mDownmixConfig;
// effect descriptor for the downmixer used by the mixer
static effect_descriptor_t sDwnmFxDesc;
// indicates whether a downmix effect has been found and is usable by this mixer
static bool sIsMultichannelCapable;
// FIXME: should we allow effects outside of the framework?
// We need to here. A special ioId that must be <= -2 so it does not map to a session.
static const int32_t SESSION_ID_INVALID_AND_IGNORED = -2;
};
// RemixBufferProvider wraps a track AudioBufferProvider to perform an
// upmix or downmix to the proper channel count and mask.
class RemixBufferProvider : public CopyBufferProvider {
public:
RemixBufferProvider(audio_channel_mask_t inputChannelMask,
audio_channel_mask_t outputChannelMask, audio_format_t format,
size_t bufferFrameCount);
virtual void copyFrames(void *dst, const void *src, size_t frames);
protected:
const audio_format_t mFormat;
const size_t mSampleSize;
const size_t mInputChannels;
const size_t mOutputChannels;
int8_t mIdxAry[sizeof(uint32_t)*8]; // 32 bits => channel indices
};
// ReformatBufferProvider wraps a track AudioBufferProvider to convert the input data
// to an acceptable mixer input format type.
class ReformatBufferProvider : public CopyBufferProvider {
public:
ReformatBufferProvider(int32_t channels,
audio_format_t inputFormat, audio_format_t outputFormat,
size_t bufferFrameCount);
virtual void copyFrames(void *dst, const void *src, size_t frames);
protected:
const int32_t mChannels;
const audio_format_t mInputFormat;
const audio_format_t mOutputFormat;
};
// bitmask of allocated track names, where bit 0 corresponds to TRACK0 etc.
uint32_t mTrackNames;
// bitmask of configured track names; ~0 if maxNumTracks == MAX_NUM_TRACKS,
// but will have fewer bits set if maxNumTracks < MAX_NUM_TRACKS
const uint32_t mConfiguredNames;
const uint32_t mSampleRate;
NBLog::Writer mDummyLog;
public:
void setLog(NBLog::Writer* log);
private:
state_t mState __attribute__((aligned(32)));
// Call after changing either the enabled status of a track, or parameters of an enabled track.
// OK to call more often than that, but unnecessary.
void invalidateState(uint32_t mask);
bool setChannelMasks(int name,
audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask);
// TODO: remove unused trackName/trackNum from functions below.
static status_t initTrackDownmix(track_t* pTrack, int trackName);
static status_t prepareTrackForDownmix(track_t* pTrack, int trackNum);
static void unprepareTrackForDownmix(track_t* pTrack, int trackName);
static status_t prepareTrackForReformat(track_t* pTrack, int trackNum);
static void unprepareTrackForReformat(track_t* pTrack, int trackName);
static void reconfigureBufferProviders(track_t* pTrack);
static void track__genericResample(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
int32_t* aux);
static void track__nop(track_t* t, int32_t* out, size_t numFrames, int32_t* temp, int32_t* aux);
static void track__16BitsStereo(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
int32_t* aux);
static void track__16BitsMono(track_t* t, int32_t* out, size_t numFrames, int32_t* temp,
int32_t* aux);
static void volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
int32_t* aux);
static void volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
int32_t* aux);
static void process__validate(state_t* state, int64_t pts);
static void process__nop(state_t* state, int64_t pts);
static void process__genericNoResampling(state_t* state, int64_t pts);
static void process__genericResampling(state_t* state, int64_t pts);
static void process__OneTrack16BitsStereoNoResampling(state_t* state,
int64_t pts);
static int64_t calculateOutputPTS(const track_t& t, int64_t basePTS,
int outputFrameIndex);
static uint64_t sLocalTimeFreq;
static pthread_once_t sOnceControl;
static void sInitRoutine();
/* multi-format volume mixing function (calls template functions
* in AudioMixerOps.h). The template parameters are as follows:
*
* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
* USEFLOATVOL (set to true if float volume is used)
* ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
* TO: int32_t (Q4.27) or float
* TI: int32_t (Q4.27) or int16_t (Q0.15) or float
* TA: int32_t (Q4.27)
*/
template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
typename TO, typename TI, typename TA>
static void volumeMix(TO *out, size_t outFrames,
const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t);
// multi-format process hooks
template <int MIXTYPE, typename TO, typename TI, typename TA>
static void process_NoResampleOneTrack(state_t* state, int64_t pts);
// multi-format track hooks
template <int MIXTYPE, typename TO, typename TI, typename TA>
static void track__Resample(track_t* t, TO* out, size_t frameCount,
TO* temp __unused, TA* aux);
template <int MIXTYPE, typename TO, typename TI, typename TA>
static void track__NoResample(track_t* t, TO* out, size_t frameCount,
TO* temp __unused, TA* aux);
static void convertMixerFormat(void *out, audio_format_t mixerOutFormat,
void *in, audio_format_t mixerInFormat, size_t sampleCount);
// hook types
enum {
PROCESSTYPE_NORESAMPLEONETRACK,
};
enum {
TRACKTYPE_NOP,
TRACKTYPE_RESAMPLE,
TRACKTYPE_NORESAMPLE,
TRACKTYPE_NORESAMPLEMONO,
};
// functions for determining the proper process and track hooks.
static process_hook_t getProcessHook(int processType, uint32_t channelCount,
audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
static hook_t getTrackHook(int trackType, uint32_t channelCount,
audio_format_t mixerInFormat, audio_format_t mixerOutFormat);
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_AUDIO_MIXER_H
Binary file not shown.
+306
View File
@@ -0,0 +1,306 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <inttypes.h>
#include <math.h>
#include <vector>
#include <audio_utils/primitives.h>
#include <audio_utils/sndfile.h>
#include <media/AudioBufferProvider.h>
#include "AudioMixer.h"
#include "test_utils.h"
/* Testing is typically through creation of an output WAV file from several
* source inputs, to be later analyzed by an audio program such as Audacity.
*
* Sine or chirp functions are typically more useful as input to the mixer
* as they show up as straight lines on a spectrogram if successfully mixed.
*
* A sample shell script is provided: mixer_to_wave_tests.sh
*/
using namespace android;
static void usage(const char* name) {
fprintf(stderr, "Usage: %s [-f] [-m] [-c channels]"
" [-s sample-rate] [-o <output-file>] [-a <aux-buffer-file>] [-P csv]"
" (<input-file> | <command>)+\n", name);
fprintf(stderr, " -f enable floating point input track\n");
fprintf(stderr, " -m enable floating point mixer output\n");
fprintf(stderr, " -c number of mixer output channels\n");
fprintf(stderr, " -s mixer sample-rate\n");
fprintf(stderr, " -o <output-file> WAV file, pcm16 (or float if -m specified)\n");
fprintf(stderr, " -a <aux-buffer-file>\n");
fprintf(stderr, " -P # frames provided per call to resample() in CSV format\n");
fprintf(stderr, " <input-file> is a WAV file\n");
fprintf(stderr, " <command> can be 'sine:<channels>,<frequency>,<samplerate>'\n");
fprintf(stderr, " 'chirp:<channels>,<samplerate>'\n");
}
static int writeFile(const char *filename, const void *buffer,
uint32_t sampleRate, uint32_t channels, size_t frames, bool isBufferFloat) {
if (filename == NULL) {
return 0; // ok to pass in NULL filename
}
// write output to file.
SF_INFO info;
info.frames = 0;
info.samplerate = sampleRate;
info.channels = channels;
info.format = SF_FORMAT_WAV | (isBufferFloat ? SF_FORMAT_FLOAT : SF_FORMAT_PCM_16);
printf("saving file:%s channels:%u samplerate:%u frames:%zu\n",
filename, info.channels, info.samplerate, frames);
SNDFILE *sf = sf_open(filename, SFM_WRITE, &info);
if (sf == NULL) {
perror(filename);
return EXIT_FAILURE;
}
if (isBufferFloat) {
(void) sf_writef_float(sf, (float*)buffer, frames);
} else {
(void) sf_writef_short(sf, (short*)buffer, frames);
}
sf_close(sf);
return EXIT_SUCCESS;
}
int main(int argc, char* argv[]) {
const char* const progname = argv[0];
bool useInputFloat = false;
bool useMixerFloat = false;
bool useRamp = true;
uint32_t outputSampleRate = 48000;
uint32_t outputChannels = 2; // stereo for now
std::vector<int> Pvalues;
const char* outputFilename = NULL;
const char* auxFilename = NULL;
std::vector<int32_t> Names;
std::vector<SignalProvider> Providers;
for (int ch; (ch = getopt(argc, argv, "fmc:s:o:a:P:")) != -1;) {
switch (ch) {
case 'f':
useInputFloat = true;
break;
case 'm':
useMixerFloat = true;
break;
case 'c':
outputChannels = atoi(optarg);
break;
case 's':
outputSampleRate = atoi(optarg);
break;
case 'o':
outputFilename = optarg;
break;
case 'a':
auxFilename = optarg;
break;
case 'P':
if (parseCSV(optarg, Pvalues) < 0) {
fprintf(stderr, "incorrect syntax for -P option\n");
return EXIT_FAILURE;
}
break;
case '?':
default:
usage(progname);
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (argc == 0) {
usage(progname);
return EXIT_FAILURE;
}
if ((unsigned)argc > AudioMixer::MAX_NUM_TRACKS) {
fprintf(stderr, "too many tracks: %d > %u", argc, AudioMixer::MAX_NUM_TRACKS);
return EXIT_FAILURE;
}
size_t outputFrames = 0;
// create providers for each track
Providers.resize(argc);
for (int i = 0; i < argc; ++i) {
static const char chirp[] = "chirp:";
static const char sine[] = "sine:";
static const double kSeconds = 10;
if (!strncmp(argv[i], chirp, strlen(chirp))) {
std::vector<int> v;
parseCSV(argv[i] + strlen(chirp), v);
if (v.size() == 2) {
printf("creating chirp(%d %d)\n", v[0], v[1]);
if (useInputFloat) {
Providers[i].setChirp<float>(v[0], 0, v[1]/2, v[1], kSeconds);
} else {
Providers[i].setChirp<int16_t>(v[0], 0, v[1]/2, v[1], kSeconds);
}
Providers[i].setIncr(Pvalues);
} else {
fprintf(stderr, "malformed input '%s'\n", argv[i]);
}
} else if (!strncmp(argv[i], sine, strlen(sine))) {
std::vector<int> v;
parseCSV(argv[i] + strlen(sine), v);
if (v.size() == 3) {
printf("creating sine(%d %d %d)\n", v[0], v[1], v[2]);
if (useInputFloat) {
Providers[i].setSine<float>(v[0], v[1], v[2], kSeconds);
} else {
Providers[i].setSine<int16_t>(v[0], v[1], v[2], kSeconds);
}
Providers[i].setIncr(Pvalues);
} else {
fprintf(stderr, "malformed input '%s'\n", argv[i]);
}
} else {
printf("creating filename(%s)\n", argv[i]);
if (useInputFloat) {
Providers[i].setFile<float>(argv[i]);
} else {
Providers[i].setFile<short>(argv[i]);
}
Providers[i].setIncr(Pvalues);
}
// calculate the number of output frames
size_t nframes = (int64_t) Providers[i].getNumFrames() * outputSampleRate
/ Providers[i].getSampleRate();
if (i == 0 || outputFrames > nframes) { // choose minimum for outputFrames
outputFrames = nframes;
}
}
// create the output buffer.
const size_t outputFrameSize = outputChannels
* (useMixerFloat ? sizeof(float) : sizeof(int16_t));
const size_t outputSize = outputFrames * outputFrameSize;
const audio_channel_mask_t outputChannelMask =
audio_channel_out_mask_from_count(outputChannels);
void *outputAddr = NULL;
(void) posix_memalign(&outputAddr, 32, outputSize);
memset(outputAddr, 0, outputSize);
// create the aux buffer, if needed.
const size_t auxFrameSize = sizeof(int32_t); // Q4.27 always
const size_t auxSize = outputFrames * auxFrameSize;
void *auxAddr = NULL;
if (auxFilename) {
(void) posix_memalign(&auxAddr, 32, auxSize);
memset(auxAddr, 0, auxSize);
}
// create the mixer.
const size_t mixerFrameCount = 320; // typical numbers may range from 240 or 960
AudioMixer *mixer = new AudioMixer(mixerFrameCount, outputSampleRate);
audio_format_t inputFormat = useInputFloat
? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
audio_format_t mixerFormat = useMixerFloat
? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
float f = AudioMixer::UNITY_GAIN_FLOAT / Providers.size(); // normalize volume by # tracks
static float f0; // zero
// set up the tracks.
for (size_t i = 0; i < Providers.size(); ++i) {
//printf("track %d out of %d\n", i, Providers.size());
uint32_t channelMask = audio_channel_out_mask_from_count(Providers[i].getNumChannels());
int32_t name = mixer->getTrackName(channelMask,
inputFormat, AUDIO_SESSION_OUTPUT_MIX);
ALOG_ASSERT(name >= 0);
Names.push_back(name);
mixer->setBufferProvider(name, &Providers[i]);
mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
(void *)outputAddr);
mixer->setParameter(
name,
AudioMixer::TRACK,
AudioMixer::MIXER_FORMAT,
(void *)(uintptr_t)mixerFormat);
mixer->setParameter(
name,
AudioMixer::TRACK,
AudioMixer::FORMAT,
(void *)(uintptr_t)inputFormat);
mixer->setParameter(
name,
AudioMixer::TRACK,
AudioMixer::MIXER_CHANNEL_MASK,
(void *)(uintptr_t)outputChannelMask);
mixer->setParameter(
name,
AudioMixer::TRACK,
AudioMixer::CHANNEL_MASK,
(void *)(uintptr_t)channelMask);
mixer->setParameter(
name,
AudioMixer::RESAMPLE,
AudioMixer::SAMPLE_RATE,
(void *)(uintptr_t)Providers[i].getSampleRate());
if (useRamp) {
mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f0);
mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f0);
mixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::VOLUME0, &f);
mixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::VOLUME1, &f);
} else {
mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME0, &f);
mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::VOLUME1, &f);
}
if (auxFilename) {
mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::AUX_BUFFER,
(void *) auxAddr);
mixer->setParameter(name, AudioMixer::VOLUME, AudioMixer::AUXLEVEL, &f0);
mixer->setParameter(name, AudioMixer::RAMP_VOLUME, AudioMixer::AUXLEVEL, &f);
}
mixer->enable(name);
}
// pump the mixer to process data.
size_t i;
for (i = 0; i < outputFrames - mixerFrameCount; i += mixerFrameCount) {
for (size_t j = 0; j < Names.size(); ++j) {
mixer->setParameter(Names[j], AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
(char *) outputAddr + i * outputFrameSize);
if (auxFilename) {
mixer->setParameter(Names[j], AudioMixer::TRACK, AudioMixer::AUX_BUFFER,
(char *) auxAddr + i * auxFrameSize);
}
}
mixer->process(AudioBufferProvider::kInvalidPTS);
}
outputFrames = i; // reset output frames to the data actually produced.
// write to files
writeFile(outputFilename, outputAddr,
outputSampleRate, outputChannels, outputFrames, useMixerFloat);
if (auxFilename) {
// Aux buffer is always in q4_27 format for now.
// memcpy_to_i16_from_q4_27(), but with stereo frame count (not sample count)
ditherAndClamp((int32_t*)auxAddr, (int32_t*)auxAddr, outputFrames >> 1);
writeFile(auxFilename, auxAddr, outputSampleRate, 1, outputFrames, false);
}
delete mixer;
free(outputAddr);
free(auxAddr);
return EXIT_SUCCESS;
}
+307
View File
@@ -0,0 +1,307 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_AUDIO_TEST_UTILS_H
#define ANDROID_AUDIO_TEST_UTILS_H
#include <audio_utils/sndfile.h>
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif
template<typename T, typename U>
struct is_same
{
static const bool value = false;
};
template<typename T>
struct is_same<T, T> // partial specialization
{
static const bool value = true;
};
template<typename T>
static inline T convertValue(double val)
{
if (is_same<T, int16_t>::value) {
return floor(val * 32767.0 + 0.5);
} else if (is_same<T, int32_t>::value) {
return floor(val * (1UL<<31) + 0.5);
}
return val; // assume float or double
}
// Convert a list of integers in CSV format to a Vector of those values.
// Returns the number of elements in the list, or -1 on error.
static inline int parseCSV(const char *string, std::vector<int>& values)
{
// pass 1: count the number of values and do syntax check
size_t numValues = 0;
bool hadDigit = false;
for (const char *p = string; ; ) {
switch (*p++) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
hadDigit = true;
break;
case '\0':
if (hadDigit) {
// pass 2: allocate and initialize vector of values
values.resize(++numValues);
values[0] = atoi(p = string);
for (size_t i = 1; i < numValues; ) {
if (*p++ == ',') {
values[i++] = atoi(p);
}
}
return numValues;
}
// fall through
case ',':
if (hadDigit) {
hadDigit = false;
numValues++;
break;
}
// fall through
default:
return -1;
}
}
}
/* Creates a type-independent audio buffer provider from
* a buffer base address, size, framesize, and input increment array.
*
* No allocation or deallocation of the provided buffer is done.
*/
class TestProvider : public android::AudioBufferProvider {
public:
TestProvider(void* addr, size_t frames, size_t frameSize,
const std::vector<int>& inputIncr)
: mAddr(addr),
mNumFrames(frames),
mFrameSize(frameSize),
mNextFrame(0), mUnrel(0), mInputIncr(inputIncr), mNextIdx(0)
{
}
TestProvider()
: mAddr(NULL), mNumFrames(0), mFrameSize(0),
mNextFrame(0), mUnrel(0), mNextIdx(0)
{
}
void setIncr(const std::vector<int>& inputIncr) {
mInputIncr = inputIncr;
mNextIdx = 0;
}
virtual android::status_t getNextBuffer(Buffer* buffer, int64_t pts __unused = kInvalidPTS)
{
size_t requestedFrames = buffer->frameCount;
if (requestedFrames > mNumFrames - mNextFrame) {
buffer->frameCount = mNumFrames - mNextFrame;
}
if (!mInputIncr.empty()) {
size_t provided = mInputIncr[mNextIdx++];
ALOGV("getNextBuffer() mValue[%zu]=%zu not %zu",
mNextIdx-1, provided, buffer->frameCount);
if (provided < buffer->frameCount) {
buffer->frameCount = provided;
}
if (mNextIdx >= mInputIncr.size()) {
mNextIdx = 0;
}
}
ALOGV("getNextBuffer() requested %zu frames out of %zu frames available"
" and returned %zu frames",
requestedFrames, mNumFrames - mNextFrame, buffer->frameCount);
mUnrel = buffer->frameCount;
if (buffer->frameCount > 0) {
buffer->raw = (char *)mAddr + mFrameSize * mNextFrame;
return android::NO_ERROR;
} else {
buffer->raw = NULL;
return android::NOT_ENOUGH_DATA;
}
}
virtual void releaseBuffer(Buffer* buffer)
{
if (buffer->frameCount > mUnrel) {
ALOGE("releaseBuffer() released %zu frames but only %zu available "
"to release", buffer->frameCount, mUnrel);
mNextFrame += mUnrel;
mUnrel = 0;
} else {
ALOGV("releaseBuffer() released %zu frames out of %zu frames available "
"to release", buffer->frameCount, mUnrel);
mNextFrame += buffer->frameCount;
mUnrel -= buffer->frameCount;
}
buffer->frameCount = 0;
buffer->raw = NULL;
}
void reset()
{
mNextFrame = 0;
}
size_t getNumFrames()
{
return mNumFrames;
}
protected:
void* mAddr; // base address
size_t mNumFrames; // total frames
int mFrameSize; // frame size (# channels * bytes per sample)
size_t mNextFrame; // index of next frame to provide
size_t mUnrel; // number of frames not yet released
std::vector<int> mInputIncr; // number of frames provided per call
size_t mNextIdx; // index of next entry in mInputIncr to use
};
/* Creates a buffer filled with a sine wave.
*/
template<typename T>
static void createSine(void *vbuffer, size_t frames,
size_t channels, double sampleRate, double freq)
{
double tscale = 1. / sampleRate;
T* buffer = reinterpret_cast<T*>(vbuffer);
for (size_t i = 0; i < frames; ++i) {
double t = i * tscale;
double y = sin(2. * M_PI * freq * t);
T yt = convertValue<T>(y);
for (size_t j = 0; j < channels; ++j) {
buffer[i*channels + j] = yt / T(j + 1);
}
}
}
/* Creates a buffer filled with a chirp signal (a sine wave sweep).
*
* When creating the Chirp, note that the frequency is the true sinusoidal
* frequency not the sampling rate.
*
* http://en.wikipedia.org/wiki/Chirp
*/
template<typename T>
static void createChirp(void *vbuffer, size_t frames,
size_t channels, double sampleRate, double minfreq, double maxfreq)
{
double tscale = 1. / sampleRate;
T *buffer = reinterpret_cast<T*>(vbuffer);
// note the chirp constant k has a divide-by-two.
double k = (maxfreq - minfreq) / (2. * tscale * frames);
for (size_t i = 0; i < frames; ++i) {
double t = i * tscale;
double y = sin(2. * M_PI * (k * t + minfreq) * t);
T yt = convertValue<T>(y);
for (size_t j = 0; j < channels; ++j) {
buffer[i*channels + j] = yt / T(j + 1);
}
}
}
/* This derived class creates a buffer provider of datatype T,
* consisting of an input signal, e.g. from createChirp().
* The number of frames can be obtained from the base class
* TestProvider::getNumFrames().
*/
class SignalProvider : public TestProvider {
public:
SignalProvider()
: mSampleRate(0),
mChannels(0)
{
}
virtual ~SignalProvider()
{
free(mAddr);
mAddr = NULL;
}
template <typename T>
void setChirp(size_t channels, double minfreq, double maxfreq, double sampleRate, double time)
{
createBufferByFrames<T>(channels, sampleRate, sampleRate*time);
createChirp<T>(mAddr, mNumFrames, mChannels, mSampleRate, minfreq, maxfreq);
}
template <typename T>
void setSine(size_t channels,
double freq, double sampleRate, double time)
{
createBufferByFrames<T>(channels, sampleRate, sampleRate*time);
createSine<T>(mAddr, mNumFrames, mChannels, mSampleRate, freq);
}
template <typename T>
void setFile(const char *file_in)
{
SF_INFO info;
info.format = 0;
SNDFILE *sf = sf_open(file_in, SFM_READ, &info);
if (sf == NULL) {
perror(file_in);
return;
}
createBufferByFrames<T>(info.channels, info.samplerate, info.frames);
if (is_same<T, float>::value) {
(void) sf_readf_float(sf, (float *) mAddr, mNumFrames);
} else if (is_same<T, short>::value) {
(void) sf_readf_short(sf, (short *) mAddr, mNumFrames);
}
sf_close(sf);
}
template <typename T>
void createBufferByFrames(size_t channels, uint32_t sampleRate, size_t frames)
{
mNumFrames = frames;
mChannels = channels;
mFrameSize = mChannels * sizeof(T);
free(mAddr);
mAddr = malloc(mFrameSize * mNumFrames);
mSampleRate = sampleRate;
}
uint32_t getSampleRate() const {
return mSampleRate;
}
uint32_t getNumChannels() const {
return mChannels;
}
protected:
uint32_t mSampleRate;
uint32_t mChannels;
};
#endif // ANDROID_AUDIO_TEST_UTILS_H
+27
View File
@@ -0,0 +1,27 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
test-resample.cpp \
LOCAL_C_INCLUDES := \
$(call include-path-for, audio-utils)
LOCAL_STATIC_LIBRARIES := \
libsndfile
LOCAL_SHARED_LIBRARIES := \
libaudioresampler \
libaudioutils \
libdl \
libcutils \
libutils \
liblog
LOCAL_MODULE:= test-resample
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
+174
View File
@@ -0,0 +1,174 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_AUDIO_RESAMPLER_H
#define ANDROID_AUDIO_RESAMPLER_H
#include <stdint.h>
#include <sys/types.h>
#include <cutils/compiler.h>
#include <media/AudioBufferProvider.h>
#include <system/audio.h>
namespace android {
// ----------------------------------------------------------------------------
class ANDROID_API AudioResampler {
public:
// Determines quality of SRC.
// LOW_QUALITY: linear interpolator (1st order)
// MED_QUALITY: cubic interpolator (3rd order)
// HIGH_QUALITY: fixed multi-tap FIR (e.g. 48KHz->44.1KHz)
// NOTE: high quality SRC will only be supported for
// certain fixed rate conversions. Sample rate cannot be
// changed dynamically.
enum src_quality {
DEFAULT_QUALITY=0,
LOW_QUALITY=1,
MED_QUALITY=2,
HIGH_QUALITY=3,
VERY_HIGH_QUALITY=4,
DYN_LOW_QUALITY=5,
DYN_MED_QUALITY=6,
DYN_HIGH_QUALITY=7,
};
static const float UNITY_GAIN_FLOAT = 1.0f;
static AudioResampler* create(audio_format_t format, int inChannelCount,
int32_t sampleRate, src_quality quality=DEFAULT_QUALITY);
virtual ~AudioResampler();
virtual void init() = 0;
virtual void setSampleRate(int32_t inSampleRate);
virtual void setVolume(float left, float right);
virtual void setLocalTimeFreq(uint64_t freq);
// set the PTS of the next buffer output by the resampler
virtual void setPTS(int64_t pts);
// Resample int16_t samples from provider and accumulate into 'out'.
// A mono provider delivers a sequence of samples.
// A stereo provider delivers a sequence of interleaved pairs of samples.
// Multi-channel providers are not supported.
// In either case, 'out' holds interleaved pairs of fixed-point Q4.27.
// That is, for a mono provider, there is an implicit up-channeling.
// Since this method accumulates, the caller is responsible for clearing 'out' initially.
// FIXME assumes provider is always successful; it should return the actual frame count.
virtual void resample(int32_t* out, size_t outFrameCount,
AudioBufferProvider* provider) = 0;
virtual void reset();
virtual size_t getUnreleasedFrames() const { return mInputIndex; }
// called from destructor, so must not be virtual
src_quality getQuality() const { return mQuality; }
protected:
// number of bits for phase fraction - 30 bits allows nearly 2x downsampling
static const int kNumPhaseBits = 30;
// phase mask for fraction
static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1;
// multiplier to calculate fixed point phase increment
static const double kPhaseMultiplier;
AudioResampler(int inChannelCount, int32_t sampleRate, src_quality quality);
// prevent copying
AudioResampler(const AudioResampler&);
AudioResampler& operator=(const AudioResampler&);
int64_t calculateOutputPTS(int outputFrameIndex);
const int32_t mChannelCount;
const int32_t mSampleRate;
int32_t mInSampleRate;
AudioBufferProvider::Buffer mBuffer;
union {
int16_t mVolume[2];
uint32_t mVolumeRL;
};
int16_t mTargetVolume[2];
size_t mInputIndex;
int32_t mPhaseIncrement;
uint32_t mPhaseFraction;
uint64_t mLocalTimeFreq;
int64_t mPTS;
// returns the inFrameCount required to generate outFrameCount frames.
//
// Placed here to be a consistent for all resamplers.
//
// Right now, we use the upper bound without regards to the current state of the
// input buffer using integer arithmetic, as follows:
//
// (static_cast<uint64_t>(outFrameCount)*mInSampleRate + (mSampleRate - 1))/mSampleRate;
//
// The double precision equivalent (float may not be precise enough):
// ceil(static_cast<double>(outFrameCount) * mInSampleRate / mSampleRate);
//
// this relies on the fact that the mPhaseIncrement is rounded down from
// #phases * mInSampleRate/mSampleRate and the fact that Sum(Floor(x)) <= Floor(Sum(x)).
// http://www.proofwiki.org/wiki/Sum_of_Floors_Not_Greater_Than_Floor_of_Sums
//
// (so long as double precision is computed accurately enough to be considered
// greater than or equal to the Floor(x) value in int32_t arithmetic; thus this
// will not necessarily hold for floats).
//
// TODO:
// Greater accuracy and a tight bound is obtained by:
// 1) subtract and adjust for the current state of the AudioBufferProvider buffer.
// 2) using the exact integer formula where (ignoring 64b casting)
// inFrameCount = (mPhaseIncrement * (outFrameCount - 1) + mPhaseFraction) / phaseWrapLimit;
// phaseWrapLimit is the wraparound (1 << kNumPhaseBits), if not specified explicitly.
//
inline size_t getInFrameCountRequired(size_t outFrameCount) {
return (static_cast<uint64_t>(outFrameCount)*mInSampleRate
+ (mSampleRate - 1))/mSampleRate;
}
inline float clampFloatVol(float volume) {
if (volume > UNITY_GAIN_FLOAT) {
return UNITY_GAIN_FLOAT;
} else if (volume >= 0.) {
return volume;
}
return 0.; // NaN or negative volume maps to 0.
}
private:
const src_quality mQuality;
// Return 'true' if the quality level is supported without explicit request
static bool qualityIsSupported(src_quality quality);
// For pthread_once()
static void init_routine();
// Return the estimated CPU load for specific resampler in MHz.
// The absolute number is irrelevant, it's the relative values that matter.
static uint32_t qualityMHz(src_quality quality);
};
// ----------------------------------------------------------------------------
}
; // namespace android
#endif // ANDROID_AUDIO_RESAMPLER_H
+509
View File
@@ -0,0 +1,509 @@
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <inttypes.h>
#include <time.h>
#include <math.h>
#include <audio_utils/primitives.h>
#include <audio_utils/sndfile.h>
#include <utils/Vector.h>
#include <media/AudioBufferProvider.h>
#include "AudioResampler.h"
using namespace android;
static bool gVerbose = false;
static int usage(const char* name) {
fprintf(stderr,"Usage: %s [-p] [-f] [-F] [-v] [-c channels]"
" [-q {dq|lq|mq|hq|vhq|dlq|dmq|dhq}]"
" [-i input-sample-rate] [-o output-sample-rate]"
" [-O csv] [-P csv] [<input-file>]"
" <output-file>\n", name);
fprintf(stderr," -p enable profiling\n");
fprintf(stderr," -f enable filter profiling\n");
fprintf(stderr," -F enable floating point -q {dlq|dmq|dhq} only");
fprintf(stderr," -v verbose : log buffer provider calls\n");
fprintf(stderr," -c # channels (1-2 for lq|mq|hq; 1-8 for dlq|dmq|dhq)\n");
fprintf(stderr," -q resampler quality\n");
fprintf(stderr," dq : default quality\n");
fprintf(stderr," lq : low quality\n");
fprintf(stderr," mq : medium quality\n");
fprintf(stderr," hq : high quality\n");
fprintf(stderr," vhq : very high quality\n");
fprintf(stderr," dlq : dynamic low quality\n");
fprintf(stderr," dmq : dynamic medium quality\n");
fprintf(stderr," dhq : dynamic high quality\n");
fprintf(stderr," -i input file sample rate (ignored if input file is specified)\n");
fprintf(stderr," -o output file sample rate\n");
fprintf(stderr," -O # frames output per call to resample() in CSV format\n");
fprintf(stderr," -P # frames provided per call to resample() in CSV format\n");
return -1;
}
// Convert a list of integers in CSV format to a Vector of those values.
// Returns the number of elements in the list, or -1 on error.
int parseCSV(const char *string, Vector<int>& values)
{
// pass 1: count the number of values and do syntax check
size_t numValues = 0;
bool hadDigit = false;
for (const char *p = string; ; ) {
switch (*p++) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
hadDigit = true;
break;
case '\0':
if (hadDigit) {
// pass 2: allocate and initialize vector of values
values.resize(++numValues);
values.editItemAt(0) = atoi(p = optarg);
for (size_t i = 1; i < numValues; ) {
if (*p++ == ',') {
values.editItemAt(i++) = atoi(p);
}
}
return numValues;
}
// fall through
case ',':
if (hadDigit) {
hadDigit = false;
numValues++;
break;
}
// fall through
default:
return -1;
}
}
}
int main(int argc, char* argv[]) {
const char* const progname = argv[0];
bool profileResample = false;
bool profileFilter = false;
bool useFloat = false;
int channels = 1;
int input_freq = 0;
int output_freq = 0;
AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
Vector<int> Ovalues;
Vector<int> Pvalues;
int ch;
while ((ch = getopt(argc, argv, "pfFvc:q:i:o:O:P:")) != -1) {
switch (ch) {
case 'p':
profileResample = true;
break;
case 'f':
profileFilter = true;
break;
case 'F':
useFloat = true;
break;
case 'v':
gVerbose = true;
break;
case 'c':
channels = atoi(optarg);
break;
case 'q':
if (!strcmp(optarg, "dq"))
quality = AudioResampler::DEFAULT_QUALITY;
else if (!strcmp(optarg, "lq"))
quality = AudioResampler::LOW_QUALITY;
else if (!strcmp(optarg, "mq"))
quality = AudioResampler::MED_QUALITY;
else if (!strcmp(optarg, "hq"))
quality = AudioResampler::HIGH_QUALITY;
else if (!strcmp(optarg, "vhq"))
quality = AudioResampler::VERY_HIGH_QUALITY;
else if (!strcmp(optarg, "dlq"))
quality = AudioResampler::DYN_LOW_QUALITY;
else if (!strcmp(optarg, "dmq"))
quality = AudioResampler::DYN_MED_QUALITY;
else if (!strcmp(optarg, "dhq"))
quality = AudioResampler::DYN_HIGH_QUALITY;
else {
usage(progname);
return -1;
}
break;
case 'i':
input_freq = atoi(optarg);
break;
case 'o':
output_freq = atoi(optarg);
break;
case 'O':
if (parseCSV(optarg, Ovalues) < 0) {
fprintf(stderr, "incorrect syntax for -O option\n");
return -1;
}
break;
case 'P':
if (parseCSV(optarg, Pvalues) < 0) {
fprintf(stderr, "incorrect syntax for -P option\n");
return -1;
}
break;
case '?':
default:
usage(progname);
return -1;
}
}
if (channels < 1
|| channels > (quality < AudioResampler::DYN_LOW_QUALITY ? 2 : 8)) {
fprintf(stderr, "invalid number of audio channels %d\n", channels);
return -1;
}
if (useFloat && quality < AudioResampler::DYN_LOW_QUALITY) {
fprintf(stderr, "float processing is only possible for dynamic resamplers\n");
return -1;
}
argc -= optind;
argv += optind;
const char* file_in = NULL;
const char* file_out = NULL;
if (argc == 1) {
file_out = argv[0];
} else if (argc == 2) {
file_in = argv[0];
file_out = argv[1];
} else {
usage(progname);
return -1;
}
// ----------------------------------------------------------
size_t input_size;
void* input_vaddr;
if (argc == 2) {
SF_INFO info;
info.format = 0;
SNDFILE *sf = sf_open(file_in, SFM_READ, &info);
if (sf == NULL) {
perror(file_in);
return EXIT_FAILURE;
}
input_size = info.frames * info.channels * sizeof(short);
input_vaddr = malloc(input_size);
(void) sf_readf_short(sf, (short *) input_vaddr, info.frames);
sf_close(sf);
channels = info.channels;
input_freq = info.samplerate;
} else {
// data for testing is exactly (input sampling rate/1000)/2 seconds
// so 44.1khz input is 22.05 seconds
double k = 1000; // Hz / s
double time = (input_freq / 2) / k;
size_t input_frames = size_t(input_freq * time);
input_size = channels * sizeof(int16_t) * input_frames;
input_vaddr = malloc(input_size);
int16_t* in = (int16_t*)input_vaddr;
for (size_t i=0 ; i<input_frames ; i++) {
double t = double(i) / input_freq;
double y = sin(M_PI * k * t * t);
int16_t yi = floor(y * 32767.0 + 0.5);
for (int j = 0; j < channels; j++) {
in[i*channels + j] = yi / (1 + j);
}
}
}
size_t input_framesize = channels * sizeof(int16_t);
size_t input_frames = input_size / input_framesize;
// For float processing, convert input int16_t to float array
if (useFloat) {
void *new_vaddr;
input_framesize = channels * sizeof(float);
input_size = input_frames * input_framesize;
new_vaddr = malloc(input_size);
memcpy_to_float_from_i16(reinterpret_cast<float*>(new_vaddr),
reinterpret_cast<int16_t*>(input_vaddr), input_frames * channels);
free(input_vaddr);
input_vaddr = new_vaddr;
}
// ----------------------------------------------------------
class Provider: public AudioBufferProvider {
const void* mAddr; // base address
const size_t mNumFrames; // total frames
const size_t mFrameSize; // size of each frame in bytes
size_t mNextFrame; // index of next frame to provide
size_t mUnrel; // number of frames not yet released
const Vector<int> mPvalues; // number of frames provided per call
size_t mNextPidx; // index of next entry in mPvalues to use
public:
Provider(const void* addr, size_t frames, size_t frameSize, const Vector<int>& Pvalues)
: mAddr(addr),
mNumFrames(frames),
mFrameSize(frameSize),
mNextFrame(0), mUnrel(0), mPvalues(Pvalues), mNextPidx(0) {
}
virtual status_t getNextBuffer(Buffer* buffer,
int64_t pts = kInvalidPTS) {
(void)pts; // suppress warning
size_t requestedFrames = buffer->frameCount;
if (requestedFrames > mNumFrames - mNextFrame) {
buffer->frameCount = mNumFrames - mNextFrame;
}
if (!mPvalues.isEmpty()) {
size_t provided = mPvalues[mNextPidx++];
printf("mPvalue[%zu]=%zu not %zu\n", mNextPidx-1, provided, buffer->frameCount);
if (provided < buffer->frameCount) {
buffer->frameCount = provided;
}
if (mNextPidx >= mPvalues.size()) {
mNextPidx = 0;
}
}
if (gVerbose) {
printf("getNextBuffer() requested %zu frames out of %zu frames available,"
" and returned %zu frames\n",
requestedFrames, (size_t) (mNumFrames - mNextFrame), buffer->frameCount);
}
mUnrel = buffer->frameCount;
if (buffer->frameCount > 0) {
buffer->raw = (char *)mAddr + mFrameSize * mNextFrame;
return NO_ERROR;
} else {
buffer->raw = NULL;
return NOT_ENOUGH_DATA;
}
}
virtual void releaseBuffer(Buffer* buffer) {
if (buffer->frameCount > mUnrel) {
fprintf(stderr, "ERROR releaseBuffer() released %zu frames but only %zu available "
"to release\n", buffer->frameCount, mUnrel);
mNextFrame += mUnrel;
mUnrel = 0;
} else {
if (gVerbose) {
printf("releaseBuffer() released %zu frames out of %zu frames available "
"to release\n", buffer->frameCount, mUnrel);
}
mNextFrame += buffer->frameCount;
mUnrel -= buffer->frameCount;
}
buffer->frameCount = 0;
buffer->raw = NULL;
}
void reset() {
mNextFrame = 0;
}
} provider(input_vaddr, input_frames, input_framesize, Pvalues);
if (gVerbose) {
printf("%zu input frames\n", input_frames);
}
audio_format_t format = useFloat ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
int output_channels = channels > 2 ? channels : 2; // output is at least stereo samples
size_t output_framesize = output_channels * (useFloat ? sizeof(float) : sizeof(int32_t));
size_t output_frames = ((int64_t) input_frames * output_freq) / input_freq;
size_t output_size = output_frames * output_framesize;
if (profileFilter) {
// Check how fast sample rate changes are that require filter changes.
// The delta sample rate changes must indicate a downsampling ratio,
// and must be larger than 10% changes.
//
// On fast devices, filters should be generated between 0.1ms - 1ms.
// (single threaded).
AudioResampler* resampler = AudioResampler::create(format, channels,
8000, quality);
int looplimit = 100;
timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
for (int i = 0; i < looplimit; ++i) {
resampler->setSampleRate(9000);
resampler->setSampleRate(12000);
resampler->setSampleRate(20000);
resampler->setSampleRate(30000);
}
clock_gettime(CLOCK_MONOTONIC, &end);
int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
int64_t time = end_ns - start_ns;
printf("%.2f sample rate changes with filter calculation/sec\n",
looplimit * 4 / (time / 1e9));
// Check how fast sample rate changes are without filter changes.
// This should be very fast, probably 0.1us - 1us per sample rate
// change.
resampler->setSampleRate(1000);
looplimit = 1000;
clock_gettime(CLOCK_MONOTONIC, &start);
for (int i = 0; i < looplimit; ++i) {
resampler->setSampleRate(1000+i);
}
clock_gettime(CLOCK_MONOTONIC, &end);
start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
time = end_ns - start_ns;
printf("%.2f sample rate changes without filter calculation/sec\n",
looplimit / (time / 1e9));
resampler->reset();
delete resampler;
}
void* output_vaddr = malloc(output_size);
AudioResampler* resampler = AudioResampler::create(format, channels,
output_freq, quality);
resampler->setSampleRate(input_freq);
resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
if (profileResample) {
/*
* For profiling on mobile devices, upon experimentation
* it is better to run a few trials with a shorter loop limit,
* and take the minimum time.
*
* Long tests can cause CPU temperature to build up and thermal throttling
* to reduce CPU frequency.
*
* For frequency checks (index=0, or 1, etc.):
* "cat /sys/devices/system/cpu/cpu${index}/cpufreq/scaling_*_freq"
*
* For temperature checks (index=0, or 1, etc.):
* "cat /sys/class/thermal/thermal_zone${index}/temp"
*
* Another way to avoid thermal throttling is to fix the CPU frequency
* at a lower level which prevents excessive temperatures.
*/
const int trials = 4;
const int looplimit = 4;
timespec start, end;
int64_t time = 0;
for (int n = 0; n < trials; ++n) {
clock_gettime(CLOCK_MONOTONIC, &start);
for (int i = 0; i < looplimit; ++i) {
resampler->resample((int*) output_vaddr, output_frames, &provider);
provider.reset(); // during benchmarking reset only the provider
}
clock_gettime(CLOCK_MONOTONIC, &end);
int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
int64_t diff_ns = end_ns - start_ns;
if (n == 0 || diff_ns < time) {
time = diff_ns; // save the best out of our trials.
}
}
// Mfrms/s is "Millions of output frames per second".
printf("quality: %d channels: %d msec: %" PRId64 " Mfrms/s: %.2lf\n",
quality, channels, time/1000000, output_frames * looplimit / (time / 1e9) / 1e6);
resampler->reset();
}
memset(output_vaddr, 0, output_size);
if (gVerbose) {
printf("resample() %zu output frames\n", output_frames);
}
if (Ovalues.isEmpty()) {
Ovalues.push(output_frames);
}
for (size_t i = 0, j = 0; i < output_frames; ) {
size_t thisFrames = Ovalues[j++];
if (j >= Ovalues.size()) {
j = 0;
}
if (thisFrames == 0 || thisFrames > output_frames - i) {
thisFrames = output_frames - i;
}
resampler->resample((int*) output_vaddr + output_channels*i, thisFrames, &provider);
i += thisFrames;
}
if (gVerbose) {
printf("resample() complete\n");
}
resampler->reset();
if (gVerbose) {
printf("reset() complete\n");
}
delete resampler;
resampler = NULL;
// For float processing, convert output format from float to Q4.27,
// which is then converted to int16_t for final storage.
if (useFloat) {
memcpy_to_q4_27_from_float(reinterpret_cast<int32_t*>(output_vaddr),
reinterpret_cast<float*>(output_vaddr), output_frames * output_channels);
}
// mono takes left channel only (out of stereo output pair)
// stereo and multichannel preserve all channels.
int32_t* out = (int32_t*) output_vaddr;
int16_t* convert = (int16_t*) malloc(output_frames * channels * sizeof(int16_t));
const int volumeShift = 12; // shift requirement for Q4.27 to Q.15
// round to half towards zero and saturate at int16 (non-dithered)
const int roundVal = (1<<(volumeShift-1)) - 1; // volumePrecision > 0
for (size_t i = 0; i < output_frames; i++) {
for (int j = 0; j < channels; j++) {
int32_t s = out[i * output_channels + j] + roundVal; // add offset here
if (s < 0) {
s = (s + 1) >> volumeShift; // round to 0
if (s < -32768) {
s = -32768;
}
} else {
s = s >> volumeShift;
if (s > 32767) {
s = 32767;
}
}
convert[i * channels + j] = int16_t(s);
}
}
// write output to disk
SF_INFO info;
info.frames = 0;
info.samplerate = output_freq;
info.channels = channels;
info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
SNDFILE *sf = sf_open(file_out, SFM_WRITE, &info);
if (sf == NULL) {
perror(file_out);
return EXIT_FAILURE;
}
(void) sf_writef_short(sf, convert, output_frames);
sf_close(sf);
return EXIT_SUCCESS;
}
+8
View File
@@ -0,0 +1,8 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= thread.cpp
LOCAL_MODULE := my_thread
LOCAL_SHARED_LIBRARIES:= libcutils libutils libbinder
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
+27
View File
@@ -0,0 +1,27 @@
#include <binder/IServiceManager.h>
#include <utils/StrongPointer.h>
#include <utils/Thread.h>
#include <binder/MemoryHeapBase.h>
#include <binder/IPCThreadState.h>
using namespace android;
class MyThread : public Thread
{
public :
bool threadLoop()
{
printf("MyThread::threadLoop()\n");
sleep(1);
return true;
}
};
int main()
{
sp<Thread> thread = new MyThread;
thread->run();
getchar();
return 0;
}