Add Pico Audio Pack example
This commit is contained in:
parent
c3f609ebe0
commit
3539762f34
|
@ -6,4 +6,5 @@ add_subdirectory(pico_scroll)
|
|||
add_subdirectory(pico_explorer)
|
||||
add_subdirectory(pico_rgb_keypad)
|
||||
add_subdirectory(pico_rtc_display)
|
||||
add_subdirectory(pico_tof_display)
|
||||
add_subdirectory(pico_tof_display)
|
||||
add_subdirectory(pico_audio)
|
|
@ -0,0 +1,23 @@
|
|||
if (TARGET pico_audio_i2s)
|
||||
# You must supply the Pico Extras path to build this example
|
||||
# -DPICO_SDK_POST_LIST_DIRS=/path/to/pico-extras
|
||||
# You can use "cmake.configureSettings" in VSCode settings.json
|
||||
# Grab the extra libraries here: https://github.com/raspberrypi/pico-extras
|
||||
add_executable(
|
||||
audio
|
||||
demo.cpp
|
||||
)
|
||||
|
||||
# Pull in pico libraries that we need
|
||||
target_link_libraries(audio pico_stdlib pico_audio_i2s)
|
||||
|
||||
target_compile_definitions(audio PRIVATE
|
||||
# compile time configuration of I2S
|
||||
PICO_AUDIO_I2S_MONO_INPUT=1
|
||||
#define for our example code
|
||||
USE_AUDIO_I2S=1
|
||||
)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(audio)
|
||||
endif()
|
|
@ -0,0 +1,97 @@
|
|||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#if PICO_ON_DEVICE
|
||||
|
||||
#include "hardware/clocks.h"
|
||||
#include "hardware/structs/clocks.h"
|
||||
|
||||
#endif
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/audio_i2s.h"
|
||||
|
||||
#define PICO_AUDIO_PAC_I2S_DATA 9
|
||||
#define PICO_AUDIO_PAC_I2S_BITCLOCK 10
|
||||
#define SINE_WAVE_TABLE_LEN 2048
|
||||
#define SAMPLES_PER_BUFFER 256
|
||||
|
||||
static int16_t sine_wave_table[SINE_WAVE_TABLE_LEN];
|
||||
|
||||
struct audio_buffer_pool *init_audio() {
|
||||
|
||||
static audio_format_t audio_format = {
|
||||
.sample_freq = 24000,
|
||||
.format = AUDIO_BUFFER_FORMAT_PCM_S16,
|
||||
.channel_count = 1,
|
||||
};
|
||||
|
||||
static struct audio_buffer_format producer_format = {
|
||||
.format = &audio_format,
|
||||
.sample_stride = 2
|
||||
};
|
||||
|
||||
struct audio_buffer_pool *producer_pool = audio_new_producer_pool(&producer_format, 3,
|
||||
SAMPLES_PER_BUFFER); // todo correct size
|
||||
bool __unused ok;
|
||||
const struct audio_format *output_format;
|
||||
struct audio_i2s_config config = {
|
||||
.data_pin = PICO_AUDIO_PAC_I2S_DATA,
|
||||
.clock_pin_base = PICO_AUDIO_PAC_I2S_BITCLOCK,
|
||||
.dma_channel = 0,
|
||||
.pio_sm = 0,
|
||||
};
|
||||
|
||||
output_format = audio_i2s_setup(&audio_format, &config);
|
||||
if (!output_format) {
|
||||
panic("PicoAudio: Unable to open audio device.\n");
|
||||
}
|
||||
|
||||
ok = audio_i2s_connect(producer_pool);
|
||||
assert(ok);
|
||||
audio_i2s_set_enabled(true);
|
||||
|
||||
return producer_pool;
|
||||
}
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
|
||||
for (int i = 0; i < SINE_WAVE_TABLE_LEN; i++) {
|
||||
sine_wave_table[i] = 32767 * cosf(i * 2 * (float) (M_PI / SINE_WAVE_TABLE_LEN));
|
||||
}
|
||||
|
||||
struct audio_buffer_pool *ap = init_audio();
|
||||
uint32_t step = 0x200000;
|
||||
uint32_t pos = 0;
|
||||
uint32_t pos_max = 0x10000 * SINE_WAVE_TABLE_LEN;
|
||||
uint vol = 128;
|
||||
while (true) {
|
||||
int c = getchar_timeout_us(0);
|
||||
if (c >= 0) {
|
||||
if (c == '-' && vol) vol -= 4;
|
||||
if ((c == '=' || c == '+') && vol < 255) vol += 4;
|
||||
if (c == '[' && step > 0x10000) step -= 0x10000;
|
||||
if (c == ']' && step < (SINE_WAVE_TABLE_LEN / 16) * 0x20000) step += 0x10000;
|
||||
if (c == 'q') break;
|
||||
printf("vol = %d, step = %d \r", vol, step >> 16);
|
||||
}
|
||||
struct audio_buffer *buffer = take_audio_buffer(ap, true);
|
||||
int16_t *samples = (int16_t *) buffer->buffer->bytes;
|
||||
for (uint i = 0; i < buffer->max_sample_count; i++) {
|
||||
samples[i] = (vol * sine_wave_table[pos >> 16u]) >> 8u;
|
||||
pos += step;
|
||||
if (pos >= pos_max) pos -= pos_max;
|
||||
}
|
||||
buffer->sample_count = buffer->max_sample_count;
|
||||
give_audio_buffer(ap, buffer);
|
||||
}
|
||||
puts("\n");
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue