Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal cppsound #16

Merged
merged 6 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- internal_cppsound
pull_request:
branches:
- master
Expand Down
19 changes: 11 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.10)
project(csound-ac)

message("CMAKE BUILD SYSTEM FOR CSOUND-AC")
Expand All @@ -17,15 +17,23 @@ set(CMAKE_CXX_STANDARD 17)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
add_compile_options(/WX-)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wno-sign-compare -Wno-unknown-pragmas -Wno-misleading-indentation -Wno-writable-strings")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -Wno-deprecated-declarations -Wno-sign-compare -Wno-unknown-pragmas -Wno-misleading-indentation -Wno-writable-strings")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
add_compile_options(-Wno-c++11-narrowing)
endif()


# CMAKE_SYSTEM_NAME relevant here can be Darwin, Windows, Linux, or Android.
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME "csound-ac")
if(DARWIN)
set(CMAKE_MACOSX_RPATH ON)
set(CMAKE_INSTALL_RPATH "/usr/local/lib;~/.local/lib;")
else()
endif()
add_definitions(-Dlinux)

set(APIVERSION "6.0")
set(APIVERSION "7.0")
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/local-linux")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/dependencies/csound/cmake/Modules")
Expand Down Expand Up @@ -73,11 +81,6 @@ message("CSOUND_INCLUDE_DIRS: ${CSOUND_INCLUDE_DIRS}")
message("CSOUND_INCLUDE_PATH: ${CSOUND_INCLUDE_PATH}")
include_directories("/Library/Frameworks/CsoundLib64.framework/Versions/6.0/Headers/")
find_library(LIBSNDFILE_LIBRARY sndfile REQUIRED)
# For WebAssembly, csnd6 is not required, on other platforms, it is.
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten")
else()
find_library(CSND6_LIBRARY csnd6)
endif()
set(EIGEN3_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/dependencies/eigen")
message("[DEBUG] EIGEN3_INCLUDE_DIRS: ${EIGEN3_INCLUDE_DIRS}")
find_package(OpenCV)
Expand Down
18 changes: 8 additions & 10 deletions CsoundAC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include(CheckIncludeFiles)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 17)

add_definitions(-fPIC)

set(CMAKE_INCLUDE_CURRENT_DIR "On")
Expand Down Expand Up @@ -36,6 +37,8 @@ if(BUILD_CSOUND_AC)
Counterpoint.cpp
CounterpointMain.cpp
CounterpointNode.cpp
CppSound.cpp
CsoundFile.cpp
Event.cpp
Lindenmayer.cpp
MCRM.cpp
Expand All @@ -49,7 +52,6 @@ if(BUILD_CSOUND_AC)
ScoreNode.cpp
Sequence.cpp
Shell.cpp
Soundfile.cpp
StrangeAttractor.cpp
System.cpp
Voicelead.cpp
Expand All @@ -64,9 +66,9 @@ if(BUILD_CSOUND_AC)
../dependencies/portsmf/mfmidi.cpp
../dependencies/portsmf/strparse.cpp
trace.cpp)
if(OPENCV_FOUND)
list(APPEND libcsoundac_SRCS "ImageToScore.cpp")
endif()
if(OPENCV_FOUND)
list(APPEND libcsoundac_SRCS "ImageToScore.cpp")
endif()
message(STATUS "libcsoundac_SRCS: " ${libcsoundac_SRCS})

if (MSVC)
Expand All @@ -75,10 +77,6 @@ endif()
add_library(libCsoundAC SHARED ${libcsoundac_SRCS})
endif()

if(APPLE)
target_include_directories(libCsoundAC SYSTEM PRIVATE ${LIBSNDFILE_INCLUDE_DIR})
endif()

target_include_directories(libCsoundAC SYSTEM PRIVATE ${Python3_INCLUDE_DIRS})
target_include_directories(libCsoundAC SYSTEM PRIVATE ${CSOUND_INCLUDE_DIRS})
target_include_directories(libCsoundAC PRIVATE ${EIGEN3_INCLUDE_DIRS})
Expand All @@ -95,7 +93,7 @@ endif()
LIBRARY_OUTPUT_DIRECTORY "${BUILD_LIB_DIR}"
ARCHIVE_OUTPUT_DIRECTORY "${BUILD_LIB_DIR}")

