| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2021 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <engine/processor.h> |
| 14 | |
| 15 | #include <config.h> |
| 16 | |
| 17 | using namespace BoCA; |
| 18 | using namespace BoCA::AS; |
| 19 | |
| 20 | freac::Processor::Processor(const BoCA::Config *iConfiguration) : Component(iConfiguration) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | freac::Processor::~Processor() |
| 25 | { |
| 26 | Destroy(); |
| 27 | } |
| 28 | |
| 29 | Bool freac::Processor::Create(const Track &track) |
| 30 | { |
| 31 | Registry &boca = Registry::Get(); |
| 32 | |
| 33 | format = track.GetFormat(); |
| 34 | |
| 35 | /* Get config values. |
| 36 | */ |
| 37 | Bool enableProcessing = configuration->GetIntValue(Config::CategoryProcessingID, Config::ProcessingEnableProcessingID, Config::ProcessingEnableProcessingDefault); |
| 38 | |
| 39 | if (!enableProcessing) return True; |
| 40 | |
| 41 | /* Create DSP components. |
| 42 | */ |
| 43 | const Array<String> &components = configuration->GetStringValue(Config::CategoryProcessingID, Config::ProcessingComponentsID, Config::ProcessingComponentsDefault).Explode(","); |
| 44 | Track dspTrack = track; |
| 45 | |
| 46 | foreach (const String &component, components) |
| 47 | { |
| 48 | AS::DSPComponent *dsp = (AS::DSPComponent *) boca.CreateComponentByID(component); |
| 49 | |
| 50 | if (dsp == NIL) |
| 51 | { |
| 52 | Destroy(); |
| 53 | |
| 54 | return False; |
| 55 | } |
| 56 | |
| 57 | /* Lock processor if it's not thread safe. |
| 58 | */ |
| 59 | LockComponent(dsp); |
| 60 | |
| 61 | /* Activate DSP component. |
| 62 | */ |
| 63 | dsp->SetConfiguration(configuration); |
| 64 | dsp->SetAudioTrackInfo(dspTrack); |
| 65 | |
| 66 | if (dsp->Activate() == False) |
| 67 | { |
| 68 | SetErrorInfo(True, "Could not set up DSP processor: %1\n\nError: %2", dsp->GetName(), dsp->GetErrorString()); |
| 69 | |
| 70 | UnlockComponent(dsp); |
| 71 | |
| 72 | boca.DeleteComponent(dsp); |
| 73 | |
| 74 | Destroy(); |
| 75 | |
| 76 | return False; |
| 77 | } |
| 78 | |
| 79 | format = dsp->GetFormatInfo(); |
| 80 | |
| 81 | dspTrack.AdjustSampleCounts(format); |
| 82 | dspTrack.SetFormat(format); |
| 83 | |
| 84 | dsps.Add(dsp); |
| 85 | } |
| 86 | |
| 87 | return True; |
| 88 | } |
| 89 | |
| 90 | Bool freac::Processor::Destroy() |
| 91 | { |
| 92 | Registry &boca = Registry::Get(); |
| 93 | |
| 94 | foreach (AS::DSPComponent *dsp, dsps) |
| 95 | { |
| 96 | dsp->Deactivate(); |
| 97 | |
| 98 | SetErrorInfo(dsp->GetErrorState(), dsp->GetErrorString()); |
| 99 | |
| 100 | UnlockComponent(dsp); |
| 101 | |
| 102 | boca.DeleteComponent(dsp); |
| 103 | } |
| 104 | |
| 105 | dsps.RemoveAll(); |
| 106 | |
| 107 | return True; |
| 108 | } |
| 109 | |
| 110 | Int freac::Processor::Transform(Buffer<UnsignedByte> &buffer) |
| 111 | { |
| 112 | /* Call transform for every DSP component. |
| 113 | */ |
| 114 | foreach (AS::DSPComponent *dsp, dsps) dsp->TransformData(buffer); |
| 115 | |
| 116 | return buffer.Size(); |
| 117 | } |
| 118 | |
| 119 | Int freac::Processor::Finish(Buffer<UnsignedByte> &buffer) |
| 120 | { |
| 121 | if (buffer.Size() != 0) return -1; |
| 122 | |
| 123 | foreach (AS::DSPComponent *dsp, dsps) |
| 124 | { |
| 125 | /* Transform data already in return buffer. |
| 126 | */ |
| 127 | if (buffer.Size() != 0) dsp->TransformData(buffer); |
| 128 | |
| 129 | /* Flush DSP component. |
| 130 | */ |
| 131 | Buffer<UnsignedByte> flush; |
| 132 | |
| 133 | dsp->Flush(flush); |
| 134 | |
| 135 | /* Append remaining data to return buffer. |
| 136 | */ |
| 137 | if (flush.Size() > 0) |
| 138 | { |
| 139 | buffer.Resize(buffer.Size() + flush.Size()); |
| 140 | |
| 141 | memcpy(buffer + buffer.Size() - flush.Size(), flush, flush.Size()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return buffer.Size(); |
| 146 | } |