summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/dsp.c210
1 files changed, 125 insertions, 85 deletions
diff --git a/apps/dsp.c b/apps/dsp.c
index 0f7f8b12bc..e0cea6d41b 100644
--- a/apps/dsp.c
+++ b/apps/dsp.c
@@ -163,8 +163,6 @@ typedef void (*channels_process_fn_type)(int count, int32_t *buf[]);
163/* DSP local channel processing in place */ 163/* DSP local channel processing in place */
164typedef void (*channels_process_dsp_fn_type)(int count, struct dsp_data *data, 164typedef void (*channels_process_dsp_fn_type)(int count, struct dsp_data *data,
165 int32_t *buf[]); 165 int32_t *buf[]);
166/* DSP processes that return a value */
167typedef int (*return_fn_type)(int count, int32_t *buf[]);
168 166
169/* 167/*
170 ***************************************************************************/ 168 ***************************************************************************/
@@ -195,7 +193,7 @@ struct dsp_config
195 channels_process_fn_type apply_crossfeed; 193 channels_process_fn_type apply_crossfeed;
196 channels_process_fn_type eq_process; 194 channels_process_fn_type eq_process;
197 channels_process_fn_type channels_process; 195 channels_process_fn_type channels_process;
198 return_fn_type compressor_process; 196 channels_process_fn_type compressor_process;
199}; 197};
200 198
201/* General DSP config */ 199/* General DSP config */
@@ -262,16 +260,13 @@ static int32_t *resample_buf;
262#define RESAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2 * RESAMPLE_RATIO) 260#define RESAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2 * RESAMPLE_RATIO)
263 261
264/* compressor */ 262/* compressor */
265/* MAX_COUNT is largest possible sample count in compressor_process */
266#define MAX_COUNT (SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO / 2)
267static struct compressor_menu c_menu; 263static struct compressor_menu c_menu;
268static int32_t comp_rel_slope IBSS_ATTR; /* S7.24 format */ 264static int32_t comp_rel_slope IBSS_ATTR; /* S7.24 format */
269static int32_t comp_makeup_gain IBSS_ATTR; /* S7.24 format */ 265static int32_t comp_makeup_gain IBSS_ATTR; /* S7.24 format */
270static int32_t comp_curve[65] IBSS_ATTR; /* S7.24 format */ 266static int32_t comp_curve[66] IBSS_ATTR; /* S7.24 format */
271static int32_t gain_buffer[MAX_COUNT] IBSS_ATTR; 267static int32_t release_gain IBSS_ATTR; /* S7.24 format */
272static int32_t release_gain IBSS_ATTR; 268#define UNITY (1L << 24) /* unity gain in S7.24 format */
273 269static void compressor_process(int count, int32_t *buf[]);
274static int compressor_process(int count, int32_t *buf[]);
275 270
276 271
277/* Clip sample to signed 16 bit range */ 272/* Clip sample to signed 16 bit range */
@@ -1269,7 +1264,7 @@ int dsp_process(struct dsp_config *dsp, char *dst, const char *src[], int count)
1269 dsp->channels_process(chunk, t2); 1264 dsp->channels_process(chunk, t2);
1270 1265
1271 if (dsp->compressor_process) 1266 if (dsp->compressor_process)
1272 chunk = dsp->compressor_process(chunk, t2); 1267 dsp->compressor_process(chunk, t2);
1273 1268
1274 dsp->output_samples(chunk, &dsp->data, (const int32_t **)t2, (int16_t *)dst); 1269 dsp->output_samples(chunk, &dsp->data, (const int32_t **)t2, (int16_t *)dst);
1275 1270
@@ -1444,7 +1439,7 @@ intptr_t dsp_configure(struct dsp_config *dsp, int setting, intptr_t value)
1444 resampler_new_delta(dsp); 1439 resampler_new_delta(dsp);
1445 tdspeed_setup(dsp); 1440 tdspeed_setup(dsp);
1446 if (dsp == &AUDIO_DSP) 1441 if (dsp == &AUDIO_DSP)
1447 release_gain = (1 << 24); 1442 release_gain = UNITY;
1448 break; 1443 break;
1449 1444
1450 case DSP_FLUSH: 1445 case DSP_FLUSH:
@@ -1454,7 +1449,7 @@ intptr_t dsp_configure(struct dsp_config *dsp, int setting, intptr_t value)
1454 dither_init(dsp); 1449 dither_init(dsp);
1455 tdspeed_setup(dsp); 1450 tdspeed_setup(dsp);
1456 if (dsp == &AUDIO_DSP) 1451 if (dsp == &AUDIO_DSP)
1457 release_gain = (1 << 24); 1452 release_gain = UNITY;
1458 break; 1453 break;
1459 1454
1460 case DSP_SET_TRACK_GAIN: 1455 case DSP_SET_TRACK_GAIN:
@@ -1614,13 +1609,15 @@ void dsp_set_compressor(int c_threshold, int c_ratio, int c_gain,
1614 { 1609 {
1615 int32_t db; /* S15.16 format */ 1610 int32_t db; /* S15.16 format */
1616 int32_t offset; /* S15.16 format */ 1611 int32_t offset; /* S15.16 format */
1617 } db_curve[4]; 1612 } db_curve[5];
1618 1613
1619 /** Set up the shape of the compression curve first as decibel values*/ 1614 /** Set up the shape of the compression curve first as decibel values*/
1620 /* db_curve[0] = bottom of knee 1615 /* db_curve[0] = bottom of knee
1621 [1] = threshold 1616 [1] = threshold
1622 [2] = top of knee 1617 [2] = top of knee
1623 [3] = 0 db input */ 1618 [3] = 0 db input
1619 [4] = ~+12db input (2 bits clipping overhead) */
1620
1624 db_curve[1].db = c_menu.threshold << 16; 1621 db_curve[1].db = c_menu.threshold << 16;
1625 if (c_menu.soft_knee) 1622 if (c_menu.soft_knee)
1626 { 1623 {
@@ -1644,37 +1641,61 @@ void dsp_set_compressor(int c_threshold, int c_ratio, int c_gain,
1644 db_curve[2].db = c_menu.threshold << 16; 1641 db_curve[2].db = c_menu.threshold << 16;
1645 db_curve[2].offset = 0; 1642 db_curve[2].offset = 0;
1646 } 1643 }
1647 /* 0db input is also max offset point (most compression) */ 1644
1645 /* Calculate 0db and ~+12db offsets */
1646 db_curve[4].db = 0xC0A8C; /* db of 2 bits clipping */
1648 if (c_menu.ratio) 1647 if (c_menu.ratio)
1648 {
1649 /* offset = threshold * (ratio - 1) / ratio */ 1649 /* offset = threshold * (ratio - 1) / ratio */
1650 db_curve[3].offset = (int32_t)((long long)(c_menu.threshold << 16) 1650 db_curve[3].offset = (int32_t)((long long)(c_menu.threshold << 16)
1651 * (c_menu.ratio - 1) / c_menu.ratio); 1651 * (c_menu.ratio - 1) / c_menu.ratio);
1652 db_curve[4].offset = (int32_t)((long long)-db_curve[4].db
1653 * (c_menu.ratio - 1) / c_menu.ratio) + db_curve[3].offset;
1654 }
1652 else 1655 else
1656 {
1653 /* offset = threshold for hard limit */ 1657 /* offset = threshold for hard limit */
1654 db_curve[3].offset = (c_menu.threshold << 16); 1658 db_curve[3].offset = (c_menu.threshold << 16);
1659 db_curve[4].offset = -db_curve[4].db + db_curve[3].offset;
1660 }
1655 1661
1656 /* Now set up the comp_curve table with compression offsets in the form 1662 /** Now set up the comp_curve table with compression offsets in the form
1657 of gain factors in S7.24 format */ 1663 of gain factors in S7.24 format */
1658 comp_curve[0] = (1 << 24); 1664 /* comp_curve[0] is 0 (-infinity db) input */
1665 comp_curve[0] = UNITY;
1666 /* comp_curve[1 to 63] are intermediate compression values corresponding
1667 to the 6 MSB of the input values of a non-clipped signal */
1659 for (i = 1; i < 64; i++) 1668 for (i = 1; i < 64; i++)
1660 { 1669 {
1670 /* db constants are stored as positive numbers;
1671 make them negative here */
1661 int32_t this_db = -db[i]; 1672 int32_t this_db = -db[i];
1673
1662 /* no compression below the knee */ 1674 /* no compression below the knee */
1663 if (this_db <= db_curve[0].db) 1675 if (this_db <= db_curve[0].db)
1664 comp_curve[i] = (1 << 24); 1676 comp_curve[i] = UNITY;
1665 1677
1666 /* if soft knee and below top of knee, interpolate along soft knee slope */ 1678 /* if soft knee and below top of knee,
1679 interpolate along soft knee slope */
1667 else if (c_menu.soft_knee && (this_db <= db_curve[2].db)) 1680 else if (c_menu.soft_knee && (this_db <= db_curve[2].db))
1668 comp_curve[i] = fp_factor(fp_mul(((this_db - db_curve[0].db) / 6), 1681 comp_curve[i] = fp_factor(fp_mul(
1682 ((this_db - db_curve[0].db) / 6),
1669 db_curve[2].offset, 16), 16) << 8; 1683 db_curve[2].offset, 16), 16) << 8;
1670 1684
1671 /* interpolate along ratio slope above the knee */ 1685 /* interpolate along ratio slope above the knee */
1672 else 1686 else
1673 comp_curve[i] = fp_factor(fp_mul(fp_div((this_db - db_curve[1].db), 1687 comp_curve[i] = fp_factor(fp_mul(
1674 -db_curve[1].db, 16), db_curve[3].offset, 16), 16) << 8; 1688 fp_div((db_curve[1].db - this_db), db_curve[1].db, 16),
1689 db_curve[3].offset, 16), 16) << 8;
1675 } 1690 }
1691 /* comp_curve[64] is the compression level of a maximum level,
1692 non-clipped signal */
1676 comp_curve[64] = fp_factor(db_curve[3].offset, 16) << 8; 1693 comp_curve[64] = fp_factor(db_curve[3].offset, 16) << 8;
1677 1694
1695 /* comp_curve[65] is the compression level of a maximum level,
1696 clipped signal */
1697 comp_curve[65] = fp_factor(db_curve[4].offset, 16) << 8;
1698
1678#if defined(SIMULATOR) && defined(LOGF_ENABLE) 1699#if defined(SIMULATOR) && defined(LOGF_ENABLE)
1679 logf("\n *** Compression Offsets ***"); 1700 logf("\n *** Compression Offsets ***");
1680 /* some settings for display only, not used in calculations */ 1701 /* some settings for display only, not used in calculations */
@@ -1682,31 +1703,33 @@ void dsp_set_compressor(int c_threshold, int c_ratio, int c_gain,
1682 db_curve[1].offset = 0; 1703 db_curve[1].offset = 0;
1683 db_curve[3].db = 0; 1704 db_curve[3].db = 0;
1684 1705
1685 for (i = 0; i <= 3; i++) 1706 for (i = 0; i <= 4; i++)
1686 { 1707 {
1687 logf("Curve[%d]: db: % .1f\toffset: % .4f", i, (float)db_curve[i].db / (1 << 16), 1708 logf("Curve[%d]: db: % 6.2f\toffset: % 6.2f", i,
1709 (float)db_curve[i].db / (1 << 16),
1688 (float)db_curve[i].offset / (1 << 16)); 1710 (float)db_curve[i].offset / (1 << 16));
1689 } 1711 }
1690 1712
1691 logf("\nGain factors:"); 1713 logf("\nGain factors:");
1692 for (i = 1; i <= 64; i++) 1714 for (i = 1; i <= 65; i++)
1693 { 1715 {
1694 debugf("%02d: %.6f ", i, (float)comp_curve[i] / (1 << 24)); 1716 debugf("%02d: %.6f ", i, (float)comp_curve[i] / UNITY);
1695 if (i % 4 == 0) debugf("\n"); 1717 if (i % 4 == 0) debugf("\n");
1696 } 1718 }
1719 debugf("\n");
1697#endif 1720#endif
1698 1721
1699 /* if using auto peak, then makeup gain is max offset - .1dB headroom */ 1722 /* if using auto peak, then makeup gain is max offset - .1dB headroom */
1700 int32_t db_makeup = (c_menu.gain == -1) ? 1723 int32_t db_makeup = (c_menu.gain == -1) ?
1701 -(db_curve[3].offset) - 0x199A : c_menu.gain << 16; 1724 -(db_curve[3].offset) - 0x199A : c_menu.gain << 16;
1702 comp_makeup_gain = fp_factor(db_makeup, 16) << 8; 1725 comp_makeup_gain = fp_factor(db_makeup, 16) << 8;
1703 logf("Makeup gain:\t%.6f", (float)comp_makeup_gain / (1 << 24)); 1726 logf("Makeup gain:\t%.6f", (float)comp_makeup_gain / UNITY);
1704 1727
1705 /* calculate per-sample gain change a rate of 10db over release time */ 1728 /* calculate per-sample gain change a rate of 10db over release time */
1706 comp_rel_slope = 0xAF0BB2 / c_menu.release; 1729 comp_rel_slope = 0xAF0BB2 / c_menu.release;
1707 logf("Release slope:\t%.6f", (float)comp_rel_slope / (1 << 24)); 1730 logf("Release slope:\t%.6f", (float)comp_rel_slope / UNITY);
1708 1731
1709 release_gain = (1 << 24); 1732 release_gain = UNITY;
1710 } 1733 }
1711 1734
1712 /* enable/disable the compressor */ 1735 /* enable/disable the compressor */
@@ -1719,83 +1742,100 @@ void dsp_set_compressor(int c_threshold, int c_ratio, int c_gain,
1719 */ 1742 */
1720static inline int32_t get_compression_gain(int32_t sample) 1743static inline int32_t get_compression_gain(int32_t sample)
1721{ 1744{
1722 const int frac_bits = AUDIO_DSP.frac_bits; 1745 const int frac_bits_offset = AUDIO_DSP.frac_bits - 15;
1723 1746
1724 /* sample must be positive */ 1747 /* sample must be positive */
1725 if (sample < 0) 1748 if (sample < 0)
1726 sample = -sample - 1; 1749 sample = -(sample + 1);
1727 1750
1728 /* shift sample into 22 frac bit range */ 1751 /* shift sample into 15 frac bit range */
1729 if (frac_bits > 22) 1752 if (frac_bits_offset > 0)
1730 sample >>= (frac_bits - 22); 1753 sample >>= frac_bits_offset;
1731 if (frac_bits < 22) 1754 if (frac_bits_offset < 0)
1732 sample <<= (22 - frac_bits); 1755 sample <<= -frac_bits_offset;
1733 1756
1734 /* index is 6 MSB, rem is 16 LSB */ 1757 /* normal case: sample isn't clipped */
1735 int index = sample >> 16; 1758 if (sample < (1 << 15))
1736 int rem = (sample & 0xFFFF) << 8; 1759 {
1760 /* index is 6 MSB, rem is 9 LSB */
1761 int index = sample >> 9;
1762 int32_t rem = (sample & 0x1FF) << 22;
1763
1764 /* interpolate from the compression curve:
1765 higher gain - ((rem / (1 << 31)) * (higher gain - lower gain)) */
1766 return comp_curve[index] - (FRACMUL(rem,
1767 (comp_curve[index] - comp_curve[index + 1])));
1768 }
1769 /* sample is somewhat clipped, up to 2 bits of overhead */
1770 if (sample < (1 << 17))
1771 {
1772 /* straight interpolation:
1773 higher gain - ((clipped portion of sample * 4/3
1774 / (1 << 31)) * (higher gain - lower gain)) */
1775 return comp_curve[64] - (FRACMUL(((sample - (1 << 15)) / 3) << 16,
1776 (comp_curve[64] - comp_curve[65])));
1777 }
1737 1778
1738 /* interpolate from the compression curve */ 1779 /* sample is too clipped, return invalid value */
1739 return comp_curve[index] + (int32_t)FRACMUL_SHL((comp_curve[index + 1] 1780 return -1;
1740 - comp_curve[index]), rem, 7);
1741} 1781}
1742 1782
1743/** COMPRESSOR PROCESS 1783/** COMPRESSOR PROCESS
1744 * Changes the gain of the samples according to the compressor curve 1784 * Changes the gain of the samples according to the compressor curve
1745 */ 1785 */
1746static int compressor_process(int count, int32_t *buf[]) 1786static void compressor_process(int count, int32_t *buf[])
1747{ 1787{
1748 const int num_chan = AUDIO_DSP.data.num_channels; 1788 const int num_chan = AUDIO_DSP.data.num_channels;
1749 const int32_t fp_one = (1 << 24); 1789 int32_t *in_buf[2] = {buf[0], buf[1]};
1750
1751 int32_t sample_gain, /* S7.24 format */
1752 this_gain; /* S7.24 format */
1753 int i, ch;
1754 1790
1755 /* Step forward through the output buffer, and modify the offset values 1791 while (count-- > 0)
1756 * to establish a smooth, slow release slope.*/
1757 for (i = 0; i < count; i++)
1758 { 1792 {
1759 sample_gain = fp_one; 1793 int ch;
1794 /* use lowest (most compressed) gain factor of the output buffer
1795 sample pair for both samples (mono is also handled correctly here) */
1796 int32_t sample_gain = UNITY;
1760 for (ch = 0; ch < num_chan; ch++) 1797 for (ch = 0; ch < num_chan; ch++)
1761 { 1798 {
1762 this_gain = get_compression_gain(buf[ch][i]); 1799 int32_t this_gain = get_compression_gain(*in_buf[ch]);
1763 if (this_gain < sample_gain) 1800 if (this_gain < sample_gain)
1764 sample_gain = this_gain; 1801 sample_gain = this_gain;
1765 } 1802 }
1766 /* if no release slope, only apply makeup gain */ 1803
1767 if ((sample_gain == fp_one) && (release_gain == fp_one)) 1804 /* perform release slope; skip if no compression and no release slope */
1768 gain_buffer[i] = comp_makeup_gain; 1805 if ((sample_gain != UNITY) || (release_gain != UNITY))
1769 else
1770 { 1806 {
1771 /* if larger offset, start release slope */ 1807 /* if larger offset than previous slope, start new release slope */
1772 if (sample_gain <= release_gain) 1808 if ((sample_gain <= release_gain) && (sample_gain > 0))
1773 release_gain = sample_gain;
1774 else /* keep sloping */
1775 { 1809 {
1776 if (release_gain < (fp_one - comp_rel_slope)) 1810 release_gain = sample_gain;
1777 release_gain += comp_rel_slope;
1778 else
1779 release_gain = fp_one;
1780 } 1811 }
1781 /* store offset with release and also apply makeup gain */
1782 if ((release_gain == fp_one) && (comp_makeup_gain == fp_one))
1783 gain_buffer[i] = fp_one;
1784 else 1812 else
1785 gain_buffer[i] = FRACMUL_SHL(release_gain, comp_makeup_gain, 7); 1813 /* keep sloping towards unity gain (and ignore invalid value) */
1814 {
1815 release_gain += comp_rel_slope;
1816 if (release_gain > UNITY)
1817 {
1818 release_gain = UNITY;
1819 }
1820 }
1786 } 1821 }
1787 } 1822
1788 1823 /* total gain factor is the product of release gain and makeup gain,
1789 /* Implement the compressor: apply those gain factors to the output 1824 but avoid computation if possible */
1790 * buffer samples */ 1825 int32_t total_gain = ((release_gain == UNITY) ? comp_makeup_gain :
1791 1826 (comp_makeup_gain == UNITY) ? release_gain :
1792 for (i = 0; i < count; i++) 1827 FRACMUL_SHL(release_gain, comp_makeup_gain, 7));
1793 { 1828
1794 if (gain_buffer[i] != fp_one) 1829 /* Implement the compressor: apply total gain factor (if any) to the
1830 output buffer sample pair/mono sample */
1831 if (total_gain != UNITY)
1795 { 1832 {
1796 for (ch = 0; ch < num_chan; ch++) 1833 for (ch = 0; ch < num_chan; ch++)
1797 buf[ch][i] = FRACMUL_SHL(buf[ch][i], gain_buffer[i], 7); 1834 {
1835 *in_buf[ch] = FRACMUL_SHL(total_gain, *in_buf[ch], 7);
1836 }
1798 } 1837 }
1838 in_buf[0]++;
1839 in_buf[1]++;
1799 } 1840 }
1800 return count;
1801} 1841}