From d686cb213f624fb6c006d00eeb53b0e2bf640330 Mon Sep 17 00:00:00 2001 From: Damien Zammit Date: Sat, 1 Aug 2015 13:41:26 +1000 Subject: [PATCH] Added PT .ptf session audio import functionality Signed-off-by: Damien Zammit --- gtk2_ardour/ardev_common.sh.in | 2 +- gtk2_ardour/ardour.menus.in | 3 + gtk2_ardour/editor.h | 14 + gtk2_ardour/editor_actions.cc | 3 + gtk2_ardour/editor_audio_import.cc | 19 +- gtk2_ardour/editor_pt_import.cc | 253 ++++++++++++ gtk2_ardour/wscript | 11 +- libs/ptformat/ptfformat.cc | 640 +++++++++++++++++++++++++++++ libs/ptformat/ptfformat.h | 131 ++++++ libs/ptformat/wscript | 43 ++ wscript | 8 + 11 files changed, 1117 insertions(+), 10 deletions(-) create mode 100644 gtk2_ardour/editor_pt_import.cc create mode 100644 libs/ptformat/ptfformat.cc create mode 100644 libs/ptformat/ptfformat.h create mode 100644 libs/ptformat/wscript diff --git a/gtk2_ardour/ardev_common.sh.in b/gtk2_ardour/ardev_common.sh.in index fc287e16ba..eca77916c0 100644 --- a/gtk2_ardour/ardev_common.sh.in +++ b/gtk2_ardour/ardev_common.sh.in @@ -34,7 +34,7 @@ export ARDOUR_DLL_PATH=$libs export GTK_PATH=~/.ardour3:$libs/clearlooks-newer export VAMP_PATH=$libs/vamp-plugins${VAMP_PATH:+:$VAMP_PATH} -export LD_LIBRARY_PATH=$libs/qm-dsp:$libs/vamp-sdk:$libs/surfaces:$libs/surfaces/control_protocol:$libs/ardour:$libs/midi++2:$libs/pbd:$libs/rubberband:$libs/soundtouch:$libs/gtkmm2ext:$libs/gnomecanvas:$libs/libsndfile:$libs/appleutility:$libs/taglib:$libs/evoral:$libs/evoral/src/libsmf:$libs/audiographer:$libs/timecode:$libs/libltc:$libs/canvas:$libs/ardouralsautil${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} +export LD_LIBRARY_PATH=$libs/ptformat:$libs/qm-dsp:$libs/vamp-sdk:$libs/surfaces:$libs/surfaces/control_protocol:$libs/ardour:$libs/midi++2:$libs/pbd:$libs/rubberband:$libs/soundtouch:$libs/gtkmm2ext:$libs/gnomecanvas:$libs/libsndfile:$libs/appleutility:$libs/taglib:$libs/evoral:$libs/evoral/src/libsmf:$libs/audiographer:$libs/timecode:$libs/libltc:$libs/canvas:$libs/ardouralsautil${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} # DYLD_LIBRARY_PATH is for darwin. export DYLD_FALLBACK_LIBRARY_PATH=$LD_LIBRARY_PATH diff --git a/gtk2_ardour/ardour.menus.in b/gtk2_ardour/ardour.menus.in index 086d2b88d7..27c030669e 100644 --- a/gtk2_ardour/ardour.menus.in +++ b/gtk2_ardour/ardour.menus.in @@ -22,6 +22,9 @@ +#ifdef PTFORMAT + +#endif diff --git a/gtk2_ardour/editor.h b/gtk2_ardour/editor.h index 1faf148e19..c137bd736a 100644 --- a/gtk2_ardour/editor.h +++ b/gtk2_ardour/editor.h @@ -410,6 +410,8 @@ class Editor : public PublicEditor, public PBD::ScopedConnectionList, public ARD int get_regionview_count_from_region_list (boost::shared_ptr); + void do_ptimport(std::string path, ARDOUR::SrcQuality quality); + void do_import (std::vector paths, Editing::ImportDisposition disposition, Editing::ImportMode mode, @@ -1317,6 +1319,18 @@ class Editor : public PublicEditor, public PBD::ScopedConnectionList, public ARD void insert_region_list_selection (float times); + /* PT import */ + void external_pt_dialog (); + typedef struct ptflookup { + uint16_t index1; + uint16_t index2; + PBD::ID id; + + bool operator ==(const struct ptflookup& other) { + return (this->index1 == other.index1); + } + } ptflookup_t; + /* import & embed */ void add_external_audio_action (Editing::ImportMode); diff --git a/gtk2_ardour/editor_actions.cc b/gtk2_ardour/editor_actions.cc index 71ce59406a..ebfe2d3fb7 100644 --- a/gtk2_ardour/editor_actions.cc +++ b/gtk2_ardour/editor_actions.cc @@ -688,6 +688,9 @@ Editor::register_actions () ActionManager::register_action (rl_actions, X_("removeUnusedRegions"), _("Remove Unused"), sigc::mem_fun (*_regions, &EditorRegions::remove_unused_regions)); + act = reg_sens (editor_actions, X_("addExistingPTFiles"), _("Import PT session"), sigc::mem_fun (*this, &Editor::external_pt_dialog)); + ActionManager::write_sensitive_actions.push_back (act); + /* the next two are duplicate items with different names for use in two different contexts */ act = reg_sens (editor_actions, X_("addExistingAudioFiles"), _("Import"), sigc::mem_fun (*this, &Editor::external_audio_dialog)); diff --git a/gtk2_ardour/editor_audio_import.cc b/gtk2_ardour/editor_audio_import.cc index 6be0a78136..406b255b46 100644 --- a/gtk2_ardour/editor_audio_import.cc +++ b/gtk2_ardour/editor_audio_import.cc @@ -303,6 +303,7 @@ Editor::do_import (vector paths, } else { ipw.show (); ok = (import_sndfiles (paths, disposition, mode, quality, pos, 1, 1, track, false, instrument) == 0); + import_status.sources.clear(); } } else { @@ -348,6 +349,7 @@ Editor::do_import (vector paths, } ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, -1, track, replace, instrument) == 0); + import_status.sources.clear(); break; case Editing::ImportDistinctChannels: @@ -356,6 +358,7 @@ Editor::do_import (vector paths, to_import.push_back (*a); ok = (import_sndfiles (to_import, disposition, mode, quality, pos, -1, -1, track, replace, instrument) == 0); + import_status.sources.clear(); break; case Editing::ImportSerializeFiles: @@ -364,6 +367,7 @@ Editor::do_import (vector paths, to_import.push_back (*a); ok = (import_sndfiles (to_import, disposition, mode, quality, pos, 1, 1, track, replace, instrument) == 0); + import_status.sources.clear(); break; case Editing::ImportMergeFiles: @@ -524,7 +528,6 @@ Editor::import_sndfiles (vector paths, pos = import_status.pos; } - import_status.sources.clear(); return result; } @@ -734,7 +737,7 @@ Editor::add_sources (vector paths, /* generate a per-channel region name so that things work as * intended */ - + string path; if (fs) { @@ -742,7 +745,7 @@ Editor::add_sources (vector paths, } else { region_name = (*x)->name(); } - + if (sources.size() == 2) { if (n == 0) { region_name += "-L"; @@ -752,9 +755,9 @@ Editor::add_sources (vector paths, } else if (sources.size() > 2) { region_name += string_compose ("-%1", n+1); } - + track_names.push_back (region_name); - + } else { if (fs) { region_name = region_name_from_path (fs->path(), false, false, sources.size(), n); @@ -825,7 +828,7 @@ Editor::add_sources (vector paths, * the API simpler. */ assert (regions.size() == track_names.size()); - + for (vector >::iterator r = regions.begin(); r != regions.end(); ++r, ++n) { boost::shared_ptr ar = boost::dynamic_pointer_cast (*r); @@ -857,7 +860,7 @@ Editor::add_sources (vector paths, pos = get_preferred_edit_position (); } } - + finish_bringing_in_material (*r, input_chan, output_chan, pos, mode, track, track_names[n], instrument); rlen = (*r)->length(); @@ -873,7 +876,7 @@ Editor::add_sources (vector paths, } commit_reversible_command (); - + /* setup peak file building in another thread */ for (SourceList::iterator x = sources.begin(); x != sources.end(); ++x) { diff --git a/gtk2_ardour/editor_pt_import.cc b/gtk2_ardour/editor_pt_import.cc new file mode 100644 index 0000000000..8689d94391 --- /dev/null +++ b/gtk2_ardour/editor_pt_import.cc @@ -0,0 +1,253 @@ +/* + Copyright (C) 2000-2006 Paul Davis + + 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. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include +#include +#include +#include +#include +#include + +#include "pbd/pthread_utils.h" +#include "pbd/basename.h" +#include "pbd/shortpath.h" +#include "pbd/stateful_diff_command.h" + +#include + +#include "ardour/audio_track.h" +#include "ardour/audiofilesource.h" +#include "ardour/audioregion.h" +#include "ardour/midi_region.h" +#include "ardour/midi_track.h" +#include "ardour/operations.h" +#include "ardour/region_factory.h" +#include "ardour/smf_source.h" +#include "ardour/source_factory.h" +#include "ardour/utils.h" +#include "ardour/playlist.h" +#include "ardour/session.h" +#include "pbd/memento_command.h" + +#include "ptformat/ptfformat.h" + +#include "ardour_ui.h" +#include "cursor_context.h" +#include "editor.h" +#include "sfdb_ui.h" +#include "editing.h" +#include "audio_time_axis.h" +#include "midi_time_axis.h" +#include "session_import_dialog.h" +#include "gui_thread.h" +#include "interthread_progress_window.h" +#include "mouse_cursors.h" +#include "editor_cursors.h" + +#include "i18n.h" + +using namespace std; +using namespace ARDOUR; +using namespace PBD; +using namespace Gtk; +using namespace Gtkmm2ext; +using namespace Editing; +using std::string; + +/* Functions supporting the incorporation of PT sessions into ardour */ + +void +Editor::external_pt_dialog () +{ + std::string ptpath; + + if (_session == 0) { + MessageDialog msg (_("You can't import a PT session until you have a session loaded.")); + msg.run (); + return; + } + + Gtk::FileChooserDialog dialog(_("Import PT Session"), FILE_CHOOSER_ACTION_OPEN); + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK); + + while (true) { + int result = dialog.run(); + + if (result == Gtk::RESPONSE_OK) { + ptpath = dialog.get_filename (); + + if (!Glib::file_test (ptpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) { + Gtk::MessageDialog msg (string_compose (_("%1: this is only the directory/folder name, not the filename.\n"), ptpath)); + msg.run (); + continue; + } + } + + if (ptpath.length()) { + do_ptimport(ptpath, SrcBest); + break; + } + + if (result == Gtk::RESPONSE_CANCEL) { + break; + } + } +} + +void +Editor::do_ptimport (std::string ptpath, + SrcQuality quality) +{ + vector > regions; + boost::shared_ptr track; + ARDOUR::PluginInfoPtr instrument; + vector to_import; + string fullpath; + bool ok = false; + PTFFormat ptf; + framepos_t pos = -1; + + vector ptfwavpair; + vector ptfregpair; + + if (ptf.load(ptpath) == -1) { + MessageDialog msg (_("Doesn't seem to be a valid PT session file (.ptf only currently supported)")); + msg.run (); + return; + } + + current_interthread_info = &import_status; + import_status.current = 1; + import_status.total = ptf.audiofiles.size (); + import_status.all_done = false; + + ImportProgressWindow ipw (&import_status, _("Import"), _("Cancel Import")); + + SourceList just_one; + SourceList imported; + + for (vector::iterator a = ptf.audiofiles.begin(); a != ptf.audiofiles.end(); ++a) { + ptflookup_t p; + + fullpath = Glib::build_filename (Glib::path_get_dirname(ptpath), "Audio Files"); + fullpath = Glib::build_filename (fullpath, a->filename); + to_import.clear (); + to_import.push_back (fullpath); + ipw.show (); + ok = import_sndfiles (to_import, Editing::ImportDistinctFiles, Editing::ImportAsRegion, quality, pos, 1, -1, track, false, instrument); + if (!import_status.sources.empty()) { + p.index1 = a->index; + p.id = import_status.sources.back()->id(); + + ptfwavpair.push_back(p); + imported.push_back(import_status.sources.back()); + } + } + + for (vector::iterator a = ptf.regions.begin(); + a != ptf.regions.end(); ++a) { + for (vector::iterator p = ptfwavpair.begin(); + p != ptfwavpair.end(); ++p) { + if (p->index1 == a->wave.index) { + for (SourceList::iterator x = imported.begin(); + x != imported.end(); ++x) { + if ((*x)->id() == p->id) { + // Matched an uncreated ptf region to ardour region + ptflookup_t rp; + PropertyList plist; + + plist.add (ARDOUR::Properties::start, a->sampleoffset); + plist.add (ARDOUR::Properties::position, 0); + plist.add (ARDOUR::Properties::length, a->length); + plist.add (ARDOUR::Properties::name, a->name); + plist.add (ARDOUR::Properties::layer, 0); + plist.add (ARDOUR::Properties::whole_file, false); + plist.add (ARDOUR::Properties::external, true); + + just_one.clear(); + just_one.push_back(*x); + + boost::shared_ptr r = RegionFactory::create (just_one, plist); + regions.push_back(r); + + rp.id = regions.back()->id(); + rp.index1 = a->index; + ptfregpair.push_back(rp); + } + } + } + } + } + + boost::shared_ptr existing_track; + uint16_t nth = 0; + vector usedtracks; + ptflookup_t utr; + + for (vector::iterator a = ptf.tracks.begin(); + a != ptf.tracks.end(); ++a) { + for (vector::iterator p = ptfregpair.begin(); + p != ptfregpair.end(); ++p) { + + if ((p->index1 == a->reg.index)) { + // Matched a ptf active region to an ardour region + utr.index1 = a->index; + utr.index2 = nth; + utr.id = p->id; + boost::shared_ptr r = RegionFactory::region_by_id (p->id); + vector::iterator lookuptr = usedtracks.begin(); + vector::iterator found; + if ((found = std::find(lookuptr, usedtracks.end(), utr)) != usedtracks.end()) { + DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) ptf_tr(%3) ard_tr(%4)\n", a->reg.wave.filename.c_str(), a->reg.index, found->index1, found->index2)); + existing_track = get_nth_selected_audio_track(found->index2); + // Put on existing track + boost::shared_ptr playlist = existing_track->playlist(); + boost::shared_ptr copy (RegionFactory::create (r, true)); + playlist->clear_changes (); + playlist->add_region (copy, a->reg.startpos); + //_session->add_command (new StatefulDiffCommand (playlist)); + } else { + // Put on a new track + DEBUG_TRACE (DEBUG::FileUtils, string_compose ("\twav(%1) reg(%2) new_tr(%3)\n", a->reg.wave.filename.c_str(), a->reg.index, nth)); + list > at (_session->new_audio_track (1, 2, Normal, 0, 1)); + if (at.empty()) { + return; + } + existing_track = at.back(); + existing_track->set_name (a->name); + boost::shared_ptr playlist = existing_track->playlist(); + boost::shared_ptr copy (RegionFactory::create (r, true)); + playlist->clear_changes (); + playlist->add_region (copy, a->reg.startpos); + //_session->add_command (new StatefulDiffCommand (playlist)); + nth++; + } + usedtracks.push_back(utr); + } + } + } + + import_status.sources.clear(); + + if (ok) { + _session->save_state (""); + } + import_status.all_done = true; +} diff --git a/gtk2_ardour/wscript b/gtk2_ardour/wscript index fde642c373..7a6f7dc240 100644 --- a/gtk2_ardour/wscript +++ b/gtk2_ardour/wscript @@ -61,6 +61,7 @@ gtk2_ardour_sources = [ 'editor.cc', 'editor_actions.cc', 'editor_audio_import.cc', + 'editor_pt_import.cc', 'editor_audiotrack.cc', 'editor_canvas.cc', 'editor_canvas_events.cc', @@ -397,6 +398,7 @@ def build(bld): 'libgtk2_ardour', 'libgtkmm2ext', 'libcanvas', + 'libptformat', ] obj.target = 'ardour-' + str (bld.env['VERSION']) + '-vst.exe.so' obj.includes = [ '../libs/fst', '.' ] @@ -412,6 +414,7 @@ def build(bld): obj.includes = [ '../libs/fst', '.' ] obj.name = 'libgtk2_ardour' obj.target = 'gtk2_ardour' + else: # just the normal executable version of the GTK GUI if bld.env['build_target'] == 'mingw': @@ -437,6 +440,7 @@ def build(bld): 'libmidipp', 'libgtkmm2ext', 'libcanvas', + 'libptformat', ] obj.defines = [ @@ -463,6 +467,8 @@ def build(bld): obj.includes += ['../libs'] + obj.use += 'libptformat' + if bld.env['build_target'] == 'mingw': obj.linkflags = ['-mwindows'] @@ -633,6 +639,9 @@ def build(bld): else: menus_argv = [ '-E', '-P' ] + if bld.is_defined('PTFORMAT'): + menus_argv += [ '-DPTFORMAT' ] + # always build all versions of the menu definitions # so that we can try them out with different program builds. for program in [ 'ardour', 'trx' ]: @@ -641,7 +650,7 @@ def build(bld): obj.command_is_external = True obj.no_inputs = True obj.argv = menus_argv - obj.dep_vars = ['GTKOSX', 'WINDOWS'] + obj.dep_vars = ['PTFORMAT', 'GTKOSX', 'WINDOWS'] obj.stdin = program + '.menus.in' obj.stdout = program + '.menus' bld.install_files (bld.env['CONFDIR'], program + '.menus') diff --git a/libs/ptformat/ptfformat.cc b/libs/ptformat/ptfformat.cc new file mode 100644 index 0000000000..dde7367c6c --- /dev/null +++ b/libs/ptformat/ptfformat.cc @@ -0,0 +1,640 @@ +/* + Copyright (C) 2015 Damien Zammit + Copyright (C) 2015 Robin Gareus + + 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 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + +*/ + +#include "ptfformat.h" + +#include +#include +#include + +using namespace std; + +static const uint32_t baselut[16] = { + 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955, + 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5, + 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d, + 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd +}; + +static const uint32_t xorlut[16] = { + 0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010, + 0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb, + 0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab, + 0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa +}; + +static const uint32_t swapbytes32 (const uint32_t v) { + uint32_t rv = 0; + rv |= ((v >> 0) & 0xf) << 28; + rv |= ((v >> 4) & 0xf) << 24; + rv |= ((v >> 8) & 0xf) << 20; + rv |= ((v >> 12) & 0xf) << 16; + rv |= ((v >> 16) & 0xf) << 12; + rv |= ((v >> 20) & 0xf) << 8; + rv |= ((v >> 24) & 0xf) << 4; + rv |= ((v >> 28) & 0xf) << 0; + return rv; +} + +static const uint64_t gen_secret (int i) { + assert (i > 0 && i < 256); + int iwrap = i & 0x7f; // wrap at 0x80; + uint32_t xor_lo = 0; // 0x40 flag + int idx; // mirror at 0x40; + + if (iwrap & 0x40) { + xor_lo = 0x1; + idx = 0x80 - iwrap; + } else { + idx = iwrap; + } + + int i16 = (idx >> 1) & 0xf; + if (idx & 0x20) { + i16 = 15 - i16; + } + + uint32_t lo = baselut [i16]; + uint32_t xk = xorlut [i16]; + + if (idx & 0x20) { + lo ^= 0xaaaaaaab; + xk ^= 0x10000000; + } + uint32_t hi = swapbytes32 (lo) ^ xk; + return ((uint64_t)hi << 32) | (lo ^ xor_lo); +} + +PTFFormat::PTFFormat() { +} + +PTFFormat::~PTFFormat() { + if (ptfunxored) { + free(ptfunxored); + } +} + +bool +PTFFormat::foundin(std::string haystack, std::string needle) { + size_t found = haystack.find(needle); + if (found != std::string::npos) { + return true; + } else { + return false; + } +} + +/* Return values: 0 success + 0x01 to 0xff value of missing lut + -1 could not open file as ptf +*/ +int +PTFFormat::load(std::string path) { + FILE *fp; + unsigned char xxor[256]; + unsigned char ct; + unsigned char px; + uint64_t key; + uint16_t i; + int j; + int inv; + unsigned char message; + + if (! (fp = fopen(path.c_str(), "rb"))) { + return -1; + } + + fseek(fp, 0, SEEK_END); + len = ftell(fp); + if (len < 0x40) { + fclose(fp); + return -1; + } + fseek(fp, 0x40, SEEK_SET); + fread(&c0, 1, 1, fp); + fread(&c1, 1, 1, fp); + + if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) { + /* Silently fail -- out of memory*/ + fclose(fp); + ptfunxored = 0; + return -1; + } + + i = 2; + + fseek(fp, 0x0, SEEK_SET); + + switch (c0) { + case 0x00: + // Success! easy one + xxor[0] = c0; + xxor[1] = c1; + for (i = 2; i < 64; i++) { + xxor[i] = (xxor[i-1] + c1 - c0) & 0xff; + } + px = xxor[0]; + fread(&ct, 1, 1, fp); + message = px ^ ct; + ptfunxored[0] = message; + px = xxor[1]; + fread(&ct, 1, 1, fp); + message = px ^ ct; + ptfunxored[1] = message; + i = 2; + j = 2; + while (fread(&ct, 1, 1, fp) != 0) { + if (i%64 == 0) { + i = 0; + } + message = xxor[i] ^ ct; + ptfunxored[j] = message; + i++; + j++; + } + break; + case 0x80: + //Success! easy two + xxor[0] = c0; + xxor[1] = c1; + for (i = 2; i < 256; i++) { + if (i%64 == 0) { + xxor[i] = c0; + } else { + xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff); + } + } + for (i = 0; i < 64; i++) { + xxor[i] ^= 0x80; + } + for (i = 128; i < 192; i++) { + xxor[i] ^= 0x80; + } + px = xxor[0]; + fread(&ct, 1, 1, fp); + message = px ^ ct; + ptfunxored[0] = message; + px = xxor[1]; + fread(&ct, 1, 1, fp); + message = px ^ ct; + ptfunxored[1] = message; + i = 2; + j = 2; + while (fread(&ct, 1, 1, fp) != 0) { + if (i%256 == 0) { + i = 0; + } + message = xxor[i] ^ ct; + ptfunxored[j] = message; + i++; + j++; + } + break; + case 0x40: + case 0xc0: + xxor[0] = c0; + xxor[1] = c1; + for (i = 2; i < 256; i++) { + if (i%64 == 0) { + xxor[i] = c0; + } else { + xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff); + } + } + + key = gen_secret(c1); + for (i = 0; i < 64; i++) { + xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40; + } + for (i = 128; i < 192; i++) { + inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3; + xxor[i] ^= (inv * 0x40); + } + + for (i = 192; i < 256; i++) { + xxor[i] ^= 0x80; + } + px = xxor[0]; + fread(&ct, 1, 1, fp); + message = px ^ ct; + ptfunxored[0] = message; + px = xxor[1]; + fread(&ct, 1, 1, fp); + message = px ^ ct; + ptfunxored[1] = message; + i = 2; + j = 2; + while (fread(&ct, 1, 1, fp) != 0) { + if (i%256 == 0) { + i = 0; + } + message = xxor[i] ^ ct; + ptfunxored[j] = message; + i++; + j++; + } + break; + break; + default: + //Should not happen, failed c[0] c[1] + return -1; + break; + } + fclose(fp); + parse(); + return 0; +} + +void +PTFFormat::parse(void) { + this->version = ptfunxored[61]; + + if (this->version == 8) { + parse8header(); + parserest(); + } else if (this->version == 9) { + parse9header(); + parserest(); + } else { + // Should not occur + } +} + +void +PTFFormat::parse8header(void) { + int k; + + // Find session sample rate + k = 0; + while (k < len) { + if ( (ptfunxored[k ] == 0x5a) && + (ptfunxored[k+1] == 0x05)) { + break; + } + k++; + } + + this->sessionrate = 0; + this->sessionrate |= ptfunxored[k+11]; + this->sessionrate |= ptfunxored[k+12] << 8; + this->sessionrate |= ptfunxored[k+13] << 16; +} + +void +PTFFormat::parse9header(void) { + int k; + + // Find session sample rate + k = 0x100; + while (k < len) { + if ( (ptfunxored[k ] == 0x5a) && + (ptfunxored[k+1] == 0x06)) { + break; + } + k++; + } + + this->sessionrate = 0; + this->sessionrate |= ptfunxored[k+11]; + this->sessionrate |= ptfunxored[k+12] << 8; + this->sessionrate |= ptfunxored[k+13] << 16; +} + +void +PTFFormat::parserest(void) { + int i,j,k,l; + + // Find end of wav file list + k = 0; + while (k < len) { + if ( (ptfunxored[k ] == 0xff) && + (ptfunxored[k+1] == 0xff) && + (ptfunxored[k+2] == 0xff) && + (ptfunxored[k+3] == 0xff)) { + break; + } + k++; + } + + j = 0; + l = 0; + + // Find actual wav names + bool first = true; + uint16_t numberofwavs; + char wavname[256]; + for (i = k; i > 4; i--) { + if ( (ptfunxored[i ] == 'W') && + (ptfunxored[i-1] == 'A') && + (ptfunxored[i-2] == 'V') && + (ptfunxored[i-3] == 'E')) { + j = i-4; + l = 0; + while (ptfunxored[j] != '\0') { + wavname[l] = ptfunxored[j]; + l++; + j--; + } + wavname[l] = 0; + //uint8_t playlist = ptfunxored[j-8]; + + if (first) { + first = false; + for (j = k; j > 4; j--) { + if ( (ptfunxored[j ] == 0x01) && + (ptfunxored[j-1] == 0x5a)) { + + numberofwavs = 0; + numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24); + numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16); + numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8); + numberofwavs |= (uint32_t)(ptfunxored[j-5]); + //printf("%d wavs\n", numberofwavs); + break; + } + k--; + } + } + + std::string wave = string(wavname); + std::reverse(wave.begin(), wave.end()); + wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0 }; + + if (foundin(wave, string(".grp"))) { + continue; + } + + actualwavs.push_back(f); + + numberofwavs--; + if (numberofwavs <= 0) + break; + } + } + + // Find Regions + uint8_t startbytes = 0; + uint8_t lengthbytes = 0; + uint8_t offsetbytes = 0; + uint8_t somethingbytes = 0; + uint8_t skipbytes = 0; + + while (k < len) { + if ( (ptfunxored[k ] == 'S') && + (ptfunxored[k+1] == 'n') && + (ptfunxored[k+2] == 'a') && + (ptfunxored[k+3] == 'p')) { + break; + } + k++; + } + first = true; + uint16_t rindex = 0; + uint32_t findex = 0; + for (i = k; i < len-70; i++) { + if ( (ptfunxored[i ] == 0x5a) && + (ptfunxored[i+1] == 0x0a)) { + break; + } + if ( (ptfunxored[i ] == 0x5a) && + (ptfunxored[i+1] == 0x0c)) { + + uint8_t lengthofname = ptfunxored[i+9]; + + char name[256] = {0}; + for (j = 0; j < lengthofname; j++) { + name[j] = ptfunxored[i+13+j]; + } + name[j] = '\0'; + j += i+13; + //uint8_t disabled = ptfunxored[j]; + + offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4; + lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4; + startbytes = (ptfunxored[j+3] & 0xf0) >> 4; + somethingbytes = (ptfunxored[j+3] & 0xf); + skipbytes = ptfunxored[j+4]; + findex = ptfunxored[j+5 + +startbytes + +lengthbytes + +offsetbytes + +somethingbytes + +skipbytes + +40]; + /*rindex = ptfunxored[j+5 + +startbytes + +lengthbytes + +offsetbytes + +somethingbytes + +skipbytes + +24]; + */ + uint32_t sampleoffset = 0; + switch (offsetbytes) { + case 4: + sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24); + case 3: + sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16); + case 2: + sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8); + case 1: + sampleoffset |= (uint32_t)(ptfunxored[j+5]); + default: + break; + } + j+=offsetbytes; + uint32_t length = 0; + switch (lengthbytes) { + case 4: + length |= (uint32_t)(ptfunxored[j+8] << 24); + case 3: + length |= (uint32_t)(ptfunxored[j+7] << 16); + case 2: + length |= (uint32_t)(ptfunxored[j+6] << 8); + case 1: + length |= (uint32_t)(ptfunxored[j+5]); + default: + break; + } + j+=lengthbytes; + uint32_t start = 0; + switch (startbytes) { + case 4: + start |= (uint32_t)(ptfunxored[j+8] << 24); + case 3: + start |= (uint32_t)(ptfunxored[j+7] << 16); + case 2: + start |= (uint32_t)(ptfunxored[j+6] << 8); + case 1: + start |= (uint32_t)(ptfunxored[j+5]); + default: + break; + } + j+=startbytes; + uint32_t something = 0; + switch (somethingbytes) { + case 4: + something |= (uint32_t)(ptfunxored[j+8] << 24); + case 3: + something |= (uint32_t)(ptfunxored[j+7] << 16); + case 2: + something |= (uint32_t)(ptfunxored[j+6] << 8); + case 1: + something |= (uint32_t)(ptfunxored[j+5]); + default: + break; + } + j+=somethingbytes; + std::string filename = string(name) + ".wav"; + wav_t f = { + filename, + 0, + (int64_t)start, + (int64_t)length, + }; + + f.index = findex; + //printf("something=%d\n", something); + + vector::iterator begin = this->actualwavs.begin(); + vector::iterator finish = this->actualwavs.end(); + vector::iterator found; + // Add file to list only if it is an actual wav + if ((found = std::find(begin, finish, f)) != finish) { + this->audiofiles.push_back(f); + // Also add plain wav as region + region_t r = { + name, + rindex, + start, + sampleoffset, + length, + f + }; + this->regions.push_back(r); + // Region only + } else { + if (foundin(filename, string(".grp"))) { + continue; + } + region_t r = { + name, + rindex, + start, + sampleoffset, + length, + f + }; + this->regions.push_back(r); + } + rindex++; + } + } + + while (k < len) { + if ( (ptfunxored[k ] == 0x5a) && + (ptfunxored[k+1] == 0x03)) { + break; + } + k++; + } + while (k < len) { + if ( (ptfunxored[k ] == 0x5a) && + (ptfunxored[k+1] == 0x02)) { + break; + } + k++; + } + k++; + + // Tracks + uint32_t offset; + uint32_t tracknumber = 0; + uint32_t regionspertrack = 0; + for (;k < len; k++) { + if ( (ptfunxored[k ] == 0x5a) && + (ptfunxored[k+1] == 0x04)) { + break; + } + if ( (ptfunxored[k ] == 0x5a) && + (ptfunxored[k+1] == 0x02)) { + + uint8_t lengthofname = 0; + lengthofname = ptfunxored[k+9]; + if (lengthofname == 0x5a) { + continue; + } + track_t tr; + + regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]); + + //printf("regions/track=%d\n", regionspertrack); + char name[256] = {0}; + for (j = 0; j < lengthofname; j++) { + name[j] = ptfunxored[j+k+13]; + } + name[j] = '\0'; + tr.name = string(name); + tr.index = tracknumber++; + + for (j = k; regionspertrack > 0 && j < len; j++) { + for (l = j; l < len; l++) { + if ( (ptfunxored[l ] == 0x5a) && + (ptfunxored[l+1] == 0x07)) { + j = l; + break; + } + } + + + if (regionspertrack == 0) { + // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5]; + break; + } else { + + tr.reg.index = (uint8_t)(ptfunxored[l+11]); + vector::iterator begin = this->regions.begin(); + vector::iterator finish = this->regions.end(); + vector::iterator found; + if ((found = std::find(begin, finish, tr.reg)) != finish) { + tr.reg = (*found); + } + startbytes = (ptfunxored[l+3] & 0xf0) >> 4; + + i = l+16; + offset = 0; + switch (startbytes) { + case 4: + offset |= (uint32_t)(ptfunxored[i+3] << 24); + case 3: + offset |= (uint32_t)(ptfunxored[i+2] << 16); + case 2: + offset |= (uint32_t)(ptfunxored[i+1] << 8); + case 1: + offset |= (uint32_t)(ptfunxored[i]); + default: + break; + } + tr.reg.startpos = (int64_t)offset; + if (tr.reg.length > 0) { + this->tracks.push_back(tr); + } + regionspertrack--; + } + } + } + } +} diff --git a/libs/ptformat/ptfformat.h b/libs/ptformat/ptfformat.h new file mode 100644 index 0000000000..0f60dc3335 --- /dev/null +++ b/libs/ptformat/ptfformat.h @@ -0,0 +1,131 @@ +/* + Copyright (C) 2015 Damien Zammit + + 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 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + +*/ +#ifndef PTFFORMAT_H +#define PTFFORMAT_H + +#include +#include +#include +#include + +class PTFFormat { +public: + PTFFormat(); + ~PTFFormat(); + + /* Return values: 0 success + -1 could not open file as ptf + */ + int load(std::string path); + + typedef struct wav { + std::string filename; + uint16_t index; + + int64_t posabsolute; + int64_t length; + + bool operator ==(const struct wav& other) { + return (this->index == other.index); + } + + } wav_t; + + typedef struct region { + std::string name; + uint16_t index; + int64_t startpos; + int64_t sampleoffset; + int64_t length; + wav_t wave; + + bool operator ==(const struct region& other) { + return (this->index == other.index); + } + } region_t; + + typedef struct track { + std::string name; + uint16_t index; + uint8_t playlist; + region_t reg; + + bool operator ==(const struct track& other) { + return (this->index == other.index); + } + } track_t; + + std::vector audiofiles; + std::vector regions; + std::vector tracks; + + static bool trackexistsin(std::vector tr, uint16_t index) { + std::vector::iterator begin = tr.begin(); + std::vector::iterator finish = tr.end(); + std::vector::iterator found; + + track_t f = { std::string(""), index }; + + if ((found = std::find(begin, finish, f)) != finish) { + return true; + } + return false; + } + + static bool regionexistsin(std::vector reg, uint16_t index) { + std::vector::iterator begin = reg.begin(); + std::vector::iterator finish = reg.end(); + std::vector::iterator found; + + region_t r = { std::string(""), index }; + + if ((found = std::find(begin, finish, r)) != finish) { + return true; + } + return false; + } + + static bool wavexistsin(std::vector wv, uint16_t index) { + std::vector::iterator begin = wv.begin(); + std::vector::iterator finish = wv.end(); + std::vector::iterator found; + + wav_t w = { std::string(""), index }; + + if ((found = std::find(begin, finish, w)) != finish) { + return true; + } + return false; + } + + uint32_t sessionrate; + uint8_t version; + + unsigned char c0; + unsigned char c1; + unsigned char *ptfunxored; + int len; + +private: + bool foundin(std::string haystack, std::string needle); + void parse(void); + void parse8header(void); + void parse9header(void); + void parserest(void); + std::vector actualwavs; +}; + + +#endif diff --git a/libs/ptformat/wscript b/libs/ptformat/wscript new file mode 100644 index 0000000000..3b191d3c75 --- /dev/null +++ b/libs/ptformat/wscript @@ -0,0 +1,43 @@ +#!/usr/bin/env python +from waflib.extras import autowaf as autowaf +import os + +# Version of this package (even if built as a child) +LIBPTFORMAT_VERSION = '0.0.0' + +# Library version (UNIX style major, minor, micro) +# major increment <=> incompatible changes +# minor increment <=> compatible changes (additions) +# micro increment <=> no interface changes +LIBPTFORMAT_LIB_VERSION = '0.0.0' + +# Variables for 'waf dist' +APPNAME = 'libptformat' +VERSION = LIBPTFORMAT_VERSION + +# Mandatory variables +top = '.' +out = 'build' + +def options(opt): + autowaf.set_options(opt) + +def configure(conf): + conf.load('compiler_cxx') + autowaf.configure(conf) + +def build(bld): + # Library + obj = bld(features = 'cxx cxxshlib') + obj.source = 'ptfformat.cc' + obj.export_includes = ['.'] + obj.includes = ['.'] + obj.name = 'libptformat' + obj.target = 'ptformat' + obj.use = 'libardour' + autowaf.ensure_visible_symbols (obj, True) + obj.vnum = LIBPTFORMAT_LIB_VERSION + obj.install_path = os.path.join(bld.env['LIBDIR'], 'ptformat') + +def shutdown(): + autowaf.shutdown() diff --git a/wscript b/wscript index 7d806858a0..bcffc419b8 100644 --- a/wscript +++ b/wscript @@ -195,6 +195,7 @@ children = [ 'libs/qm-dsp', 'libs/vamp-plugins', 'libs/libltc', + 'libs/ptformat', # core ardour libraries 'libs/pbd', 'libs/midi++2', @@ -706,6 +707,9 @@ def options(opt): help='Turn on c++11 compiler flags (-std=c++11)') opt.add_option('--address-sanitizer', action='store_true', default=False, dest='asan', help='Turn on AddressSanitizer (requires GCC >= 4.8 or clang >= 3.1)') + opt.add_option('--ptformat', action='store_true', default=False, dest='ptformat', + help='Turn on PT session import option') + for i in children: opt.recurse(i) @@ -998,6 +1002,9 @@ def configure(conf): conf.env['DEBUG_DENORMAL_EXCEPTION'] = True if opts.build_tests: autowaf.check_pkg(conf, 'cppunit', uselib_store='CPPUNIT', atleast_version='1.12.0', mandatory=True) + if opts.ptformat: + conf.define('PTFORMAT', 1) + conf.env['PTFORMAT'] = True backends = opts.with_backends.split(',') @@ -1096,6 +1103,7 @@ const char* const ardour_config_info = "\\n\\ write_config_text('Phone home', conf.is_defined('PHONE_HOME')) write_config_text('Program name', opts.program_name) write_config_text('Samplerate', conf.is_defined('HAVE_SAMPLERATE')) + write_config_text('PT format', conf.is_defined('PTFORMAT')) # write_config_text('Soundtouch', conf.is_defined('HAVE_SOUNDTOUCH')) write_config_text('Translation', opts.nls) # write_config_text('Tranzport', opts.tranzport)