| @@ -53,6 +53,8 @@ BUILD_CXX_FLAGS += -DVESTIGE_HEADER | |||
| # -------------------------------------------------------------- | |||
| HAVE_JACK = $(shell pkg-config --exists jack && echo true) | |||
| HAVE_AF_DEPS = $(shell pkg-config --exists libavcodec libavformat sndfile && echo true) | |||
| HAVE_ZYN_DEPS = $(shell pkg-config --exists fftw3 mxml && echo true) | |||
| ifeq ($(CARLA_PLUGIN_SUPPORT),true) | |||
| @@ -205,6 +205,10 @@ void carla_register_native_plugin_3BandSplitter(); | |||
| void carla_register_native_plugin_PingPongPan(); | |||
| #endif | |||
| #ifdef WANT_AUDIOFILE | |||
| void carla_register_native_plugin_audiofile(); | |||
| #endif | |||
| #ifdef WANT_ZYNADDSUBFX | |||
| // ZynAddSubFX | |||
| void carla_register_native_plugin_zynaddsubfx(); | |||
| @@ -41,6 +41,10 @@ ifeq ($(HAVE_SUIL),true) | |||
| BUILD_CXX_FLAGS += -DWANT_SUIL | |||
| endif | |||
| ifeq ($(HAVE_AF_DEPS),true) | |||
| BUILD_CXX_FLAGS += -DWANT_AUDIOFILE | |||
| endif | |||
| ifeq ($(HAVE_ZYN_DEPS),true) | |||
| BUILD_CXX_FLAGS += -DWANT_ZYNADDSUBFX | |||
| endif | |||
| @@ -722,13 +722,8 @@ protected: | |||
| fFreewheel = isFreewheel; | |||
| } | |||
| void handleJackProcessCallback(const uint32_t nframes) | |||
| void saveTransportInfo() | |||
| { | |||
| #ifndef BUILD_BRIDGE | |||
| if (kData->curPluginCount == 0) | |||
| return proccessPendingEvents(); | |||
| #endif | |||
| fTransportPos.unique_1 = fTransportPos.unique_2 + 1; // invalidate | |||
| fTransportState = jackbridge_transport_query(fClient, &fTransportPos); | |||
| @@ -760,6 +755,16 @@ protected: | |||
| fTimeInfo.frame = 0; | |||
| fTimeInfo.valid = 0x0; | |||
| } | |||
| } | |||
| void handleJackProcessCallback(const uint32_t nframes) | |||
| { | |||
| #ifndef BUILD_BRIDGE | |||
| if (kData->curPluginCount == 0) | |||
| return proccessPendingEvents(); | |||
| #endif | |||
| saveTransportInfo(); | |||
| #ifdef BUILD_BRIDGE | |||
| CarlaPlugin* const plugin = getPluginUnchecked(0); | |||
| @@ -1176,6 +1181,7 @@ private: | |||
| CarlaEngineJack* const engine = (CarlaEngineJack*)CarlaPluginGetEngine(plugin); | |||
| plugin->initBuffers(); | |||
| engine->saveTransportInfo(); | |||
| engine->processPlugin(plugin, nframes); | |||
| } | |||
| @@ -10,11 +10,17 @@ include ../Makefile.mk | |||
| BUILD_CXX_FLAGS += -I../../libs/distrho-plugin-toolkit | |||
| BUILD_CXX_FLAGS += $(shell pkg-config --cflags QtGui) | |||
| LINK_FLAGS += $(shell pkg-config --libs QtGui) -lGL | |||
| LINK_FLAGS += $(shell pkg-config --libs QtGui gl) | |||
| ifeq ($(HAVE_AF_DEPS),true) | |||
| AF_CXX_FLAGS = $(BUILD_CXX_FLAGS) | |||
| AF_CXX_FLAGS += $(shell pkg-config --cflags libavcodec libavformat sndfile) -dpthread | |||
| LINK_FLAGS += $(shell pkg-config --libs libavcodec libavformat sndfile) -lpthread | |||
| endif | |||
| ifeq ($(HAVE_ZYN_DEPS),true) | |||
| ZYN_CXX_FLAGS = $(BUILD_CXX_FLAGS) | |||
| ZYN_CXX_FLAGS += $(shell pkg-config --cflags fftw3 mxml) | |||
| ZYN_CXX_FLAGS += $(shell pkg-config --cflags fftw3 mxml) -dpthread | |||
| LINK_FLAGS += $(shell pkg-config --libs fftw3 mxml) -lpthread | |||
| endif | |||
| @@ -32,6 +38,11 @@ OBJS = \ | |||
| # distrho-3bandsplitter.cpp.o \ | |||
| # distrho-pingpongpan.cpp.o | |||
| ifeq ($(HAVE_AF_DEPS),true) | |||
| OBJS += \ | |||
| audiofile.c.o | |||
| endif | |||
| # ZynAddSubFX | |||
| ifeq ($(HAVE_ZYN_DEPS),true) | |||
| OBJS += \ | |||
| @@ -0,0 +1,127 @@ | |||
| /** | |||
| @brief audio-decoder - wrapper around libsndfile and libav* | |||
| @file ad.h | |||
| @author Robin Gareus <robin@gareus.org> | |||
| Copyright (C) 2011-2013 Robin Gareus <robin@gareus.org> | |||
| This program is free software; you can redistribute it and/or modify | |||
| it under the terms of the GNU Lesser Public License as published by | |||
| the Free Software Foundation; either version 2.1, or (at your option) | |||
| any later version. | |||
| This program is distributed in the hope that it will be useful, | |||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| GNU Lesser Public License for more details. | |||
| You should have received a copy of the GNU Lesser General Public | |||
| License along with this library; if not, write to the Free Software | |||
| Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |||
| */ | |||
| #ifndef __AD_H__ | |||
| #define __AD_H__ | |||
| #include <unistd.h> | |||
| #include <stdint.h> | |||
| struct adinfo { | |||
| unsigned int sample_rate; | |||
| unsigned int channels; | |||
| int64_t length; //milliseconds | |||
| int64_t frames; //total number of frames (eg a frame for 16bit stereo is 4 bytes). | |||
| int bit_rate; | |||
| int bit_depth; | |||
| char * meta_data; | |||
| }; | |||
| /* global init function - register codecs */ | |||
| void ad_init(); | |||
| /* --- public API --- */ | |||
| /** open an audio file | |||
| * @param fn file-name | |||
| * @param nfo pointer to a adinfo struct which will hold information about the file. | |||
| * @return NULL on error, a pointer to an opaque soundfile-decoder object on success. | |||
| */ | |||
| void * ad_open (const char *fn, struct adinfo *nfo); | |||
| /** close an audio file and release decoder structures | |||
| * @param sf decoder handle | |||
| * @return 0 on succees, -1 if sf was invalid or not open (return value can usually be ignored) | |||
| */ | |||
| int ad_close (void *sf); | |||
| /** seel to a given position in the file | |||
| * @param sf decoder handle | |||
| * @param pos frame position to seek to in frames (1 frame = number-of-channel samples) from the start of the file. | |||
| * @return the current position in frames (multi-channel samples) from the start of the file. On error this function returns -1. | |||
| */ | |||
| int64_t ad_seek (void *sf, int64_t pos); | |||
| /** decode audio data chunk to raw interleaved channel floating point data | |||
| * | |||
| * @param sf decoder handle | |||
| * @param out place to store data -- must be large enough to hold (sizeof(float) * len) bytes. | |||
| * @param len number of samples (!) to read (should be a multiple of nfo->channels). | |||
| * @return the number of read samples. | |||
| */ | |||
| ssize_t ad_read (void *sf, float* out, size_t len); | |||
| /** re-read the file information and meta-data. | |||
| * | |||
| * this is not neccesary in general \ref ad_open includes an inplicit call | |||
| * but meta-data may change in live-stream in which case en explicit call to | |||
| * ad_into is needed to update the inforation | |||
| * | |||
| * @param fn file-name | |||
| * @param nfo pointer to a adinfo struct which will hold information about the file. | |||
| * @return 0 on succees, -1 if sf was invalid or not open | |||
| */ | |||
| int ad_info (void *sf, struct adinfo *nfo); | |||
| /** zero initialize the information struct. * (does not free nfo->meta_data) | |||
| * @param nfo pointer to a adinfo struct | |||
| */ | |||
| void ad_clear_nfo (struct adinfo *nfo); | |||
| /** free possibly allocated meta-data text | |||
| * @param nfo pointer to a adinfo struct | |||
| */ | |||
| void ad_free_nfo (struct adinfo *nfo); | |||
| /* --- helper functions --- */ | |||
| /** read file info | |||
| * combines ad_open() and ad_close() | |||
| */ | |||
| int ad_finfo (const char *, struct adinfo *); | |||
| /** | |||
| * wrapper around \ref ad_read, downmixes all channels to mono | |||
| */ | |||
| ssize_t ad_read_mono_dbl (void *, struct adinfo *, double*, size_t); | |||
| /** | |||
| * calls dbg() to print file info to stderr. | |||
| * | |||
| * @param dbglvl | |||
| * @param nfo | |||
| */ | |||
| void ad_dump_nfo (int dbglvl, struct adinfo *nfo); | |||
| /** set audio-decoder debug level -- all info is printed to stderr. | |||
| * | |||
| * @param lvl debug-level threshold | |||
| * -1: absolutley silent | |||
| * 0: errors only | |||
| * 1: errors + info | |||
| * 2: + debug | |||
| * 3: + low-level-debug info | |||
| */ | |||
| void ad_set_debuglevel(int lvl); | |||
| #endif | |||
| @@ -0,0 +1,366 @@ | |||
| /** | |||
| Copyright (C) 2011-2013 Robin Gareus <robin@gareus.org> | |||
| This program is free software; you can redistribute it and/or modify | |||
| it under the terms of the GNU Lesser Public License as published by | |||
| the Free Software Foundation; either version 2.1, or (at your option) | |||
| any later version. | |||
| This program is distributed in the hope that it will be useful, | |||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| GNU Lesser Public License for more details. | |||
| You should have received a copy of the GNU Lesser General Public | |||
| License along with this library; if not, write to the Free Software | |||
| Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |||
| */ | |||
| #include "config.h" | |||
| #include <stdio.h> | |||
| #include <stdlib.h> | |||
| #include <string.h> | |||
| #include <unistd.h> | |||
| #include <math.h> | |||
| #include "audio_decoder/ad_plugin.h" | |||
| #ifdef HAVE_FFMPEG | |||
| #include "ffcompat.h" | |||
| #ifndef MIN | |||
| #define MIN(a,b) ( ( (a) < (b) )? (a) : (b) ) | |||
| #endif | |||
| typedef struct { | |||
| AVFormatContext* formatContext; | |||
| AVCodecContext* codecContext; | |||
| AVCodec* codec; | |||
| AVPacket packet; | |||
| int audioStream; | |||
| int pkt_len; | |||
| uint8_t* pkt_ptr; | |||
| int16_t m_tmpBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE]; | |||
| int16_t* m_tmpBufferStart; | |||
| unsigned long m_tmpBufferLen; | |||
| int64_t decoder_clock; | |||
| int64_t output_clock; | |||
| int64_t seek_frame; | |||
| unsigned int samplerate; | |||
| unsigned int channels; | |||
| int64_t length; | |||
| } ffmpeg_audio_decoder; | |||
| static int ad_info_ffmpeg(void *sf, struct adinfo *nfo) { | |||
| ffmpeg_audio_decoder *priv = (ffmpeg_audio_decoder*) sf; | |||
| if (!priv) return -1; | |||
| if (nfo) { | |||
| nfo->sample_rate = priv->samplerate; | |||
| nfo->channels = priv->channels; | |||
| nfo->frames = priv->length; | |||
| if (nfo->sample_rate==0) return -1; | |||
| nfo->length = (nfo->frames * 1000) / nfo->sample_rate; | |||
| nfo->bit_rate = priv->formatContext->bit_rate; | |||
| nfo->bit_depth = 0; | |||
| nfo->meta_data = NULL; | |||
| #ifdef WITH_GTK // XXX replace g_* functions with POSIX equiv | |||
| AVDictionaryEntry *tag = NULL; | |||
| // Tags in container | |||
| while ((tag = av_dict_get(priv->formatContext->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) { | |||
| dbg(2, "FTAG: %s=%s", tag->key, tag->value); | |||
| char * tmp = g_strdup_printf("%s%s<i>%s</i>:%s", nfo->meta_data?nfo->meta_data:"",nfo->meta_data?"\n":"", tag->key, tag->value); | |||
| if (nfo->meta_data) g_free(nfo->meta_data); | |||
| nfo->meta_data = tmp; | |||
| } | |||
| // Tags in stream | |||
| tag=NULL; | |||
| AVStream *stream = priv->formatContext->streams[priv->audioStream]; | |||
| while ((tag = av_dict_get(stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) { | |||
| dbg(2, "STAG: %s=%s", tag->key, tag->value); | |||
| char * tmp = g_strdup_printf("%s%s<i>%s</i>:%s", nfo->meta_data?nfo->meta_data:"",nfo->meta_data?"\n":"", tag->key, tag->value); | |||
| if (nfo->meta_data) g_free(nfo->meta_data); | |||
| nfo->meta_data = tmp; | |||
| } | |||
| #endif | |||
| } | |||
| return 0; | |||
| } | |||
| static void *ad_open_ffmpeg(const char *fn, struct adinfo *nfo) { | |||
| ffmpeg_audio_decoder *priv = (ffmpeg_audio_decoder*) calloc(1, sizeof(ffmpeg_audio_decoder)); | |||
| priv->m_tmpBufferStart=NULL; | |||
| priv->m_tmpBufferLen=0; | |||
| priv->decoder_clock=priv->output_clock=priv->seek_frame=0; | |||
| priv->packet.size=0; priv->packet.data=NULL; | |||
| if (avformat_open_input(&priv->formatContext, fn, NULL, NULL) <0) { | |||
| dbg(0, "ffmpeg is unable to open file '%s'.", fn); | |||
| free(priv); return(NULL); | |||
| } | |||
| if (avformat_find_stream_info(priv->formatContext, NULL) < 0) { | |||
| avformat_close_input(&priv->formatContext); | |||
| dbg(0, "av_find_stream_info failed" ); | |||
| free(priv); return(NULL); | |||
| } | |||
| priv->audioStream = -1; | |||
| unsigned int i; | |||
| for (i=0; i<priv->formatContext->nb_streams; i++) { | |||
| if (priv->formatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) { | |||
| priv->audioStream = i; | |||
| break; | |||
| } | |||
| } | |||
| if (priv->audioStream == -1) { | |||
| dbg(0, "No Audio Stream found in file"); | |||
| avformat_close_input(&priv->formatContext); | |||
| free(priv); return(NULL); | |||
| } | |||
| priv->codecContext = priv->formatContext->streams[priv->audioStream]->codec; | |||
| priv->codec = avcodec_find_decoder(priv->codecContext->codec_id); | |||
| if (priv->codec == NULL) { | |||
| avformat_close_input(&priv->formatContext); | |||
| dbg(0, "Codec not supported by ffmpeg"); | |||
| free(priv); return(NULL); | |||
| } | |||
| if (avcodec_open2(priv->codecContext, priv->codec, NULL) < 0) { | |||
| dbg(0, "avcodec_open failed" ); | |||
| free(priv); return(NULL); | |||
| } | |||
| dbg(2, "ffmpeg - audio tics: %i/%i [sec]",priv->formatContext->streams[priv->audioStream]->time_base.num,priv->formatContext->streams[priv->audioStream]->time_base.den); | |||
| int64_t len = priv->formatContext->duration - priv->formatContext->start_time; | |||
| priv->formatContext->flags|=AVFMT_FLAG_GENPTS; | |||
| priv->formatContext->flags|=AVFMT_FLAG_IGNIDX; | |||
| priv->samplerate = priv->codecContext->sample_rate; | |||
| priv->channels = priv->codecContext->channels ; | |||
| priv->length = (int64_t)( len * priv->samplerate / AV_TIME_BASE ); | |||
| if (ad_info_ffmpeg((void*)priv, nfo)) { | |||
| dbg(0, "invalid file info (sample-rate==0)"); | |||
| free(priv); return(NULL); | |||
| } | |||
| dbg(1, "ffmpeg - %s", fn); | |||
| if (nfo) | |||
| dbg(1, "ffmpeg - sr:%i c:%i d:%"PRIi64" f:%"PRIi64, nfo->sample_rate, nfo->channels, nfo->length, nfo->frames); | |||
| return (void*) priv; | |||
| } | |||
| static int ad_close_ffmpeg(void *sf) { | |||
| ffmpeg_audio_decoder *priv = (ffmpeg_audio_decoder*) sf; | |||
| if (!priv) return -1; | |||
| avcodec_close(priv->codecContext); | |||
| avformat_close_input(&priv->formatContext); | |||
| free(priv); | |||
| return 0; | |||
| } | |||
| static void int16_to_float(int16_t *in, float *out, int num_channels, int num_samples, int out_offset) { | |||
| int i,ii; | |||
| for (i=0;i<num_samples;i++) { | |||
| for (ii=0;ii<num_channels;ii++) { | |||
| out[(i+out_offset)*num_channels+ii]= (float) in[i*num_channels+ii]/ 32768.0; | |||
| } | |||
| } | |||
| } | |||
| static ssize_t ad_read_ffmpeg(void *sf, float* d, size_t len) { | |||
| ffmpeg_audio_decoder *priv = (ffmpeg_audio_decoder*) sf; | |||
| if (!priv) return -1; | |||
| size_t frames = len / priv->channels; | |||
| size_t written = 0; | |||
| ssize_t ret = 0; | |||
| while (ret >= 0 && written < frames) { | |||
| dbg(3,"loop: %i/%i (bl:%lu)",written, frames, priv->m_tmpBufferLen ); | |||
| if (priv->seek_frame == 0 && priv->m_tmpBufferLen > 0 ) { | |||
| int s = MIN(priv->m_tmpBufferLen / priv->channels, frames - written ); | |||
| int16_to_float(priv->m_tmpBufferStart, d, priv->channels, s , written); | |||
| written += s; | |||
| priv->output_clock+=s; | |||
| s = s * priv->channels; | |||
| priv->m_tmpBufferStart += s; | |||
| priv->m_tmpBufferLen -= s; | |||
| ret = 0; | |||
| } else { | |||
| priv->m_tmpBufferStart = priv->m_tmpBuffer; | |||
| priv->m_tmpBufferLen = 0; | |||
| if (!priv->pkt_ptr || priv->pkt_len <1 ) { | |||
| if (priv->packet.data) av_free_packet(&priv->packet); | |||
| ret = av_read_frame(priv->formatContext, &priv->packet); | |||
| if (ret<0) { dbg(1, "reached end of file."); break; } | |||
| priv->pkt_len = priv->packet.size; | |||
| priv->pkt_ptr = priv->packet.data; | |||
| } | |||
| if (priv->packet.stream_index != priv->audioStream) { | |||
| priv->pkt_ptr = NULL; | |||
| continue; | |||
| } | |||
| /* decode all chunks in packet */ | |||
| int data_size= AVCODEC_MAX_AUDIO_FRAME_SIZE; | |||
| #if 0 // TODO ffcompat.h -- this works but is not optimal (channels may not be planar/interleaved) | |||
| AVFrame avf; // TODO statically allocate | |||
| memset(&avf, 0, sizeof(AVFrame)); // not sure if that is needed | |||
| int got_frame = 0; | |||
| ret = avcodec_decode_audio4(priv->codecContext, &avf, &got_frame, &priv->packet); | |||
| data_size = avf.linesize[0]; | |||
| memcpy(priv->m_tmpBuffer, avf.data[0], avf.linesize[0] * sizeof(uint8_t)); | |||
| #else // this was deprecated in LIBAVCODEC_VERSION_MAJOR 53 | |||
| ret = avcodec_decode_audio3(priv->codecContext, | |||
| priv->m_tmpBuffer, &data_size, &priv->packet); | |||
| #endif | |||
| if (ret < 0 || ret > priv->pkt_len) { | |||
| #if 0 | |||
| dbg(0, "audio decode error"); | |||
| return -1; | |||
| #endif | |||
| priv->pkt_len=0; | |||
| ret=0; | |||
| continue; | |||
| } | |||
| priv->pkt_len -= ret; priv->pkt_ptr += ret; | |||
| /* sample exact alignment */ | |||
| if (priv->packet.pts != AV_NOPTS_VALUE) { | |||
| priv->decoder_clock = priv->samplerate * av_q2d(priv->formatContext->streams[priv->audioStream]->time_base) * priv->packet.pts; | |||
| } else { | |||
| dbg(0, "!!! NO PTS timestamp in file"); | |||
| priv->decoder_clock += (data_size>>1) / priv->channels; | |||
| } | |||
| if (data_size>0) { | |||
| priv->m_tmpBufferLen+= (data_size>>1); // 2 bytes per sample | |||
| } | |||
| /* align buffer after seek. */ | |||
| if (priv->seek_frame > 0) { | |||
| const int diff = priv->output_clock-priv->decoder_clock; | |||
| if (diff<0) { | |||
| /* seek ended up past the wanted sample */ | |||
| dbg(0, " !!! Audio seek failed."); | |||
| return -1; | |||
| } else if (priv->m_tmpBufferLen < (diff*priv->channels)) { | |||
| /* wanted sample not in current buffer - keep going */ | |||
| dbg(2, " !!! seeked sample was not in decoded buffer. frames-to-go: %li", diff); | |||
| priv->m_tmpBufferLen = 0; | |||
| } else if (diff!=0 && data_size > 0) { | |||
| /* wanted sample is in current buffer but not at the beginnning */ | |||
| dbg(2, " !!! sync buffer to seek. (diff:%i)", diff); | |||
| priv->m_tmpBufferStart+= diff*priv->codecContext->channels; | |||
| priv->m_tmpBufferLen -= diff*priv->codecContext->channels; | |||
| #if 1 | |||
| memmove(priv->m_tmpBuffer, priv->m_tmpBufferStart, priv->m_tmpBufferLen); | |||
| priv->m_tmpBufferStart = priv->m_tmpBuffer; | |||
| #endif | |||
| priv->seek_frame=0; | |||
| priv->decoder_clock += diff; | |||
| } else if (data_size > 0) { | |||
| dbg(2, "Audio exact sync-seek (%"PRIi64" == %"PRIi64")", priv->decoder_clock, priv->seek_frame); | |||
| priv->seek_frame=0; | |||
| } else { | |||
| dbg(0, "Error: no audio data in packet"); | |||
| } | |||
| } | |||
| //dbg(0, "PTS: decoder:%"PRIi64". - want: %"PRIi64, priv->decoder_clock, priv->output_clock); | |||
| //dbg(0, "CLK: frame: %"PRIi64" T:%.3fs",priv->decoder_clock, (float) priv->decoder_clock/priv->samplerate); | |||
| } | |||
| } | |||
| if (written!=frames) { | |||
| dbg(2, "short-read"); | |||
| } | |||
| return written * priv->channels; | |||
| } | |||
| static int64_t ad_seek_ffmpeg(void *sf, int64_t pos) { | |||
| ffmpeg_audio_decoder *priv = (ffmpeg_audio_decoder*) sf; | |||
| if (!sf) return -1; | |||
| if (pos == priv->output_clock) return pos; | |||
| /* flush internal buffer */ | |||
| priv->m_tmpBufferLen = 0; | |||
| priv->seek_frame = pos; | |||
| priv->output_clock = pos; | |||
| priv->pkt_len = 0; priv->pkt_ptr = NULL; | |||
| priv->decoder_clock = 0; | |||
| #if 0 | |||
| /* TODO seek at least 1 packet before target. | |||
| * for mpeg compressed files, the | |||
| * output may depend on past frames! */ | |||
| if (pos > 8192) pos -= 8192; | |||
| else pos = 0; | |||
| #endif | |||
| const int64_t timestamp = pos / av_q2d(priv->formatContext->streams[priv->audioStream]->time_base) / priv->samplerate; | |||
| dbg(2, "seek frame:%"PRIi64" - idx:%"PRIi64, pos, timestamp); | |||
| av_seek_frame(priv->formatContext, priv->audioStream, timestamp, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD); | |||
| avcodec_flush_buffers(priv->codecContext); | |||
| return pos; | |||
| } | |||
| static int ad_eval_ffmpeg(const char *f) { | |||
| char *ext = strrchr(f, '.'); | |||
| if (!ext) return 10; | |||
| // libavformat.. guess_format.. | |||
| return 40; | |||
| } | |||
| #endif | |||
| static const ad_plugin ad_ffmpeg = { | |||
| #ifdef HAVE_FFMPEG | |||
| &ad_eval_ffmpeg, | |||
| &ad_open_ffmpeg, | |||
| &ad_close_ffmpeg, | |||
| &ad_info_ffmpeg, | |||
| &ad_seek_ffmpeg, | |||
| &ad_read_ffmpeg | |||
| #else | |||
| &ad_eval_null, | |||
| &ad_open_null, | |||
| &ad_close_null, | |||
| &ad_info_null, | |||
| &ad_seek_null, | |||
| &ad_read_null | |||
| #endif | |||
| }; | |||
| /* dlopen handler */ | |||
| const ad_plugin * adp_get_ffmpeg() { | |||
| #ifdef HAVE_FFMPEG | |||
| static int ffinit = 0; | |||
| if (!ffinit) { | |||
| ffinit=1; | |||
| #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 5, 0) | |||
| avcodec_init(); | |||
| #endif | |||
| av_register_all(); | |||
| avcodec_register_all(); | |||
| if(ad_debug_level <= 1) | |||
| av_log_set_level(AV_LOG_QUIET); | |||
| else | |||
| av_log_set_level(AV_LOG_VERBOSE); | |||
| } | |||
| #endif | |||
| return &ad_ffmpeg; | |||
| } | |||
| @@ -0,0 +1,176 @@ | |||
| /** | |||
| Copyright (C) 2011-2013 Robin Gareus <robin@gareus.org> | |||
| This program is free software; you can redistribute it and/or modify | |||
| it under the terms of the GNU Lesser Public License as published by | |||
| the Free Software Foundation; either version 2.1, or (at your option) | |||
| any later version. | |||
| This program is distributed in the hope that it will be useful, | |||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| GNU Lesser Public License for more details. | |||
| You should have received a copy of the GNU Lesser General Public | |||
| License along with this library; if not, write to the Free Software | |||
| Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |||
| */ | |||
| #include "config.h" | |||
| #include <stdio.h> | |||
| #include <stdlib.h> | |||
| #include <stdarg.h> | |||
| #include <string.h> | |||
| #include <unistd.h> | |||
| #include <math.h> | |||
| #include "audio_decoder/ad_plugin.h" | |||
| int ad_debug_level = 0; | |||
| #define UNUSED(x) (void)(x) | |||
| int ad_eval_null(const char *f) { UNUSED(f); return -1; } | |||
| void * ad_open_null(const char *f, struct adinfo *n) { UNUSED(f); UNUSED(n); return NULL; } | |||
| int ad_close_null(void *x) { UNUSED(x); return -1; } | |||
| int ad_info_null(void *x, struct adinfo *n) { UNUSED(x); UNUSED(n); return -1; } | |||
| int64_t ad_seek_null(void *x, int64_t p) { UNUSED(x); UNUSED(p); return -1; } | |||
| ssize_t ad_read_null(void *x, float*d, size_t s) { UNUSED(x); UNUSED(d); UNUSED(s); return -1;} | |||
| typedef struct { | |||
| ad_plugin const *b; ///< decoder back-end | |||
| void *d; ///< backend data | |||
| } adecoder; | |||
| /* samplecat api */ | |||
| void ad_init() { /* global init */ } | |||
| static ad_plugin const * choose_backend(const char *fn) { | |||
| int max, val; | |||
| ad_plugin const *b=NULL; | |||
| max=0; | |||
| val=adp_get_sndfile()->eval(fn); | |||
| if (val>max) {max=val; b=adp_get_sndfile();} | |||
| val=adp_get_ffmpeg()->eval(fn); | |||
| if (val>max) {max=val; b=adp_get_ffmpeg();} | |||
| return b; | |||
| } | |||
| void *ad_open(const char *fn, struct adinfo *nfo) { | |||
| adecoder *d = (adecoder*) calloc(1, sizeof(adecoder)); | |||
| ad_clear_nfo(nfo); | |||
| d->b = choose_backend(fn); | |||
| if (!d->b) { | |||
| dbg(0, "fatal: no decoder backend available"); | |||
| free(d); | |||
| return NULL; | |||
| } | |||
| d->d = d->b->open(fn, nfo); | |||
| if (!d->d) { | |||
| free(d); | |||
| return NULL; | |||
| } | |||
| return (void*)d; | |||
| } | |||
| int ad_info(void *sf, struct adinfo *nfo) { | |||
| adecoder *d = (adecoder*) sf; | |||
| if (!d) return -1; | |||
| return d->b->info(d->d, nfo); | |||
| } | |||
| int ad_close(void *sf) { | |||
| adecoder *d = (adecoder*) sf; | |||
| if (!d) return -1; | |||
| int rv = d->b->close(d->d); | |||
| free(d); | |||
| return rv; | |||
| } | |||
| int64_t ad_seek(void *sf, int64_t pos) { | |||
| adecoder *d = (adecoder*) sf; | |||
| if (!d) return -1; | |||
| return d->b->seek(d->d, pos); | |||
| } | |||
| ssize_t ad_read(void *sf, float* out, size_t len){ | |||
| adecoder *d = (adecoder*) sf; | |||
| if (!d) return -1; | |||
| return d->b->read(d->d, out, len); | |||
| } | |||
| /* | |||
| * side-effects: allocates buffer | |||
| */ | |||
| ssize_t ad_read_mono_dbl(void *sf, struct adinfo *nfo, double* d, size_t len){ | |||
| unsigned int c,f; | |||
| unsigned int chn = nfo->channels; | |||
| if (len<1) return 0; | |||
| static float *buf = NULL; | |||
| static size_t bufsiz = 0; | |||
| if (!buf || bufsiz != len*chn) { | |||
| bufsiz=len*chn; | |||
| buf = (float*) realloc((void*)buf, bufsiz * sizeof(float)); | |||
| } | |||
| len = ad_read(sf, buf, bufsiz); | |||
| for (f=0;f< (len/chn);f++) { | |||
| double val=0.0; | |||
| for (c=0;c<chn;c++) { | |||
| val+=buf[f*chn + c]; | |||
| } | |||
| d[f]= val/chn; | |||
| } | |||
| return len/chn; | |||
| } | |||
| int ad_finfo (const char *fn, struct adinfo *nfo) { | |||
| ad_clear_nfo(nfo); | |||
| void * sf = ad_open(fn, nfo); | |||
| return ad_close(sf)?1:0; | |||
| } | |||
| void ad_clear_nfo(struct adinfo *nfo) { | |||
| memset(nfo, 0, sizeof(struct adinfo)); | |||
| } | |||
| void ad_free_nfo(struct adinfo *nfo) { | |||
| if (nfo->meta_data) free(nfo->meta_data); | |||
| } | |||
| void ad_dump_nfo(int dbglvl, struct adinfo *nfo) { | |||
| dbg(dbglvl, "sample_rate: %u", nfo->sample_rate); | |||
| dbg(dbglvl, "channels: %u", nfo->channels); | |||
| dbg(dbglvl, "length: %"PRIi64" ms", nfo->length); | |||
| dbg(dbglvl, "frames: %"PRIi64, nfo->frames); | |||
| dbg(dbglvl, "bit_rate: %d", nfo->bit_rate); | |||
| dbg(dbglvl, "bit_depth: %d", nfo->bit_depth); | |||
| dbg(dbglvl, "channels: %u", nfo->channels); | |||
| dbg(dbglvl, "meta-data: %s", nfo->meta_data?nfo->meta_data:"-"); | |||
| } | |||
| void ad_debug_printf(const char* func, int level, const char* format, ...) { | |||
| va_list args; | |||
| va_start(args, format); | |||
| if (level <= ad_debug_level) { | |||
| fprintf(stderr, "%s(): ", func); | |||
| vfprintf(stderr, format, args); | |||
| fprintf(stderr, "\n"); | |||
| } | |||
| va_end(args); | |||
| } | |||
| void ad_set_debuglevel(int lvl) { | |||
| ad_debug_level = lvl; | |||
| if (ad_debug_level<-1) ad_debug_level=-1; | |||
| if (ad_debug_level>3) ad_debug_level=3; | |||
| } | |||
| @@ -0,0 +1,64 @@ | |||
| /** | |||
| Copyright (C) 2011-2013 Robin Gareus <robin@gareus.org> | |||
| This program is free software; you can redistribute it and/or modify | |||
| it under the terms of the GNU Lesser Public License as published by | |||
| the Free Software Foundation; either version 2.1, or (at your option) | |||
| any later version. | |||
| This program is distributed in the hope that it will be useful, | |||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| GNU Lesser Public License for more details. | |||
| You should have received a copy of the GNU Lesser General Public | |||
| License along with this library; if not, write to the Free Software | |||
| Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |||
| */ | |||
| #ifndef __AD_PLUGIN_H__ | |||
| #define __AD_PLUGIN_H__ | |||
| #include <stdint.h> | |||
| #include "audio_decoder/ad.h" | |||
| #define dbg(A, B, ...) ad_debug_printf(__func__, A, B, ##__VA_ARGS__) | |||
| #ifndef __PRI64_PREFIX | |||
| #if (defined __X86_64__ || defined __LP64__) | |||
| # define __PRI64_PREFIX "l" | |||
| #else | |||
| # define __PRI64_PREFIX "ll" | |||
| #endif | |||
| #endif | |||
| #ifndef PRIu64 | |||
| # define PRIu64 __PRI64_PREFIX "u" | |||
| #endif | |||
| #ifndef PRIi64 | |||
| # define PRIi64 __PRI64_PREFIX "i" | |||
| #endif | |||
| extern int ad_debug_level; | |||
| void ad_debug_printf(const char* func, int level, const char* format, ...); | |||
| typedef struct { | |||
| int (*eval)(const char *); | |||
| void * (*open)(const char *, struct adinfo *); | |||
| int (*close)(void *); | |||
| int (*info)(void *, struct adinfo *); | |||
| int64_t (*seek)(void *, int64_t); | |||
| ssize_t (*read)(void *, float *, size_t); | |||
| } ad_plugin; | |||
| int ad_eval_null(const char *); | |||
| void * ad_open_null(const char *, struct adinfo *); | |||
| int ad_close_null(void *); | |||
| int ad_info_null(void *, struct adinfo *); | |||
| int64_t ad_seek_null(void *, int64_t); | |||
| ssize_t ad_read_null(void *, float*, size_t); | |||
| /* hardcoded backends */ | |||
| const ad_plugin * adp_get_sndfile(); | |||
| const ad_plugin * adp_get_ffmpeg(); | |||
| #endif | |||
| @@ -0,0 +1,154 @@ | |||
| /** | |||
| Copyright (C) 2011-2013 Robin Gareus <robin@gareus.org> | |||
| This program is free software; you can redistribute it and/or modify | |||
| it under the terms of the GNU Lesser Public License as published by | |||
| the Free Software Foundation; either version 2.1, or (at your option) | |||
| any later version. | |||
| This program is distributed in the hope that it will be useful, | |||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| GNU Lesser Public License for more details. | |||
| You should have received a copy of the GNU Lesser General Public | |||
| License along with this library; if not, write to the Free Software | |||
| Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |||
| */ | |||
| #include "config.h" | |||
| #include <stdio.h> | |||
| #include <stdlib.h> | |||
| #include <string.h> | |||
| #include <strings.h> | |||
| #include <unistd.h> | |||
| #include <math.h> | |||
| #include <sndfile.h> | |||
| #include "audio_decoder/ad_plugin.h" | |||
| /* internal abstraction */ | |||
| typedef struct { | |||
| SF_INFO sfinfo; | |||
| SNDFILE *sffile; | |||
| } sndfile_audio_decoder; | |||
| static int parse_bit_depth(int format) { | |||
| /* see http://www.mega-nerd.com/libsndfile/api.html */ | |||
| switch (format&0x0f) { | |||
| case SF_FORMAT_PCM_S8: return 8; | |||
| case SF_FORMAT_PCM_16: return 16; /* Signed 16 bit data */ | |||
| case SF_FORMAT_PCM_24: return 24; /* Signed 24 bit data */ | |||
| case SF_FORMAT_PCM_32: return 32; /* Signed 32 bit data */ | |||
| case SF_FORMAT_PCM_U8: return 8; /* Unsigned 8 bit data (WAV and RAW only) */ | |||
| case SF_FORMAT_FLOAT : return 32; /* 32 bit float data */ | |||
| case SF_FORMAT_DOUBLE: return 64; /* 64 bit float data */ | |||
| default: break; | |||
| } | |||
| return 0; | |||
| } | |||
| static int ad_info_sndfile(void *sf, struct adinfo *nfo) { | |||
| sndfile_audio_decoder *priv = (sndfile_audio_decoder*) sf; | |||
| if (!priv) return -1; | |||
| if (nfo) { | |||
| nfo->channels = priv->sfinfo.channels; | |||
| nfo->frames = priv->sfinfo.frames; | |||
| nfo->sample_rate = priv->sfinfo.samplerate; | |||
| nfo->length = priv->sfinfo.samplerate ? (priv->sfinfo.frames * 1000) / priv->sfinfo.samplerate : 0; | |||
| nfo->bit_depth = parse_bit_depth(priv->sfinfo.format); | |||
| nfo->bit_rate = nfo->bit_depth * nfo->channels * nfo->sample_rate; | |||
| nfo->meta_data = NULL; | |||
| } | |||
| return 0; | |||
| } | |||
| static void *ad_open_sndfile(const char *fn, struct adinfo *nfo) { | |||
| sndfile_audio_decoder *priv = (sndfile_audio_decoder*) calloc(1, sizeof(sndfile_audio_decoder)); | |||
| priv->sfinfo.format=0; | |||
| if(!(priv->sffile = sf_open(fn, SFM_READ, &priv->sfinfo))){ | |||
| dbg(0, "unable to open file '%s'.", fn); | |||
| puts(sf_strerror(NULL)); | |||
| int e = sf_error(NULL); | |||
| dbg(0, "error=%i", e); | |||
| free(priv); | |||
| return NULL; | |||
| } | |||
| ad_info_sndfile(priv, nfo); | |||
| return (void*) priv; | |||
| } | |||
| static int ad_close_sndfile(void *sf) { | |||
| sndfile_audio_decoder *priv = (sndfile_audio_decoder*) sf; | |||
| if (!priv) return -1; | |||
| if(!sf || sf_close(priv->sffile)) { | |||
| dbg(0, "fatal: bad file close.\n"); | |||
| return -1; | |||
| } | |||
| free(priv); | |||
| return 0; | |||
| } | |||
| static int64_t ad_seek_sndfile(void *sf, int64_t pos) { | |||
| sndfile_audio_decoder *priv = (sndfile_audio_decoder*) sf; | |||
| if (!priv) return -1; | |||
| return sf_seek(priv->sffile, pos, SEEK_SET); | |||
| } | |||
| static ssize_t ad_read_sndfile(void *sf, float* d, size_t len) { | |||
| sndfile_audio_decoder *priv = (sndfile_audio_decoder*) sf; | |||
| if (!priv) return -1; | |||
| return sf_read_float (priv->sffile, d, len); | |||
| } | |||
| static int ad_eval_sndfile(const char *f) { | |||
| char *ext = strrchr(f, '.'); | |||
| if (!ext) return 5; | |||
| /* see http://www.mega-nerd.com/libsndfile/ */ | |||
| if (!strcasecmp(ext, ".wav")) return 100; | |||
| if (!strcasecmp(ext, ".aiff")) return 100; | |||
| if (!strcasecmp(ext, ".aifc")) return 100; | |||
| if (!strcasecmp(ext, ".snd")) return 100; | |||
| if (!strcasecmp(ext, ".au")) return 100; | |||
| if (!strcasecmp(ext, ".paf")) return 100; | |||
| if (!strcasecmp(ext, ".iff")) return 100; | |||
| if (!strcasecmp(ext, ".svx")) return 100; | |||
| if (!strcasecmp(ext, ".sf")) return 100; | |||
| if (!strcasecmp(ext, ".vcc")) return 100; | |||
| if (!strcasecmp(ext, ".w64")) return 100; | |||
| if (!strcasecmp(ext, ".mat4")) return 100; | |||
| if (!strcasecmp(ext, ".mat5")) return 100; | |||
| if (!strcasecmp(ext, ".pvf5")) return 100; | |||
| if (!strcasecmp(ext, ".xi")) return 100; | |||
| if (!strcasecmp(ext, ".htk")) return 100; | |||
| if (!strcasecmp(ext, ".pvf")) return 100; | |||
| if (!strcasecmp(ext, ".sd2")) return 100; | |||
| // libsndfile >= 1.0.18 | |||
| if (!strcasecmp(ext, ".flac")) return 80; | |||
| if (!strcasecmp(ext, ".ogg")) return 80; | |||
| return 0; | |||
| } | |||
| static const ad_plugin ad_sndfile = { | |||
| #if 1 | |||
| &ad_eval_sndfile, | |||
| &ad_open_sndfile, | |||
| &ad_close_sndfile, | |||
| &ad_info_sndfile, | |||
| &ad_seek_sndfile, | |||
| &ad_read_sndfile | |||
| #else | |||
| &ad_eval_null, | |||
| &ad_open_null, | |||
| &ad_close_null, | |||
| &ad_info_null, | |||
| &ad_seek_null, | |||
| &ad_read_null | |||
| #endif | |||
| }; | |||
| /* dlopen handler */ | |||
| const ad_plugin * adp_get_sndfile() { | |||
| return &ad_sndfile; | |||
| } | |||
| @@ -0,0 +1 @@ | |||
| #define HAVE_FFMPEG 1 | |||
| @@ -0,0 +1,81 @@ | |||
| /* ffmpeg compatibility wrappers | |||
| * | |||
| * Copyright 2012 Robin Gareus <robin@gareus.org> | |||
| * | |||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |||
| * of this software and associated documentation files (the "Software"), to deal | |||
| * in the Software without restriction, including without limitation the rights | |||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||
| * copies of the Software, and to permit persons to whom the Software is | |||
| * furnished to do so, subject to the following conditions: | |||
| * | |||
| * 1. Redistributions of source code must retain the above copyright notice, this | |||
| * list of conditions and the following disclaimer. | |||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | |||
| * this list of conditions and the following disclaimer in the documentation | |||
| * and/or other materials provided with the distribution. | |||
| * | |||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |||
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |||
| * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |||
| * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |||
| * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |||
| * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |||
| * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |||
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
| */ | |||
| #include <libavcodec/avcodec.h> | |||
| #include <libavformat/avformat.h> | |||
| #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 2, 0) | |||
| static inline int avformat_open_input(AVFormatContext **ps, const char *filename, void *fmt, void **options) | |||
| { | |||
| return av_open_input_file(ps, filename, NULL, 0, NULL); | |||
| } | |||
| #endif | |||
| #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 5, 0) | |||
| static inline AVCodecContext * | |||
| avcodec_alloc_context3(AVCodec *codec __attribute__((unused))) | |||
| { | |||
| return avcodec_alloc_context(); | |||
| } | |||
| static inline AVStream * | |||
| avformat_new_stream(AVFormatContext *s, AVCodec *c) { | |||
| return av_new_stream(s,0); | |||
| } | |||
| static inline int | |||
| avcodec_get_context_defaults3(AVCodecContext *s, AVCodec *codec) | |||
| { | |||
| avcodec_get_context_defaults(s); | |||
| return 0; | |||
| } | |||
| #endif /* < 53.5.0 */ | |||
| #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 5, 6) | |||
| static inline int | |||
| avcodec_open2(AVCodecContext *avctx, AVCodec *codec, void **options __attribute__((unused))) | |||
| { | |||
| return avcodec_open(avctx, codec); | |||
| } | |||
| #endif /* <= 53.5.6 */ | |||
| #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 5, 0) | |||
| static inline int | |||
| avformat_find_stream_info(AVFormatContext *ic, void **options) | |||
| { | |||
| return av_find_stream_info(ic); | |||
| } | |||
| static inline void | |||
| avformat_close_input(AVFormatContext **s) | |||
| { | |||
| av_close_input_file(*s); | |||
| } | |||
| #endif /* < 53.5.0 */ | |||
| @@ -0,0 +1,422 @@ | |||
| /* | |||
| * Carla Native Plugins | |||
| * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com> | |||
| * | |||
| * This program is free software; you can redistribute it and/or | |||
| * modify it under the terms of the GNU General Public License as | |||
| * published by the Free Software Foundation; either version 2 of | |||
| * the License, or any later version. | |||
| * | |||
| * This program is distributed in the hope that it will be useful, | |||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
| * GNU General Public License for more details. | |||
| * | |||
| * For a full copy of the GNU General Public License see the GPL.txt file | |||
| */ | |||
| #include "CarlaNative.h" | |||
| #include "audio_decoder/ad.h" | |||
| #include <pthread.h> | |||
| #include <stdlib.h> | |||
| #include <stdio.h> | |||
| #include <unistd.h> | |||
| typedef struct adinfo ADInfo; | |||
| typedef pthread_mutex_t Mutex; | |||
| typedef pthread_t Thread; | |||
| typedef struct _AudioFilePool { | |||
| float* buffer[2]; | |||
| uint32_t startFrame; | |||
| uint32_t size; | |||
| } AudioFilePool; | |||
| typedef struct _AudioFileInstance { | |||
| HostDescriptor* host; | |||
| void* filePtr; | |||
| ADInfo fileNfo; | |||
| uint32_t lastFrame; | |||
| uint32_t maxFrame; | |||
| AudioFilePool pool; | |||
| bool needsRead; | |||
| bool doProcess; | |||
| bool doQuit; | |||
| Mutex mutex; | |||
| Thread thread; | |||
| } AudioFileInstance; | |||
| // ------------------------------------------------------------------------------------------ | |||
| static bool gADInitiated = false; | |||
| // ------------------------------------------------------------------------------------------ | |||
| void zeroFloat(float* data, unsigned size) | |||
| { | |||
| for (unsigned i=0; i < size; ++i) | |||
| *data++ = 0.0f; | |||
| } | |||
| void audiofile_read_poll(AudioFileInstance* const handlePtr) | |||
| { | |||
| if (handlePtr->fileNfo.frames == 0) | |||
| { | |||
| fprintf(stderr, "R: no song loaded\n"); | |||
| handlePtr->needsRead = false; | |||
| return; | |||
| } | |||
| int64_t lastFrame = handlePtr->lastFrame; | |||
| if (lastFrame >= handlePtr->maxFrame) | |||
| { | |||
| //fprintf(stderr, "R: transport out of bounds\n"); | |||
| handlePtr->needsRead = false; | |||
| return; | |||
| } | |||
| // temp data buffer | |||
| const uint32_t tmpSize = handlePtr->pool.size * handlePtr->fileNfo.channels; | |||
| float tmpData[tmpSize]; | |||
| zeroFloat(tmpData, tmpSize); | |||
| { | |||
| fprintf(stderr, "R: poll data - reading at %li:%02li\n", lastFrame/44100/60, (lastFrame/44100) % 60); | |||
| ad_seek(handlePtr->filePtr, lastFrame); | |||
| ssize_t i, j, rv = ad_read(handlePtr->filePtr, tmpData, tmpSize); | |||
| { | |||
| // lock, and put data asap | |||
| pthread_mutex_lock(&handlePtr->mutex); | |||
| //zeroFloat(handlePtr->pool.buffer[0], handlePtr->pool.size); | |||
| //zeroFloat(handlePtr->pool.buffer[1], handlePtr->pool.size); | |||
| for (i=0, j=0; i < handlePtr->pool.size && j < rv; j++) | |||
| { | |||
| if (handlePtr->fileNfo.channels == 1) | |||
| { | |||
| handlePtr->pool.buffer[0][i] = tmpData[j]; | |||
| handlePtr->pool.buffer[1][i] = tmpData[j]; | |||
| i++; | |||
| } | |||
| else | |||
| { | |||
| if (j % 2 == 0) | |||
| { | |||
| handlePtr->pool.buffer[0][i] = tmpData[j]; | |||
| } | |||
| else | |||
| { | |||
| handlePtr->pool.buffer[1][i] = tmpData[j]; | |||
| i++; | |||
| } | |||
| } | |||
| } | |||
| for (; i < handlePtr->pool.size; i++) | |||
| { | |||
| handlePtr->pool.buffer[0][i] = 0.0f; | |||
| handlePtr->pool.buffer[1][i] = 0.0f; | |||
| } | |||
| handlePtr->pool.startFrame = lastFrame; | |||
| // done | |||
| pthread_mutex_unlock(&handlePtr->mutex); | |||
| } | |||
| } | |||
| handlePtr->needsRead = false; | |||
| } | |||
| void audiofile_load_filename(AudioFileInstance* const handlePtr, const char* const filename) | |||
| { | |||
| // wait for jack processing to end | |||
| handlePtr->doProcess = false; | |||
| pthread_mutex_lock(&handlePtr->mutex); | |||
| pthread_mutex_unlock(&handlePtr->mutex); | |||
| // clear old data | |||
| if (handlePtr->filePtr != NULL) | |||
| { | |||
| ad_close(handlePtr->filePtr); | |||
| handlePtr->filePtr = NULL; | |||
| } | |||
| ad_clear_nfo(&handlePtr->fileNfo); | |||
| // open new | |||
| handlePtr->filePtr = ad_open(filename, &handlePtr->fileNfo); | |||
| if (handlePtr->filePtr != NULL) | |||
| { | |||
| ad_dump_nfo(1, &handlePtr->fileNfo); | |||
| if (handlePtr->fileNfo.channels == 1 || handlePtr->fileNfo.channels == 2) | |||
| { | |||
| handlePtr->maxFrame = handlePtr->fileNfo.frames; | |||
| audiofile_read_poll(handlePtr); | |||
| handlePtr->doProcess = true; | |||
| } | |||
| else | |||
| { | |||
| ad_close(handlePtr->filePtr); | |||
| handlePtr->filePtr = NULL; | |||
| ad_clear_nfo(&handlePtr->fileNfo); | |||
| } | |||
| } | |||
| } | |||
| static void audiofile_thread_idle(void* ptr) | |||
| { | |||
| AudioFileInstance* const handlePtr = (AudioFileInstance*)ptr; | |||
| while (! handlePtr->doQuit) | |||
| { | |||
| if (handlePtr->needsRead || handlePtr->lastFrame - handlePtr->pool.startFrame >= handlePtr->pool.size*3/4) | |||
| audiofile_read_poll(handlePtr); | |||
| else | |||
| usleep(50*1000); | |||
| } | |||
| pthread_exit(0); | |||
| } | |||
| // ------------------------------------------------------------------------------------------ | |||
| static PluginHandle audiofile_instantiate(const PluginDescriptor* _this_, HostDescriptor* host) | |||
| { | |||
| AudioFileInstance* const handlePtr = (AudioFileInstance*)malloc(sizeof(AudioFileInstance)); | |||
| if (handlePtr == NULL) | |||
| return NULL; | |||
| if (! gADInitiated) | |||
| { | |||
| ad_init(); | |||
| gADInitiated = true; | |||
| } | |||
| // init | |||
| handlePtr->host = host; | |||
| handlePtr->filePtr = NULL; | |||
| handlePtr->lastFrame = 0; | |||
| handlePtr->maxFrame = 0; | |||
| handlePtr->pool.buffer[0] = NULL; | |||
| handlePtr->pool.buffer[1] = NULL; | |||
| handlePtr->pool.startFrame = 0; | |||
| handlePtr->pool.size = 0; | |||
| handlePtr->needsRead = false; | |||
| handlePtr->doProcess = false; | |||
| handlePtr->doQuit = false; | |||
| ad_clear_nfo(&handlePtr->fileNfo); | |||
| pthread_mutex_init(&handlePtr->mutex, NULL); | |||
| // create audio pool | |||
| handlePtr->pool.size = host->get_sample_rate(host->handle) * 6; // 6 secs | |||
| handlePtr->pool.buffer[0] = (float*)malloc(sizeof(float) * handlePtr->pool.size); | |||
| handlePtr->pool.buffer[1] = (float*)malloc(sizeof(float) * handlePtr->pool.size); | |||
| if (handlePtr->pool.buffer[0] == NULL || handlePtr->pool.buffer[1] == NULL) | |||
| { | |||
| free(handlePtr); | |||
| return NULL; | |||
| } | |||
| zeroFloat(handlePtr->pool.buffer[0], handlePtr->pool.size); | |||
| zeroFloat(handlePtr->pool.buffer[1], handlePtr->pool.size); | |||
| pthread_create(&handlePtr->thread, NULL, (void*)&audiofile_thread_idle, handlePtr); | |||
| // load file, TESTING | |||
| // wait for jack processing to end | |||
| handlePtr->doProcess = false; | |||
| pthread_mutex_lock(&handlePtr->mutex); | |||
| pthread_mutex_unlock(&handlePtr->mutex); | |||
| return handlePtr; | |||
| // unused | |||
| (void)_this_; | |||
| } | |||
| static void audiofile_cleanup(PluginHandle handle) | |||
| { | |||
| AudioFileInstance* const handlePtr = (AudioFileInstance*)handle; | |||
| // wait for processing to end | |||
| handlePtr->doProcess = false; | |||
| handlePtr->doQuit = true; | |||
| pthread_mutex_lock(&handlePtr->mutex); | |||
| pthread_join(handlePtr->thread, NULL); | |||
| pthread_mutex_unlock(&handlePtr->mutex); | |||
| pthread_mutex_destroy(&handlePtr->mutex); | |||
| if (handlePtr->filePtr != NULL) | |||
| ad_close(handlePtr->filePtr); | |||
| if (handlePtr->pool.buffer[0] != NULL) | |||
| free(handlePtr->pool.buffer[0]); | |||
| if (handlePtr->pool.buffer[1] != NULL) | |||
| free(handlePtr->pool.buffer[1]); | |||
| free(handlePtr); | |||
| } | |||
| static void audiofile_set_custom_data(PluginHandle handle, const char* key, const char* value) | |||
| { | |||
| AudioFileInstance* const handlePtr = (AudioFileInstance*)handle; | |||
| if (strcmp(key, "file") == 0) | |||
| audiofile_load_filename(handlePtr, value); | |||
| } | |||
| static void audiofile_process(PluginHandle handle, float** inBuffer, float** outBuffer, uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents) | |||
| { | |||
| AudioFileInstance* const handlePtr = (AudioFileInstance*)handle; | |||
| float* out1 = outBuffer[0]; | |||
| float* out2 = outBuffer[1]; | |||
| if (! handlePtr->doProcess) | |||
| { | |||
| fprintf(stderr, "P: no process\n"); | |||
| zeroFloat(out1, frames); | |||
| zeroFloat(out2, frames); | |||
| return; | |||
| } | |||
| const TimeInfo* const timePos = handlePtr->host->get_time_info(handlePtr->host->handle); | |||
| // not playing | |||
| if (! timePos->playing) | |||
| { | |||
| fprintf(stderr, "P: not rolling\n"); | |||
| handlePtr->lastFrame = timePos->frame; | |||
| zeroFloat(out1, frames); | |||
| zeroFloat(out2, frames); | |||
| return; | |||
| } | |||
| pthread_mutex_lock(&handlePtr->mutex); | |||
| // out of reach | |||
| if (timePos->frame + frames < handlePtr->pool.startFrame || timePos->frame >= handlePtr->maxFrame) | |||
| { | |||
| fprintf(stderr, "P: non-continuous playback, out of reach %u vs %u\n", timePos->frame + frames, handlePtr->maxFrame); | |||
| handlePtr->lastFrame = timePos->frame; | |||
| handlePtr->needsRead = true; | |||
| pthread_mutex_unlock(&handlePtr->mutex); | |||
| zeroFloat(out1, frames); | |||
| zeroFloat(out2, frames); | |||
| return; | |||
| } | |||
| int64_t poolFrame = (int64_t)timePos->frame - handlePtr->pool.startFrame; | |||
| int64_t poolSize = handlePtr->pool.size; | |||
| for (uint32_t i=0; i < frames; i++, poolFrame++) | |||
| { | |||
| if (poolFrame >= 0 && poolFrame < poolSize) | |||
| { | |||
| out1[i] = handlePtr->pool.buffer[0][poolFrame]; | |||
| out2[i] = handlePtr->pool.buffer[1][poolFrame]; | |||
| // reset | |||
| handlePtr->pool.buffer[0][poolFrame] = 0.0f; | |||
| handlePtr->pool.buffer[1][poolFrame] = 0.0f; | |||
| } | |||
| else | |||
| { | |||
| out1[i] = 0.0f; | |||
| out2[i] = 0.0f; | |||
| } | |||
| } | |||
| handlePtr->lastFrame = timePos->frame; | |||
| pthread_mutex_unlock(&handlePtr->mutex); | |||
| return; | |||
| . | |||
| // unused | |||
| (void)inBuffer; | |||
| (void)midiEventCount; | |||
| (void)midiEvents; | |||
| } | |||
| // ----------------------------------------------------------------------- | |||
| static const PluginDescriptor audiofileDesc = { | |||
| .category = PLUGIN_CATEGORY_UTILITY, | |||
| .hints = PLUGIN_IS_RTSAFE|PLUGIN_HAS_GUI, | |||
| .audioIns = 0, | |||
| .audioOuts = 2, | |||
| .midiIns = 0, | |||
| .midiOuts = 0, | |||
| .parameterIns = 0, | |||
| .parameterOuts = 0, | |||
| .name = "Audio File", | |||
| .label = "audiofile", | |||
| .maker = "falkTX", | |||
| .copyright = "GNU GPL v2+", | |||
| .instantiate = audiofile_instantiate, | |||
| .cleanup = audiofile_cleanup, | |||
| .get_parameter_count = NULL, | |||
| .get_parameter_info = NULL, | |||
| .get_parameter_value = NULL, | |||
| .get_parameter_text = NULL, | |||
| .get_midi_program_count = NULL, | |||
| .get_midi_program_info = NULL, | |||
| .set_parameter_value = NULL, | |||
| .set_midi_program = NULL, | |||
| .set_custom_data = audiofile_set_custom_data, | |||
| .ui_show = NULL, | |||
| .ui_idle = NULL, | |||
| .ui_set_parameter_value = NULL, | |||
| .ui_set_midi_program = NULL, | |||
| .ui_set_custom_data = NULL, | |||
| .activate = NULL, | |||
| .deactivate = NULL, | |||
| .process = audiofile_process | |||
| }; | |||
| // ----------------------------------------------------------------------- | |||
| void carla_register_native_plugin_audiofile() | |||
| { | |||
| carla_register_native_plugin(&audiofileDesc); | |||
| } | |||
| // ----------------------------------------------------------------------- | |||
| // amagamated build | |||
| #include "audio_decoder/ad_ffmpeg.c" | |||
| #include "audio_decoder/ad_plugin.c" | |||
| #include "audio_decoder/ad_soundfile.c" | |||
| // ----------------------------------------------------------------------- | |||
| @@ -33,7 +33,7 @@ static void bypass_process(PluginHandle handle, float** inBuffer, float** outBuf | |||
| float* out = outBuffer[0]; | |||
| for (uint32_t i=0; i < frames; i++) | |||
| *in++ = *out++; | |||
| *out++ = *in++; | |||
| return; | |||
| @@ -17,6 +17,8 @@ | |||
| #include "CarlaPluginInternal.hpp" | |||
| #include <QtGui/QFileDialog> | |||
| CARLA_BACKEND_START_NAMESPACE | |||
| struct NativePluginMidiData { | |||
| @@ -458,8 +460,20 @@ public: | |||
| CARLA_ASSERT(fDescriptor != nullptr); | |||
| CARLA_ASSERT(fHandle != nullptr); | |||
| if (fDescriptor != nullptr && fHandle != nullptr && fDescriptor->ui_show != nullptr) | |||
| fDescriptor->ui_show(fHandle, yesNo); | |||
| if (fDescriptor != nullptr && fHandle != nullptr) | |||
| { | |||
| if (fDescriptor->name != nullptr && std::strcmp(fDescriptor->label, "audiofile") == 0) | |||
| { | |||
| QString filenameTry = QFileDialog::getOpenFileName(nullptr, "Open Audio File"); | |||
| if (! filenameTry.isEmpty()) | |||
| fDescriptor->set_custom_data(fHandle, "file", filenameTry.toUtf8().constData()); | |||
| kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr); | |||
| } | |||
| else if (fDescriptor->ui_show != nullptr) | |||
| fDescriptor->ui_show(fHandle, yesNo); | |||
| } | |||
| } | |||
| void idleGui() | |||
| @@ -984,6 +998,37 @@ public: | |||
| } | |||
| } | |||
| CARLA_PROCESS_CONTINUE_CHECK; | |||
| // -------------------------------------------------------------------------------------------------------- | |||
| // Set TimeInfo | |||
| const EngineTimeInfo& timeInfo = kData->engine->getTimeInfo(); | |||
| fTimeInfo.playing = timeInfo.playing; | |||
| fTimeInfo.frame = timeInfo.frame; | |||
| fTimeInfo.time = timeInfo.time; | |||
| if (timeInfo.valid & EngineTimeInfo::ValidBBT) | |||
| { | |||
| fTimeInfo.bbt.valid = true; | |||
| fTimeInfo.bbt.bar = timeInfo.bbt.bar; | |||
| fTimeInfo.bbt.beat = timeInfo.bbt.beat; | |||
| fTimeInfo.bbt.tick = timeInfo.bbt.tick; | |||
| fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick; | |||
| fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar; | |||
| fTimeInfo.bbt.beatType = timeInfo.bbt.beatType; | |||
| fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat; | |||
| fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute; | |||
| } | |||
| else | |||
| fTimeInfo.bbt.valid = false; | |||
| CARLA_PROCESS_CONTINUE_CHECK; | |||
| // -------------------------------------------------------------------------------------------------------- | |||
| // Event Input and Processing | |||
| @@ -1417,6 +1462,7 @@ public: | |||
| fDescriptor->process(fHandle2, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents); | |||
| fIsProcessing = false; | |||
| fTimeInfo.frame += frames; | |||
| for (uint32_t i=0, k; i < kData->audioOut.count; i++) | |||
| { | |||
| @@ -1510,8 +1556,9 @@ protected: | |||
| const ::TimeInfo* handleGetTimeInfo() | |||
| { | |||
| // TODO | |||
| return nullptr; | |||
| CARLA_ASSERT(fIsProcessing); | |||
| return &fTimeInfo; | |||
| } | |||
| bool handleWriteMidiEvent(const MidiEvent* const event) | |||
| @@ -1605,6 +1652,10 @@ public: | |||
| carla_register_native_plugin_PingPongPan(); | |||
| #endif | |||
| # ifdef WANT_AUDIOFILE | |||
| carla_register_native_plugin_audiofile(); | |||
| # endif | |||
| # ifdef WANT_ZYNADDSUBFX | |||
| carla_register_native_plugin_zynaddsubfx(); | |||
| # endif | |||
| @@ -1696,6 +1747,8 @@ private: | |||
| NativePluginMidiData fMidiIn; | |||
| NativePluginMidiData fMidiOut; | |||
| ::TimeInfo fTimeInfo; | |||
| static bool sFirstInit; | |||
| static std::vector<const PluginDescriptor*> sPluginDescriptors; | |||
| @@ -40,7 +40,11 @@ endif | |||
| # -------------------------------------------------------------- | |||
| # Native | |||
| LINK_FLAGS += -lGL | |||
| LINK_FLAGS += $(shell pkg-config --libs gl) | |||
| ifeq ($(HAVE_AF_DEPS),true) | |||
| LINK_FLAGS += $(shell pkg-config --libs libavcodec libavformat sndfile) -lpthread | |||
| endif | |||
| ifeq ($(HAVE_ZYN_DEPS),true) | |||
| LINK_FLAGS += $(shell pkg-config --libs fftw3 mxml) -lpthread | |||