2007-10-06 22:27:27 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Michael Sevakis
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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 (at your option) any later version.
|
2007-10-06 22:27:27 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
2009-10-22 00:59:42 +00:00
|
|
|
|
|
|
|
/* Define LOGF_ENABLE to enable logf output in this file */
|
|
|
|
/*#define LOGF_ENABLE*/
|
2007-10-06 22:27:27 +00:00
|
|
|
#include "logf.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "sound.h"
|
2008-12-12 11:01:07 +00:00
|
|
|
#include "general.h"
|
2011-06-29 06:37:04 +00:00
|
|
|
#include "pcm-internal.h"
|
|
|
|
#include "pcm_mixer.h"
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Aspects implemented in the target-specific portion:
|
|
|
|
*
|
|
|
|
* ==Playback==
|
|
|
|
* Public -
|
|
|
|
* pcm_postinit
|
|
|
|
* pcm_get_bytes_waiting
|
|
|
|
* pcm_play_lock
|
|
|
|
* pcm_play_unlock
|
|
|
|
* Semi-private -
|
2010-05-24 16:42:32 +00:00
|
|
|
* pcm_play_get_more_callback
|
2007-10-06 22:27:27 +00:00
|
|
|
* pcm_play_dma_init
|
2011-09-01 12:15:43 +00:00
|
|
|
* pcm_play_dma_postinit
|
2007-10-06 22:27:27 +00:00
|
|
|
* pcm_play_dma_start
|
|
|
|
* pcm_play_dma_stop
|
|
|
|
* pcm_play_dma_pause
|
|
|
|
* pcm_play_dma_get_peak_buffer
|
|
|
|
* Data Read/Written within TSP -
|
2008-12-12 11:01:07 +00:00
|
|
|
* pcm_sampr (R)
|
|
|
|
* pcm_fsel (R)
|
|
|
|
* pcm_curr_sampr (R)
|
2007-10-06 22:27:27 +00:00
|
|
|
* pcm_playing (R)
|
|
|
|
* pcm_paused (R)
|
|
|
|
*
|
2008-12-12 11:01:07 +00:00
|
|
|
* ==Playback/Recording==
|
2010-05-24 16:42:32 +00:00
|
|
|
* Public -
|
|
|
|
* pcm_dma_addr
|
2008-12-12 11:01:07 +00:00
|
|
|
* Semi-private -
|
|
|
|
* pcm_dma_apply_settings
|
|
|
|
*
|
2007-10-06 22:27:27 +00:00
|
|
|
* ==Recording==
|
|
|
|
* Public -
|
|
|
|
* pcm_rec_lock
|
|
|
|
* pcm_rec_unlock
|
|
|
|
* Semi-private -
|
2010-05-24 16:42:32 +00:00
|
|
|
* pcm_rec_more_ready_callback
|
2007-10-06 22:27:27 +00:00
|
|
|
* pcm_rec_dma_init
|
|
|
|
* pcm_rec_dma_close
|
|
|
|
* pcm_rec_dma_start
|
|
|
|
* pcm_rec_dma_stop
|
|
|
|
* pcm_rec_dma_get_peak_buffer
|
|
|
|
* Data Read/Written within TSP -
|
|
|
|
* pcm_recording (R)
|
|
|
|
*
|
|
|
|
* States are set _after_ the target's pcm driver is called so that it may
|
2007-10-07 23:19:09 +00:00
|
|
|
* know from whence the state is changed. One exception is init.
|
2007-10-06 22:27:27 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-09-01 12:15:43 +00:00
|
|
|
/* 'true' when all stages of pcm initialization have completed */
|
|
|
|
static bool pcm_is_ready = false;
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* the registered callback function to ask for more mp3 data */
|
2011-06-29 06:37:04 +00:00
|
|
|
static pcm_play_callback_type pcm_callback_for_more SHAREDBSS_ATTR = NULL;
|
|
|
|
void (* pcm_play_dma_started)(void) SHAREDBSS_ATTR = NULL;
|
2007-10-06 22:27:27 +00:00
|
|
|
/* PCM playback state */
|
2008-04-06 04:34:57 +00:00
|
|
|
volatile bool pcm_playing SHAREDBSS_ATTR = false;
|
2007-10-06 22:27:27 +00:00
|
|
|
/* PCM paused state. paused implies playing */
|
2008-04-06 04:34:57 +00:00
|
|
|
volatile bool pcm_paused SHAREDBSS_ATTR = false;
|
2007-10-06 22:27:27 +00:00
|
|
|
/* samplerate of currently playing audio - undefined if stopped */
|
2008-04-06 04:34:57 +00:00
|
|
|
unsigned long pcm_curr_sampr SHAREDBSS_ATTR = 0;
|
2008-12-12 11:01:07 +00:00
|
|
|
/* samplerate waiting to be set */
|
|
|
|
unsigned long pcm_sampr SHAREDBSS_ATTR = HW_SAMPR_DEFAULT;
|
|
|
|
/* samplerate frequency selection index */
|
|
|
|
int pcm_fsel SHAREDBSS_ATTR = HW_FREQ_DEFAULT;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2011-07-02 11:55:38 +00:00
|
|
|
/* peak data for the global peak values - i.e. what the final output is */
|
|
|
|
static struct pcm_peaks global_peaks;
|
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
/* Called internally by functions to reset the state */
|
|
|
|
static void pcm_play_stopped(void)
|
|
|
|
{
|
|
|
|
pcm_callback_for_more = NULL;
|
2011-06-29 06:37:04 +00:00
|
|
|
pcm_play_dma_started = NULL;
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_paused = false;
|
|
|
|
pcm_playing = false;
|
|
|
|
}
|
|
|
|
|
2011-09-01 12:15:43 +00:00
|
|
|
static void pcm_wait_for_init(void)
|
|
|
|
{
|
|
|
|
while (!pcm_is_ready)
|
|
|
|
sleep(0);
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/**
|
2010-05-13 04:12:23 +00:00
|
|
|
* Perform peak calculation on a buffer of packed 16-bit samples.
|
2007-10-06 22:27:27 +00:00
|
|
|
*
|
|
|
|
* Used for recording and playback.
|
|
|
|
*/
|
2011-07-02 11:55:38 +00:00
|
|
|
static void pcm_peak_peeker(const int32_t *addr, int count, uint16_t peaks[2])
|
2007-10-06 22:27:27 +00:00
|
|
|
{
|
2010-05-13 04:12:23 +00:00
|
|
|
int peak_l = 0, peak_r = 0;
|
|
|
|
const int32_t * const end = addr + count;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2010-05-13 04:12:23 +00:00
|
|
|
int32_t value = *addr;
|
|
|
|
int ch;
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
#ifdef ROCKBOX_BIG_ENDIAN
|
|
|
|
ch = value >> 16;
|
|
|
|
#else
|
|
|
|
ch = (int16_t)value;
|
|
|
|
#endif
|
2010-05-13 04:12:23 +00:00
|
|
|
if (ch < 0)
|
|
|
|
ch = -ch;
|
|
|
|
if (ch > peak_l)
|
|
|
|
peak_l = ch;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
#ifdef ROCKBOX_BIG_ENDIAN
|
|
|
|
ch = (int16_t)value;
|
|
|
|
#else
|
|
|
|
ch = value >> 16;
|
|
|
|
#endif
|
2010-05-13 04:12:23 +00:00
|
|
|
if (ch < 0)
|
|
|
|
ch = -ch;
|
|
|
|
if (ch > peak_r)
|
|
|
|
peak_r = ch;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2010-05-13 04:12:23 +00:00
|
|
|
addr += 4;
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
2010-05-13 04:12:23 +00:00
|
|
|
while (addr < end);
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2010-05-13 04:12:23 +00:00
|
|
|
peaks[0] = peak_l;
|
|
|
|
peaks[1] = peak_r;
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2011-07-02 11:55:38 +00:00
|
|
|
void pcm_do_peak_calculation(struct pcm_peaks *peaks, bool active,
|
|
|
|
const void *addr, int count)
|
2007-10-06 22:27:27 +00:00
|
|
|
{
|
|
|
|
long tick = current_tick;
|
|
|
|
|
2011-07-02 11:55:38 +00:00
|
|
|
/* Peak no farther ahead than expected period to avoid overcalculation */
|
|
|
|
long period = tick - peaks->tick;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
/* Keep reasonable limits on period */
|
|
|
|
if (period < 1)
|
|
|
|
period = 1;
|
|
|
|
else if (period > HZ/5)
|
|
|
|
period = HZ/5;
|
|
|
|
|
2011-07-02 11:55:38 +00:00
|
|
|
peaks->period = (3*peaks->period + period) >> 2;
|
|
|
|
peaks->tick = tick;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2011-07-02 11:55:38 +00:00
|
|
|
if (active)
|
2007-10-06 22:27:27 +00:00
|
|
|
{
|
2011-07-02 11:55:38 +00:00
|
|
|
int framecount = peaks->period*pcm_curr_sampr / HZ;
|
2007-10-06 22:27:27 +00:00
|
|
|
count = MIN(framecount, count);
|
|
|
|
|
|
|
|
if (count > 0)
|
2011-07-02 11:55:38 +00:00
|
|
|
pcm_peak_peeker((int32_t *)addr, count, peaks->val);
|
2008-12-14 11:42:47 +00:00
|
|
|
/* else keep previous peak values */
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-02 11:55:38 +00:00
|
|
|
/* peaks are zero */
|
|
|
|
peaks->val[0] = peaks->val[1] = 0;
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
2011-07-02 11:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_calculate_peaks(int *left, int *right)
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
const void *addr = pcm_play_dma_get_peak_buffer(&count);
|
|
|
|
|
|
|
|
pcm_do_peak_calculation(&global_peaks, pcm_playing && !pcm_paused,
|
|
|
|
addr, count);
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
if (left)
|
2011-07-02 11:55:38 +00:00
|
|
|
*left = global_peaks.val[0];
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
if (right)
|
2011-07-02 11:55:38 +00:00
|
|
|
*right = global_peaks.val[1];
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2010-02-10 19:44:11 +00:00
|
|
|
const void* pcm_get_peak_buffer(int * count)
|
|
|
|
{
|
|
|
|
return pcm_play_dma_get_peak_buffer(count);
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
bool pcm_is_playing(void)
|
|
|
|
{
|
|
|
|
return pcm_playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pcm_is_paused(void)
|
|
|
|
{
|
|
|
|
return pcm_paused;
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Functions that do not require targeted implementation but only a targeted
|
|
|
|
* interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This should only be called at startup before any audio playback or
|
|
|
|
recording is attempted */
|
|
|
|
void pcm_init(void)
|
|
|
|
{
|
|
|
|
logf("pcm_init");
|
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_play_stopped();
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2008-12-12 11:01:07 +00:00
|
|
|
pcm_set_frequency(HW_SAMPR_DEFAULT);
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
logf(" pcm_play_dma_init");
|
|
|
|
pcm_play_dma_init();
|
|
|
|
}
|
|
|
|
|
2011-09-01 12:15:43 +00:00
|
|
|
/* Finish delayed init */
|
|
|
|
void pcm_postinit(void)
|
|
|
|
{
|
|
|
|
logf("pcm_postinit");
|
|
|
|
|
|
|
|
logf(" pcm_play_dma_postinit");
|
|
|
|
|
|
|
|
pcm_play_dma_postinit();
|
|
|
|
|
|
|
|
pcm_is_ready = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pcm_is_initialized(void)
|
|
|
|
{
|
|
|
|
return pcm_is_ready;
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/* Common code to pcm_play_data and pcm_play_pause */
|
|
|
|
static void pcm_play_data_start(unsigned char *start, size_t size)
|
|
|
|
{
|
2010-05-12 14:05:36 +00:00
|
|
|
start = (unsigned char *)(((uintptr_t)start + 3) & ~3);
|
|
|
|
size &= ~3;
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
if (!(start && size))
|
|
|
|
{
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_play_callback_type get_more = pcm_callback_for_more;
|
2007-10-06 22:27:27 +00:00
|
|
|
size = 0;
|
|
|
|
if (get_more)
|
|
|
|
{
|
|
|
|
logf(" get_more");
|
|
|
|
get_more(&start, &size);
|
2010-05-12 14:05:36 +00:00
|
|
|
|
|
|
|
start = (unsigned char *)(((uintptr_t)start + 3) & ~3);
|
|
|
|
size &= ~3;
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start && size)
|
|
|
|
{
|
|
|
|
logf(" pcm_play_dma_start");
|
2008-12-12 12:46:57 +00:00
|
|
|
pcm_apply_settings();
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_play_dma_start(start, size);
|
|
|
|
pcm_playing = true;
|
|
|
|
pcm_paused = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Force a stop */
|
|
|
|
logf(" pcm_play_dma_stop");
|
|
|
|
pcm_play_dma_stop();
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_play_stopped();
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
void pcm_play_data(pcm_play_callback_type get_more,
|
2007-10-06 22:27:27 +00:00
|
|
|
unsigned char *start, size_t size)
|
|
|
|
{
|
|
|
|
logf("pcm_play_data");
|
|
|
|
|
|
|
|
pcm_play_lock();
|
|
|
|
|
|
|
|
pcm_callback_for_more = get_more;
|
|
|
|
|
2008-07-13 17:34:58 +00:00
|
|
|
logf(" pcm_play_data_start");
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_play_data_start(start, size);
|
|
|
|
|
|
|
|
pcm_play_unlock();
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
void pcm_play_get_more_callback(void **start, size_t *size)
|
|
|
|
{
|
|
|
|
pcm_play_callback_type get_more = pcm_callback_for_more;
|
|
|
|
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
if (get_more && start)
|
|
|
|
{
|
|
|
|
/* Call registered callback */
|
|
|
|
get_more((unsigned char **)start, size);
|
|
|
|
|
|
|
|
*start = (void *)(((uintptr_t)*start + 3) & ~3);
|
|
|
|
*size &= ~3;
|
|
|
|
|
|
|
|
if (*start && *size)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error, callback missing or no more DMA to do */
|
|
|
|
pcm_play_dma_stop();
|
|
|
|
pcm_play_stopped();
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_play_pause(bool play)
|
|
|
|
{
|
|
|
|
logf("pcm_play_pause: %s", play ? "play" : "pause");
|
|
|
|
|
|
|
|
pcm_play_lock();
|
|
|
|
|
|
|
|
if (play == pcm_paused && pcm_playing)
|
|
|
|
{
|
|
|
|
if (!play)
|
|
|
|
{
|
|
|
|
logf(" pcm_play_dma_pause");
|
|
|
|
pcm_play_dma_pause(true);
|
|
|
|
pcm_paused = true;
|
|
|
|
}
|
2008-12-12 12:46:57 +00:00
|
|
|
else if (pcm_get_bytes_waiting() > 0)
|
2007-10-06 22:27:27 +00:00
|
|
|
{
|
2008-12-12 12:46:57 +00:00
|
|
|
logf(" pcm_play_dma_pause");
|
2008-12-12 11:01:07 +00:00
|
|
|
pcm_apply_settings();
|
2008-12-12 12:46:57 +00:00
|
|
|
pcm_play_dma_pause(false);
|
|
|
|
pcm_paused = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logf(" pcm_play_dma_start: no data");
|
|
|
|
pcm_play_data_start(NULL, 0);
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logf(" no change");
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm_play_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_stop(void)
|
|
|
|
{
|
|
|
|
logf("pcm_play_stop");
|
|
|
|
|
|
|
|
pcm_play_lock();
|
|
|
|
|
|
|
|
if (pcm_playing)
|
|
|
|
{
|
|
|
|
logf(" pcm_play_dma_stop");
|
|
|
|
pcm_play_dma_stop();
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_play_stopped();
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logf(" not playing");
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm_play_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**/
|
|
|
|
|
2008-12-12 11:01:07 +00:00
|
|
|
/* set frequency next frequency used by the audio hardware -
|
|
|
|
* what pcm_apply_settings will set */
|
|
|
|
void pcm_set_frequency(unsigned int samplerate)
|
|
|
|
{
|
|
|
|
logf("pcm_set_frequency");
|
|
|
|
|
2010-06-26 10:07:17 +00:00
|
|
|
int index;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SAMPR_TYPES
|
2010-09-17 20:42:45 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
2010-06-26 10:07:17 +00:00
|
|
|
unsigned int type = samplerate & SAMPR_TYPE_MASK;
|
2010-09-17 20:42:45 +00:00
|
|
|
#endif
|
2010-06-26 10:07:17 +00:00
|
|
|
samplerate &= ~SAMPR_TYPE_MASK;
|
|
|
|
|
2010-06-26 10:37:41 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
#if SAMPR_TYPE_REC != 0
|
2010-06-26 10:07:17 +00:00
|
|
|
/* For now, supported targets have direct conversion when configured with
|
|
|
|
* CONFIG_SAMPR_TYPES.
|
|
|
|
* Some hypothetical target with independent rates would need slightly
|
|
|
|
* different handling throughout this source. */
|
|
|
|
if (type == SAMPR_TYPE_REC)
|
|
|
|
samplerate = pcm_sampr_type_rec_to_play(samplerate);
|
|
|
|
#endif
|
2010-06-26 10:37:41 +00:00
|
|
|
#endif /* HAVE_RECORDING */
|
2010-06-26 10:07:17 +00:00
|
|
|
#endif /* CONFIG_SAMPR_TYPES */
|
|
|
|
|
|
|
|
index = round_value_to_list32(samplerate, hw_freq_sampr,
|
|
|
|
HW_NUM_FREQ, false);
|
2008-12-12 11:01:07 +00:00
|
|
|
|
|
|
|
if (samplerate != hw_freq_sampr[index])
|
|
|
|
index = HW_FREQ_DEFAULT; /* Invalid = default */
|
|
|
|
|
|
|
|
pcm_sampr = hw_freq_sampr[index];
|
|
|
|
pcm_fsel = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apply pcm settings to the hardware */
|
|
|
|
void pcm_apply_settings(void)
|
|
|
|
{
|
|
|
|
logf("pcm_apply_settings");
|
|
|
|
|
2011-09-01 12:15:43 +00:00
|
|
|
pcm_wait_for_init();
|
|
|
|
|
2008-12-12 11:01:07 +00:00
|
|
|
if (pcm_sampr != pcm_curr_sampr)
|
|
|
|
{
|
|
|
|
logf(" pcm_dma_apply_settings");
|
|
|
|
pcm_dma_apply_settings();
|
|
|
|
pcm_curr_sampr = pcm_sampr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* register callback to buffer more data */
|
|
|
|
void pcm_play_set_dma_started_callback(void (* callback)(void))
|
|
|
|
{
|
|
|
|
pcm_play_dma_started = callback;
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
/** Low level pcm recording apis **/
|
|
|
|
|
|
|
|
/* Next start for recording peaks */
|
2010-05-12 14:05:36 +00:00
|
|
|
static const void * volatile pcm_rec_peak_addr SHAREDBSS_ATTR = NULL;
|
2007-10-06 22:27:27 +00:00
|
|
|
/* the registered callback function for when more data is available */
|
2010-05-24 16:42:32 +00:00
|
|
|
static volatile pcm_rec_callback_type
|
2008-04-06 04:34:57 +00:00
|
|
|
pcm_callback_more_ready SHAREDBSS_ATTR = NULL;
|
2007-10-06 22:27:27 +00:00
|
|
|
/* DMA transfer in is currently active */
|
2008-04-06 04:34:57 +00:00
|
|
|
volatile bool pcm_recording SHAREDBSS_ATTR = false;
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
/* Called internally by functions to reset the state */
|
|
|
|
static void pcm_recording_stopped(void)
|
|
|
|
{
|
|
|
|
pcm_recording = false;
|
|
|
|
pcm_callback_more_ready = NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/**
|
|
|
|
* Return recording peaks - From the end of the last peak up to
|
|
|
|
* current write position.
|
|
|
|
*/
|
|
|
|
void pcm_calculate_rec_peaks(int *left, int *right)
|
|
|
|
{
|
2011-07-02 11:55:38 +00:00
|
|
|
static uint16_t peaks[2];
|
2007-10-06 22:27:27 +00:00
|
|
|
|
|
|
|
if (pcm_recording)
|
|
|
|
{
|
2010-05-12 14:05:36 +00:00
|
|
|
const void *peak_addr = pcm_rec_peak_addr;
|
|
|
|
const void *addr = pcm_rec_dma_get_peak_buffer();
|
|
|
|
|
|
|
|
if (addr != NULL)
|
2007-10-06 22:27:27 +00:00
|
|
|
{
|
2010-05-12 14:05:36 +00:00
|
|
|
int count = (int)(((intptr_t)addr - (intptr_t)peak_addr) >> 2);
|
2007-10-06 22:27:27 +00:00
|
|
|
|
2010-05-12 14:05:36 +00:00
|
|
|
if (count > 0)
|
|
|
|
{
|
2010-05-13 04:12:23 +00:00
|
|
|
pcm_peak_peeker((int32_t *)peak_addr, count, peaks);
|
2010-05-12 14:05:36 +00:00
|
|
|
|
|
|
|
if (peak_addr == pcm_rec_peak_addr)
|
|
|
|
pcm_rec_peak_addr = addr;
|
|
|
|
}
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
2008-12-14 11:42:47 +00:00
|
|
|
/* else keep previous peak values */
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
peaks[0] = peaks[1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
*left = peaks[0];
|
|
|
|
|
|
|
|
if (right)
|
|
|
|
*right = peaks[1];
|
|
|
|
} /* pcm_calculate_rec_peaks */
|
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
bool pcm_is_recording(void)
|
|
|
|
{
|
|
|
|
return pcm_recording;
|
|
|
|
}
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Functions that do not require targeted implementation but only a targeted
|
|
|
|
* interface
|
|
|
|
*/
|
2010-05-24 16:42:32 +00:00
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
void pcm_init_recording(void)
|
|
|
|
{
|
|
|
|
logf("pcm_init_recording");
|
|
|
|
|
2011-09-01 12:15:43 +00:00
|
|
|
pcm_wait_for_init();
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Stop the beasty before attempting recording */
|
|
|
|
mixer_reset();
|
|
|
|
|
2007-10-07 23:19:09 +00:00
|
|
|
/* Recording init is locked unlike general pcm init since this is not
|
|
|
|
* just a one-time event at startup and it should and must be safe by
|
|
|
|
* now. */
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_rec_lock();
|
|
|
|
|
|
|
|
logf(" pcm_rec_dma_init");
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_recording_stopped();
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_rec_dma_init();
|
|
|
|
|
|
|
|
pcm_rec_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_close_recording(void)
|
|
|
|
{
|
|
|
|
logf("pcm_close_recording");
|
|
|
|
|
|
|
|
pcm_rec_lock();
|
|
|
|
|
|
|
|
if (pcm_recording)
|
|
|
|
{
|
|
|
|
logf(" pcm_rec_dma_stop");
|
|
|
|
pcm_rec_dma_stop();
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_recording_stopped();
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logf(" pcm_rec_dma_close");
|
|
|
|
pcm_rec_dma_close();
|
|
|
|
|
|
|
|
pcm_rec_unlock();
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
void pcm_record_data(pcm_rec_callback_type more_ready,
|
2007-10-06 22:27:27 +00:00
|
|
|
void *start, size_t size)
|
|
|
|
{
|
|
|
|
logf("pcm_record_data");
|
|
|
|
|
2010-05-12 14:05:36 +00:00
|
|
|
/* 32-bit aligned and sized data only */
|
|
|
|
start = (void *)(((uintptr_t)start + 3) & ~3);
|
|
|
|
size &= ~3;
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
if (!(start && size))
|
|
|
|
{
|
|
|
|
logf(" no buffer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm_rec_lock();
|
|
|
|
|
|
|
|
pcm_callback_more_ready = more_ready;
|
|
|
|
|
2010-05-12 14:05:36 +00:00
|
|
|
#ifdef HAVE_PCM_REC_DMA_ADDRESS
|
|
|
|
/* Need a physical DMA address translation, if not already physical. */
|
|
|
|
pcm_rec_peak_addr = pcm_dma_addr(start);
|
|
|
|
#else
|
|
|
|
pcm_rec_peak_addr = start;
|
|
|
|
#endif
|
|
|
|
|
2007-10-06 22:27:27 +00:00
|
|
|
logf(" pcm_rec_dma_start");
|
2008-12-12 11:01:07 +00:00
|
|
|
pcm_apply_settings();
|
2007-10-06 22:27:27 +00:00
|
|
|
pcm_rec_dma_start(start, size);
|
|
|
|
pcm_recording = true;
|
|
|
|
|
|
|
|
pcm_rec_unlock();
|
|
|
|
} /* pcm_record_data */
|
|
|
|
|
|
|
|
void pcm_stop_recording(void)
|
|
|
|
{
|
|
|
|
logf("pcm_stop_recording");
|
|
|
|
|
|
|
|
pcm_rec_lock();
|
|
|
|
|
|
|
|
if (pcm_recording)
|
|
|
|
{
|
|
|
|
logf(" pcm_rec_dma_stop");
|
|
|
|
pcm_rec_dma_stop();
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_recording_stopped();
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pcm_rec_unlock();
|
|
|
|
} /* pcm_stop_recording */
|
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
void pcm_rec_more_ready_callback(int status, void **start, size_t *size)
|
2010-05-12 14:05:36 +00:00
|
|
|
{
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_rec_callback_type have_more = pcm_callback_more_ready;
|
2010-05-12 14:05:36 +00:00
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
*size = 0;
|
2010-05-12 14:05:36 +00:00
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
if (have_more && start)
|
|
|
|
{
|
|
|
|
have_more(status, start, size);
|
|
|
|
*start = (void *)(((uintptr_t)*start + 3) & ~3);
|
|
|
|
*size &= ~3;
|
2010-05-12 14:05:36 +00:00
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
if (*start && *size)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_PCM_REC_DMA_ADDRESS
|
|
|
|
/* Need a physical DMA address translation, if not already
|
|
|
|
* physical. */
|
|
|
|
pcm_rec_peak_addr = pcm_dma_addr(*start);
|
|
|
|
#else
|
|
|
|
pcm_rec_peak_addr = *start;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-12-13 06:01:08 +00:00
|
|
|
|
2010-05-24 16:42:32 +00:00
|
|
|
/* Error, callback missing or no more DMA to do */
|
|
|
|
pcm_rec_dma_stop();
|
|
|
|
pcm_recording_stopped();
|
2007-10-06 22:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_RECORDING */
|