target_link_libraries(libCsoundAC PRIVATE ${MUSICXML2_LIBRARY} ${CSND6_LIBRARY} ${CSOUND_LIBRARIES} ${OpenCV_LIBRARIES} ${LIBSNDFILE_LIBRARY})
target_link_libraries(libCsoundAC PRIVATE ${MUSICXML2_LIBRARY} ${CSOUND_LIBRARIES} ${OpenCV_LIBRARIES} ${LIBSNDFILE_LIBRARY})

if(WIN32) # for MSYS2/mingw64
# mingw64 needs this to create a valid .pyd.
Expand Down Expand Up @@ -139,7 +137,7 @@ endif()
swig_add_library(CsoundAC
LANGUAGE python
SOURCES CsoundAC.i)
swig_link_libraries(CsoundAC libCsoundAC ${MUSICXML2_LIBRARY} ${CSND6_LIBRARY} ${CSOUND_LIBRARIES} ${OpenCV_LIBRARIES} ${LIBSNDFILE_LIBRARY} ${Python3_LIBRARIES})
swig_link_libraries(CsoundAC libCsoundAC ${MUSICXML2_LIBRARY} ${CSOUND_LIBRARIES} ${OpenCV_LIBRARIES} ${LIBSNDFILE_LIBRARY} ${Python3_LIBRARIES})
target_include_directories(${SWIG_MODULE_CsoundAC_REAL_NAME} PRIVATE ${EIGEN3_INCLUDE_DIRS} ${Boost_INCLUDE_DIR} ${CSOUND_SOURCE_HOME}/include)
if(APPLE)
target_include_directories(${SWIG_MODULE_CsoundAC_REAL_NAME} SYSTEM PRIVATE ${LIBSNDFILE_INCLUDE_DIR})
Expand Down
219 changes: 219 additions & 0 deletions CsoundAC/CMaskNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
#ifndef CMASKNODE_HPP_INCLUDED
#define CMASKNODE_HPP_INCLUDED
/*
* C S O U N D
*
* L I C E N S E
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This software 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 General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "Platform.hpp"
#ifdef SWIG
%module CsoundAC
%{
#include "Conversions.hpp"
#include "Event.hpp"
#include "Score.hpp"
#include "ScoreNode.hpp"
#include <algorithm>
#include <Eigen/Dense>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
%}
%include "std_string.i"
%include "std_vector.i"
%template(EventVector) std::vector<csound::Event>;
#else
#include "Conversions.hpp"
#include "Event.hpp"
#include "Score.hpp"
#include "ScoreNode.hpp"
#include <algorithm>
#include <cerrno>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

namespace cmask {
using namespace std;
// Andre Bartetzki's original code is bodily included into this file
// rather than adding his code to the CsoundAC build system.
#include "../dependencies/cmask/source/globals.h"
#if !defined(NL)
#define NL "\n"
#endif
#include "../dependencies/cmask/source/event.h"
#include "../dependencies/cmask/source/field.h"
#include "../dependencies/cmask/source/fileio.h"
#include "../dependencies/cmask/source/gen.h"
#include "../dependencies/cmask/source/items.h"
#include "../dependencies/cmask/source/mask.h"
#include "../dependencies/cmask/source/parser.h"
#include "../dependencies/cmask/source/quant.h"
#include "../dependencies/cmask/source/tables.h"
#include "../dependencies/cmask/source/utils.h"
#include "../dependencies/cmask/source/cmask.cpp"
#include "../dependencies/cmask/source/event.cpp"
#include "../dependencies/cmask/source/field.cpp"
#include "../dependencies/cmask/source/fileio.cpp"
#include "../dependencies/cmask/source/gen.cpp"
#include "../dependencies/cmask/source/items.cpp"
#include "../dependencies/cmask/source/mask.cpp"
#include "../dependencies/cmask/source/parser.cpp"
#include "../dependencies/cmask/source/quant.cpp"
#include "../dependencies/cmask/source/tables.cpp"
#include "../dependencies/cmask/source/utils.cpp"
};
#endif

namespace csound
{
/**
* Uses the CMask library for tendency masks to generate events as a Csound
* score in the format determined by the CMask parameters text. The generated
* Csound score is also translated to a Silence score in the Silence music
* graph. This only works if at least the first 5 pfields in the Csound score
* (instrument number, time, duration, MIDI key, MIDI velocity) follow the
* Silence conventions declared in Event.hpp.
*
* Score literals in the CMask parameters are simply appended to the Csound
* score header in the Score object, and likewise are copied directly and
* without any processing to the translated Csound Score.
*
* For documentation and examples for CMask, see [Andre Bartetzki's original
* documentation](https://www.bartetzki.de/de/software.html).
*/

