2 Commits

Author SHA1 Message Date
6a89c19d24 saving stereo 2023-04-12 15:48:56 +01:00
13ee3ed817 streaming stereo status, fixes: #74 2022-12-05 09:16:05 +00:00
18 changed files with 165 additions and 238 deletions

6
.gitignore vendored
View File

@ -1,7 +1,3 @@
*.rej
*.orig
.DS_Store
*.ini
build
*.*~
*.o

View File

@ -1,24 +0,0 @@
build-ubuntu:
stage: build
image: ubuntu:22.04
script:
- apt-get update
- apt-get install -y git cmake g++ freeglut3-dev qtbase5-dev qt5-qmake qtbase5-dev-tools
- mkdir -p build
- cd build
- cmake ..
- cmake --build . --target samplebrain
build-win64:
tags:
- "windows"
before_script:
- choco install -y cmake python qt5-default
- $env:Path += ';C:\Program Files\CMake\bin'
stage: build
script:
- set CMAKE_PREFIX_PATH=C:\Qt\5.15.2\mingw81_64
- mkdir build
- cd build
- cmake ..
- cmake --build . --target samplebrain

View File

@ -1,81 +0,0 @@
cmake_minimum_required(VERSION 3.16)
project(samplebrain VERSION 1.0 LANGUAGES C CXX)
include(Dependencies.cmake)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Set up AUTOMOC and some sensible defaults for runtime execution
# When using Qt 6.3, you can replace the code block below with
# qt_standard_project_setup()
set(CMAKE_AUTOMOC ON)
include(GNUInstallDirs)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Gui Widgets)
find_package(OpenMP)
add_executable(samplebrain WIN32 MACOSX_BUNDLE
app/MainWindow.cpp app/MainWindow.h
app/SettingsDialog.cpp app/SettingsDialog.h
app/audio_thread.cpp
app/feedback.cpp
app/process_thread.cpp
app/qtmain.cpp
app/samplebrain.qrc
app/sound_items.cpp
brain/src/aquila/filter/MelFilter.cpp
brain/src/aquila/filter/MelFilterBank.cpp
brain/src/aquila/transform/Dct.cpp
brain/src/block.cpp
brain/src/block_stream.cpp
brain/src/brain.cpp
brain/src/fft.cpp
brain/src/mfcc.cpp
brain/src/renderer.cpp
brain/src/search_params.cpp
brain/src/spiralcore/OSC_server.cpp
brain/src/spiralcore/allocator.cpp
brain/src/spiralcore/audio.cpp
brain/src/spiralcore/command_ring_buffer.cpp
brain/src/spiralcore/portaudio_client.cpp
brain/src/spiralcore/ring_buffer.cpp
brain/src/spiralcore/sample.cpp
brain/src/spiralcore/stream.cpp
brain/src/status.cpp
brain/src/window.cpp
app/gui/samplebrain.ui
app/gui/settings.ui
)
target_include_directories(samplebrain PRIVATE
.
brain/src
)
target_link_libraries(samplebrain PRIVATE
Qt5::Core
Qt5::Gui
Qt5::Widgets
fftw3
lo_shared
portaudio
sndfile
)
if(OpenMP_CXX_FOUND)
target_link_libraries(samplebrain PUBLIC OpenMP::OpenMP_CXX)
endif()
install(TARGETS samplebrain
BUNDLE DESTINATION .
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
# Consider using qt_generate_deploy_app_script() for app deployment if
# the project can use Qt 6.3. In that case rerun qmake2cmake with
# --min-qt-version=6.3.

View File

@ -1,49 +0,0 @@
################################################################################
# FetchContent
################################################################################
include(FetchContent)
################################################################################
# fftw3
################################################################################
FetchContent_Declare(
fftw3
URL http://fftw.org/fftw-3.3.10.tar.gz
URL_HASH MD5=8ccbf6a5ea78a16dbc3e1306e234cc5c)
FetchContent_MakeAvailable(fftw3)
include_directories(${fftw3_SOURCE_DIR}/api)
################################################################################
# liblo
################################################################################
FetchContent_Declare(
liblo
URL http://downloads.sourceforge.net/liblo/liblo-0.31.tar.gz
URL_HASH MD5=14378c1e74c58e777fbb4fcf33ac5315)
FetchContent_MakeAvailable(liblo)
add_subdirectory(${liblo_SOURCE_DIR}/cmake)
################################################################################
# PortAudio
################################################################################
FetchContent_Declare(
portaudio
URL http://files.portaudio.com/archives/pa_stable_v190700_20210406.tgz
URL_HASH MD5=ad319249932c6794b551d954b8844402)
FetchContent_MakeAvailable(portaudio)
################################################################################
# libsndfile
################################################################################
FetchContent_Declare(
libsndfile
GIT_REPOSITORY https://github.com/libsndfile/libsndfile
GIT_TAG 1.1.0)
FetchContent_MakeAvailable(libsndfile)

View File

@ -113,8 +113,8 @@ void MainWindow::init_from_session(const string &filename) {
ifstream ifs(filename.c_str(),ios::binary);
if (!ifs) return;
brain s,t;
u32 version=0;
brain s,t,lt;
u32 version=1;
ifs||version;
renderer r(s,t);
ifs||r;
@ -133,7 +133,14 @@ void MainWindow::init_from_session(const string &filename) {
ifs||source_window||target_window;
// todo: probably don't need to load all the sample data too :/
ifs||s;
ifs||t;
ifs||t; // left
ifs||lt; // right
if (version>0) {
ifs||m_stereo;
m_Ui.checkBoxStereo->setChecked(m_stereo);
}
// brain tweaks
search_params * p = r.get_params();
@ -198,8 +205,6 @@ void MainWindow::init_from_session(const string &filename) {
m_Ui.sliderAutotune->setValue(r.get_autotune()*100);
m_Ui.doubleSpinBoxAutotune->setValue(r.get_autotune());
}
#endif

View File

@ -19,7 +19,7 @@
#include <QFileDialog>
#include <QLineEdit>
#include <QMessageBox>
#include "gui/ui_samplebrain.h"
#include "ui_samplebrain.h"
#include "SettingsDialog.h"
#include <iostream>
@ -393,7 +393,11 @@ private slots:
}
void stereo_mode(bool s) {
m_stereo=s;
send_audio_osc("/stereo","i",s);
// irritating but need to tell process thread about stereo state
// just to it can save it to the session file
send_process_osc("/stereo","i",s);
}
void net_enable(int id) {
@ -469,4 +473,5 @@ private:
string m_audio_port;
string m_process_port;
QString m_format_string;
bool m_stereo;
};

View File

@ -21,7 +21,7 @@
#include <QDialog>
#include <QLineEdit>
#include <QSettings>
#include "gui/ui_settings.h"
#include "ui_settings.h"
#include <iostream>
#include <lo/lo.h>

View File

@ -140,6 +140,10 @@ void process_thread::process() {
if (name=="/save_session") {
save_session(cmd.get_string(0));
}
if (name=="/stereo") {
// only for session file save
m_stereo=cmd.get_int(0);
}
}
#ifdef WIN32
Sleep(1);
@ -174,16 +178,7 @@ void process_thread::load_session(const std::string &filename) {
m_left_target.clear();
m_right_target.clear();
ifstream ifs(filename.c_str(),ios::binary);
u32 version=0;
ifs||version;
ifs||(*m_left_renderer);
ifs||(*m_right_renderer);
ifs||m_source_block_size||m_source_overlap;
ifs||m_target_block_size||m_target_overlap;
ifs||m_window_type||m_target_window_type;
ifs||m_source;
ifs||m_left_target;
ifs||m_right_target;
stream_session(ifs);
ifs.close();
pthread_mutex_unlock(m_brain_mutex);
}
@ -191,16 +186,23 @@ void process_thread::load_session(const std::string &filename) {
void process_thread::save_session(const std::string &filename) {
pthread_mutex_lock(m_brain_mutex);
ofstream ofs(filename.c_str(),ios::binary);
u32 version=0;
ofs||version;
ofs||(*m_left_renderer);
ofs||(*m_right_renderer);
ofs||m_source_block_size||m_source_overlap;
ofs||m_target_block_size||m_target_overlap;
ofs||m_window_type||m_target_window_type;
ofs||m_source;
ofs||m_left_target;
ofs||m_right_target;
stream_session(ofs);
ofs.close();
pthread_mutex_unlock(m_brain_mutex);
}
void process_thread::stream_session(std::ios &fs) {
u32 version=1;
fs||version;
fs||(*m_left_renderer);
fs||(*m_right_renderer);
fs||m_source_block_size||m_source_overlap;
fs||m_target_block_size||m_target_overlap;
fs||m_window_type||m_target_window_type;
fs||m_source;
fs||m_left_target;
fs||m_right_target;
if (version>0) {
fs||m_stereo;
}
}

View File

@ -54,6 +54,8 @@ namespace spiralcore {
brain m_source, m_left_target, m_right_target;
private:
void stream_session(std::ios &fs);
OSC_server m_osc;
u32 m_source_block_size;
float m_source_overlap;
@ -61,6 +63,7 @@ namespace spiralcore {
float m_target_overlap;
window::type m_window_type;
window::type m_target_window_type;
bool m_stereo;
pthread_t *m_thread;
// only use in mutex obvs...

View File

@ -33,7 +33,7 @@ int main( int argc , char *argv[] ){
QApplication app(argc, argv);
cerr<<"Qt version: "<<qVersion()<<endl;
QSettings settings("thentrythis", "samplebrain");
// slight over-use of OSC servers here, but the are packaged nicely for

View File

@ -16,7 +16,6 @@
#include <iostream>
#include <algorithm>
#include <atomic>
#include <sndfile.h>
#include <float.h>
#include <spiralcore/audio.h>
@ -246,12 +245,10 @@ void brain::build_synapses_thresh(search_params &params, double thresh) {
m_average_error = calc_average_diff(params)*thresh;
double err = m_average_error*thresh;
u32 brain_size = m_blocks.size();
std::atomic<u32> progress{0};
#pragma omp parallel for
for (u32 outer_index = 0; outer_index < brain_size; ++outer_index) {
auto &i = m_blocks[outer_index];
u32 outer_index = 0;
for (auto &i : m_blocks) {
u32 index = 0;
status::update("building synapses %d%%",(int)(progress/(float)brain_size*100));
status::update("building synapses %d%%",(int)(outer_index/(float)brain_size*100));
for (auto &j : m_blocks) {
if (index!=outer_index) {
// collect connections that are under threshold in closeness
@ -262,7 +259,7 @@ void brain::build_synapses_thresh(search_params &params, double thresh) {
}
++index;
}
++progress;
++outer_index;
}
}
*/
@ -270,25 +267,23 @@ void brain::build_synapses_thresh(search_params &params, double thresh) {
void brain::build_synapses_fixed(search_params &params) {
//m_average_error = calc_average_diff(params)*thresh;
u32 brain_size = m_blocks.size();
u32 outer_index = 0;
u32 num_synapses = NUM_FIXED_SYNAPSES;
if (num_synapses>=m_blocks.size()) num_synapses=m_blocks.size()-1;
// need to stop the progress updates flooding osc
u32 update_period = 100;
std::atomic<u32> update_tick{0};
std::atomic<u32> progress{0};
#pragma omp parallel for
for (u32 outer_index = 0; outer_index < brain_size; ++outer_index) {
auto &i = m_blocks[outer_index];
u32 update_tick = 0;
for (auto &i:m_blocks) {
if (update_tick>update_period) {
status::update("building synapses %d%%",(int)(progress/(float)brain_size*100));
status::update("building synapses %d%%",(int)(outer_index/(float)brain_size*100));
update_tick=0;
}
update_tick++;
u32 index = 0;
vector<pair<u32,double>> collect;
collect.reserve(brain_size);
// collect comparisons to all other blocks
for (auto &j:m_blocks) {
@ -313,7 +308,7 @@ void brain::build_synapses_fixed(search_params &params) {
i.get_synapse().push_back(collect[n].first);
}
++progress;
++outer_index;
}
status::update("Done: %d synapses grown for %d blocks",num_synapses*brain_size,brain_size);
}

View File

@ -51,12 +51,12 @@ void audio_device::connect(const string &output_device_name, const string &clien
m_client.attach(output_device_name,clientname,opt);
}
void audio_device::save_sample(const string &filename, const sample s) {
void audio_device::save_sample(const string &filename, unsigned int channels, const sample s) {
SF_INFO sfinfo;
sfinfo.format=SF_FORMAT_WAV | SF_FORMAT_FLOAT;
sfinfo.frames=s.get_length();
sfinfo.samplerate=m_samplerate;
sfinfo.channels=1;
sfinfo.channels=channels;
sfinfo.sections=1;
sfinfo.seekable=0;
SNDFILE* f=sf_open(filename.c_str(), SFM_WRITE, &sfinfo);
@ -69,8 +69,7 @@ void audio_device::save_sample(const string &filename, const sample s) {
void audio_device::start_recording(std::string filename) {
m_record_filename=filename;
m_recording=true;
m_record_buffer_left.clear();
m_record_buffer_right.clear();
m_record_buffer.clear();
m_record_counter=0;
}
@ -81,12 +80,18 @@ void audio_device::stop_recording() {
void audio_device::maybe_record() {
if (m_recording) {
m_record_buffer_left.add(left_out);
m_record_buffer_right.add(right_out);
sample news(left_out.get_length()*2);
for (unsigned int i=0; i<left_out.get_length(); i++) {
news[i*2]=left_out[i];
news[i*2+1]=right_out[i];
}
m_record_buffer.add(news);
m_record_counter++;
// save "every now and again"
if (m_record_counter%10==0) {
save_sample(m_record_filename+"-left.wav", m_record_buffer_left);
save_sample(m_record_filename+"-right.wav", m_record_buffer_right);
save_sample(m_record_filename+".wav",2,m_record_buffer);
}
}
}

View File

@ -42,13 +42,12 @@ namespace spiralcore {
portaudio_client m_client;
void save_sample(const std::string &filename, const sample s);
void save_sample(const std::string &filename, unsigned int channels, const sample s);
private:
bool m_recording;
std::string m_record_filename;
sample m_record_buffer_left;
sample m_record_buffer_right;
sample m_record_buffer;
u32 m_record_counter;
u32 m_samplerate;
};

View File

@ -1,8 +1,8 @@
# Building from source
## Linux (Ubuntu)
Install cmake:
Install libraries for the sample engine (use brew on mac, MinGW on win):
$ sudo apt install cmake
$ sudo apt install libsndfile1-dev portaudio19-dev liblo-dev libfftw3-dev
Install dependencies for the interface:
@ -16,14 +16,15 @@ Build & run it:
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .
$ ./samplebrain
$ qmake ..
$ make
$ sudo make install
$ samplebrain
## Mac
Install cmake:
Install libraries for sample engine:
$ brew install cmake
$ brew install fftw portaudio liblo libsndfile
Install dependencies for the interface:
@ -34,10 +35,10 @@ Build & run it:
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .
$ qmake ..
$ make
`samplebrain.app` should then be in the build folder for you to run.
`samplebrain.app` should then be in the app folder for you to run.
# Mac build additions

View File

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>1188</width>
<height>939</height>
<height>898</height>
</rect>
</property>
<property name="windowTitle">
@ -1473,7 +1473,7 @@
<string/>
</property>
<property name="icon">
<iconset resource="../samplebrain.qrc">
<iconset resource="../app/samplebrain.qrc">
<normaloff>:/images/images/play.png</normaloff>:/images/images/play.png</iconset>
</property>
<property name="iconSize">
@ -1500,7 +1500,7 @@
<string/>
</property>
<property name="icon">
<iconset resource="../samplebrain.qrc">
<iconset resource="../app/samplebrain.qrc">
<normaloff>:/images/images/pause.png</normaloff>:/images/images/pause.png</iconset>
</property>
<property name="iconSize">
@ -1520,7 +1520,7 @@
<string/>
</property>
<property name="icon">
<iconset resource="../samplebrain.qrc">
<iconset resource="../app/samplebrain.qrc">
<normaloff>:/images/images/record.png</normaloff>:/images/images/record.png</iconset>
</property>
<property name="iconSize">
@ -1540,7 +1540,7 @@
<string/>
</property>
<property name="icon">
<iconset resource="../samplebrain.qrc">
<iconset resource="../app/samplebrain.qrc">
<normaloff>:/images/images/stop.png</normaloff>:/images/images/stop.png</iconset>
</property>
<property name="iconSize">
@ -1606,7 +1606,7 @@
<string/>
</property>
<property name="icon">
<iconset resource="../samplebrain.qrc">
<iconset resource="../app/samplebrain.qrc">
<normaloff>:/images/images/settings.png</normaloff>:/images/images/settings.png</iconset>
</property>
<property name="iconSize">
@ -1639,7 +1639,7 @@
<string/>
</property>
<property name="pixmap">
<pixmap resource="../samplebrain.qrc">:/images/images/at.png</pixmap>
<pixmap resource="../app/samplebrain.qrc">:/images/images/at.png</pixmap>
</property>
</widget>
</item>
@ -1650,8 +1650,7 @@
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources>
<include location="../samplebrain.qrc"/>
<include location="../samplebrain.qrc"/>
<include location="../app/samplebrain.qrc"/>
</resources>
<connections>
<connection>

71
samplebrain.pro Normal file
View File

@ -0,0 +1,71 @@
######################################################################
# Automatically generated by qmake (2.01a) Sun Jul 5 17:49:45 2015
######################################################################
TEMPLATE = app
TARGET = samplebrain
DEPENDPATH += . 2
INCLUDEPATH += . 2
QT += core gui widgets
# Input
HEADERS += app/MainWindow.h \
app/SettingsDialog.h
FORMS += gui/samplebrain.ui \
gui/settings.ui
SOURCES += app/MainWindow.cpp \
app/SettingsDialog.cpp \
app/sound_items.cpp \
app/audio_thread.cpp \
app/process_thread.cpp \
app/feedback.cpp \
app/qtmain.cpp \
brain/src/block.cpp \
brain/src/brain.cpp \
brain/src/fft.cpp \
brain/src/mfcc.cpp \
brain/src/renderer.cpp \
brain/src/search_params.cpp \
brain/src/status.cpp \
brain/src/window.cpp \
brain/src/block_stream.cpp \
brain/src/aquila/filter/MelFilterBank.cpp \
brain/src/aquila/filter/MelFilter.cpp \
brain/src/aquila/transform/Dct.cpp \
brain/src/spiralcore/sample.cpp \
brain/src/spiralcore/ring_buffer.cpp \
brain/src/spiralcore/command_ring_buffer.cpp \
brain/src/spiralcore/portaudio_client.cpp \
brain/src/spiralcore/audio.cpp \
brain/src/spiralcore/OSC_server.cpp \
brain/src/spiralcore/allocator.cpp \
brain/src/spiralcore/stream.cpp
INCLUDEPATH += brain/src
INCLUDEPATH += /usr/local/include
INCLUDEPATH += /opt/homebrew/include
LIBS += -L.. -L/usr/local/lib -L/opt/homebrew/lib -lportaudio -lfftw3 -lsndfile -llo -ldl -lpthread -lm
QMAKE_CXXFLAGS += -O3 -Wall -Wno-unused -std=c++11
# assets
RESOURCES = app/samplebrain.qrc
ICON = desktop/samplebrain.icns
PREFIX = $$(PREFIX)
isEmpty(PREFIX) {
PREFIX = /usr
}
unix:desktopfile.path = $$PREFIX/share/applications/
unix:desktopfile.files = desktop/samplebrain.desktop
unix:iconfile.path = $$PREFIX/share/icons/hicolor/scalable/apps
unix:iconfile.files = desktop/samplebrain.svg
unix:metainfofile.path = $$PREFIX/share/metainfo
unix:metainfofile.files = desktop/org.thentrythis.Samplebrain.metainfo.xml
target.path = $$PREFIX/bin
INSTALLS += target desktopfile iconfile metainfofile