SILENCE_PUBLIC class CMaskNode : public ScoreNode {
public:
virtual std::string getParametersText() const {
return parameters_text;
}
virtual void setParametersText(const std::string &parameters_text_) {
parameters_text = parameters_text_;
}
virtual void generateLocally() {
cmask::frandinit();
cmask::scanner scanner_;
//std::cout << "CMask parameters: " << std::endl << parameters_text << std::endl;
// Rather than monkey with the existing code,
// we just use temporary files.
char parameters_filename[0x100];
std::tmpnam(parameters_filename);
std::string score_filename = parameters_filename;
score_filename.append(".sco");
std::ofstream parameters_file(parameters_filename);
parameters_file << parameters_text->data << std::endl;
parameters_file.close();
scanner_.scn(parameters_filename, const_cast<char *>(score_filename.c_str()));
scanner_.analyze();
std::ifstream score_file(score_filename);
std::stringstream score_buffer;
score_buffer << score_file.rdbuf();
score_file.close();
score_text = score_buffer.str();
//std::cout << "CMask generated: " << std::endl << score_text << std::endl;
std::remove(parameters_filename);
std::remove(score_filename.c_str());
translate_to_silence();
}
/**
* Maps Silence score fields to Csound "i" statement pfields. As noted
* above, the CMask fields _must_ be configured to match the Silence
* conventions for Csound scores. The first 5 fields are required.
* No more than 10 fields are used.
*
* i_instrument = p1
* i_time = p2
* i_duration = p3
* i_midi_key = p4
* i_midi_velocity = p5
* k_space_front_to_back = p6 ; Ambisonic X
* k_space_left_to_right = p7 ; Ambisonic Y
* k_space_bottom_to_top = p8; Ambisonic Z
* i_phase = p9
* i_pitches = p10 ; Mason number
*/
virtual void translate_to_silence() {
score.clear();
std::stringstream score_stream;
score_stream << score_text;
for (std::string line; std::getline(input, line); ) {
std::stringstream line_stream(line);
// All lines that don't begin with "i" are treated as
// Csound score literals and appended to the score header.
if (line.begins_with("i") == false) {
score.appendToCsoundScoreHeader(line);
score.appendToCsoundScoreHeader("\n");
} else {
csound::Event event;
int index = 0;
std::string token;
while (line_stream >> token) {
switch (index) {
case 0:
event.setStatus(144);
break;
case 1:
event.setInstrument(std::stof(token));
break;
case 2:
event.setTime(std::stof(token));
break;
case 3:
event.setDuration(std::stof(token));
break;
case 4:
event.setKey(std::stof(token));
break;
case 6:
event.setDepth(std::stof(token));
break;
case 7:
event.setPan(std::stof(token));
break;
case 8:
event.setHeight(std::stof(token));
break;
case 9:
event.setPhase(std::stof(token));
break;
case 10:
event.setPitches(std::stof(token));
break;
}
}
score.append(event);
}
}
}
protected:
std::string parameters_text;
std::string score_text;
}
};
Loading
Loading