|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2013 - Raw Material Software Ltd.
-
- Permission is granted to use this software under the terms of either:
- a) the GPL v2 (or any later version)
- b) the Affero GPL v3
-
- Details of these licenses can be found at: www.gnu.org/licenses
-
- JUCE 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.
-
- ------------------------------------------------------------------------------
-
- To release a closed-source product which uses JUCE, commercial licenses are
- available: visit www.juce.com for more information.
-
- ==============================================================================
- */
-
- const int kilobytesPerSecond1x = 176;
-
- struct AudioTrackProducerClass : public ObjCClass <NSObject>
- {
- AudioTrackProducerClass() : ObjCClass <NSObject> ("JUCEAudioTrackProducer_")
- {
- addIvar<AudioSourceHolder*> ("source");
-
- addMethod (@selector (initWithAudioSourceHolder:), initWithAudioSourceHolder, "@@:^v");
- addMethod (@selector (cleanupTrackAfterBurn:), cleanupTrackAfterBurn, "v@:@");
- addMethod (@selector (cleanupTrackAfterVerification:), cleanupTrackAfterVerification, "c@:@");
- addMethod (@selector (estimateLengthOfTrack:), estimateLengthOfTrack, "Q@:@");
- addMethod (@selector (prepareTrack:forBurn:toMedia:), prepareTrack, "c@:@@@");
- addMethod (@selector (prepareTrackForVerification:), prepareTrackForVerification, "c@:@");
- addMethod (@selector (produceDataForTrack:intoBuffer:length:atAddress:blockSize:ioFlags:),
- produceDataForTrack, "I@:@^cIQI^I");
- addMethod (@selector (producePreGapForTrack:intoBuffer:length:atAddress:blockSize:ioFlags:),
- produceDataForTrack, "I@:@^cIQI^I");
- addMethod (@selector (verifyDataForTrack:intoBuffer:length:atAddress:blockSize:ioFlags:),
- produceDataForTrack, "I@:@^cIQI^I");
-
- registerClass();
- }
-
- struct AudioSourceHolder
- {
- AudioSourceHolder (AudioSource* s, int numFrames)
- : source (s), readPosition (0), lengthInFrames (numFrames)
- {
- }
-
- ~AudioSourceHolder()
- {
- if (source != nullptr)
- source->releaseResources();
- }
-
- ScopedPointer<AudioSource> source;
- int readPosition, lengthInFrames;
- };
-
- private:
- static id initWithAudioSourceHolder (id self, SEL, AudioSourceHolder* source)
- {
- self = sendSuperclassMessage (self, @selector (init));
- object_setInstanceVariable (self, "source", source);
- return self;
- }
-
- static AudioSourceHolder* getSource (id self)
- {
- return getIvar<AudioSourceHolder*> (self, "source");
- }
-
- static void dealloc (id self, SEL)
- {
- delete getSource (self);
- sendSuperclassMessage (self, @selector (dealloc));
- }
-
- static void cleanupTrackAfterBurn (id self, SEL, DRTrack*) {}
- static BOOL cleanupTrackAfterVerification (id self, SEL, DRTrack*) { return true; }
-
- static uint64_t estimateLengthOfTrack (id self, SEL, DRTrack*)
- {
- return getSource (self)->lengthInFrames;
- }
-
- static BOOL prepareTrack (id self, SEL, DRTrack*, DRBurn*, NSDictionary*)
- {
- if (AudioSourceHolder* const source = getSource (self))
- {
- source->source->prepareToPlay (44100 / 75, 44100);
- source->readPosition = 0;
- }
-
- return true;
- }
-
- static BOOL prepareTrackForVerification (id self, SEL, DRTrack*)
- {
- if (AudioSourceHolder* const source = getSource (self))
- source->source->prepareToPlay (44100 / 75, 44100);
-
- return true;
- }
-
- static uint32_t produceDataForTrack (id self, SEL, DRTrack*, char* buffer,
- uint32_t bufferLength, uint64_t /*address*/,
- uint32_t /*blockSize*/, uint32_t* /*flags*/)
- {
- if (AudioSourceHolder* const source = getSource (self))
- {
- const int numSamples = jmin ((int) bufferLength / 4,
- (source->lengthInFrames * (44100 / 75)) - source->readPosition);
-
- if (numSamples > 0)
- {
- AudioSampleBuffer tempBuffer (2, numSamples);
- AudioSourceChannelInfo info (tempBuffer);
-
- source->source->getNextAudioBlock (info);
-
- typedef AudioData::Pointer <AudioData::Int16, AudioData::LittleEndian, AudioData::Interleaved, AudioData::NonConst> CDSampleFormat;
- typedef AudioData::Pointer <AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::Const> SourceSampleFormat;
-
- CDSampleFormat left (buffer, 2);
- left.convertSamples (SourceSampleFormat (tempBuffer.getSampleData (0)), numSamples);
- CDSampleFormat right (buffer + 2, 2);
- right.convertSamples (SourceSampleFormat (tempBuffer.getSampleData (1)), numSamples);
-
- source->readPosition += numSamples;
- }
-
- return numSamples * 4;
- }
-
- return 0;
- }
-
- static uint32_t producePreGapForTrack (id self, SEL, DRTrack*, char* buffer,
- uint32_t bufferLength, uint64_t /*address*/,
- uint32_t /*blockSize*/, uint32_t* /*flags*/)
- {
- zeromem (buffer, bufferLength);
- return bufferLength;
- }
-
- static BOOL verifyDataForTrack (id self, SEL, DRTrack*, const char*,
- uint32_t /*bufferLength*/, uint64_t /*address*/,
- uint32_t /*blockSize*/, uint32_t* /*flags*/)
- {
- return true;
- }
- };
-
- struct OpenDiskDevice
- {
- OpenDiskDevice (DRDevice* d)
- : device (d),
- tracks ([[NSMutableArray alloc] init]),
- underrunProtection (true)
- {
- }
-
- ~OpenDiskDevice()
- {
- [tracks release];
- }
-
- void addSourceTrack (AudioSource* source, int numSamples)
- {
- if (source != nullptr)
- {
- const int numFrames = (numSamples + 587) / 588;
-
- static AudioTrackProducerClass cls;
-
- NSObject* producer = [cls.createInstance() performSelector: @selector (initWithAudioSourceHolder:)
- withObject: (id) new AudioTrackProducerClass::AudioSourceHolder (source, numFrames)];
- DRTrack* track = [[DRTrack alloc] initWithProducer: producer];
-
- {
- NSMutableDictionary* p = [[track properties] mutableCopy];
- [p setObject: [DRMSF msfWithFrames: numFrames] forKey: DRTrackLengthKey];
- [p setObject: [NSNumber numberWithUnsignedShort: 2352] forKey: DRBlockSizeKey];
- [p setObject: [NSNumber numberWithInt: 0] forKey: DRDataFormKey];
- [p setObject: [NSNumber numberWithInt: 0] forKey: DRBlockTypeKey];
- [p setObject: [NSNumber numberWithInt: 0] forKey: DRTrackModeKey];
- [p setObject: [NSNumber numberWithInt: 0] forKey: DRSessionFormatKey];
- [track setProperties: p];
- [p release];
- }
-
- [tracks addObject: track];
-
- [track release];
- [producer release];
- }
- }
-
- String burn (AudioCDBurner::BurnProgressListener* listener,
- bool shouldEject, bool peformFakeBurnForTesting, int burnSpeed)
- {
- DRBurn* burn = [DRBurn burnForDevice: device];
-
- if (! [device acquireExclusiveAccess])
- return "Couldn't open or write to the CD device";
-
- [device acquireMediaReservation];
-
- NSMutableDictionary* d = [[burn properties] mutableCopy];
- [d autorelease];
- [d setObject: [NSNumber numberWithBool: peformFakeBurnForTesting] forKey: DRBurnTestingKey];
- [d setObject: [NSNumber numberWithBool: false] forKey: DRBurnVerifyDiscKey];
- [d setObject: (shouldEject ? DRBurnCompletionActionEject : DRBurnCompletionActionMount) forKey: DRBurnCompletionActionKey];
-
- if (burnSpeed > 0)
- [d setObject: [NSNumber numberWithFloat: burnSpeed * kilobytesPerSecond1x] forKey: DRBurnRequestedSpeedKey];
-
- if (! underrunProtection)
- [d setObject: [NSNumber numberWithBool: false] forKey: DRBurnUnderrunProtectionKey];
-
- [burn setProperties: d];
-
- [burn writeLayout: tracks];
-
- for (;;)
- {
- Thread::sleep (300);
- float progress = [[[burn status] objectForKey: DRStatusPercentCompleteKey] floatValue];
-
- if (listener != nullptr && listener->audioCDBurnProgress (progress))
- {
- [burn abort];
- return "User cancelled the write operation";
- }
-
- if ([[[burn status] objectForKey: DRStatusStateKey] isEqualTo: DRStatusStateFailed])
- return "Write operation failed";
-
- if ([[[burn status] objectForKey: DRStatusStateKey] isEqualTo: DRStatusStateDone])
- break;
-
- NSString* err = (NSString*) [[[burn status] objectForKey: DRErrorStatusKey]
- objectForKey: DRErrorStatusErrorStringKey];
- if ([err length] > 0)
- return nsStringToJuce (err);
- }
-
- [device releaseMediaReservation];
- [device releaseExclusiveAccess];
- return String::empty;
- }
-
- DRDevice* device;
- NSMutableArray* tracks;
- bool underrunProtection;
- };
-
- //==============================================================================
- class AudioCDBurner::Pimpl : public Timer
- {
- public:
- Pimpl (AudioCDBurner& b, int deviceIndex) : owner (b)
- {
- if (DRDevice* dev = [[DRDevice devices] objectAtIndex: deviceIndex])
- {
- device = new OpenDiskDevice (dev);
- lastState = getDiskState();
- startTimer (1000);
- }
- }
-
- ~Pimpl()
- {
- stopTimer();
- }
-
- void timerCallback() override
- {
- const DiskState state = getDiskState();
-
- if (state != lastState)
- {
- lastState = state;
- owner.sendChangeMessage();
- }
- }
-
- DiskState getDiskState() const
- {
- if ([device->device isValid])
- {
- NSDictionary* status = [device->device status];
- NSString* state = [status objectForKey: DRDeviceMediaStateKey];
-
- if ([state isEqualTo: DRDeviceMediaStateNone])
- {
- if ([[status objectForKey: DRDeviceIsTrayOpenKey] boolValue])
- return trayOpen;
-
- return noDisc;
- }
-
- if ([state isEqualTo: DRDeviceMediaStateMediaPresent])
- {
- if ([[[status objectForKey: DRDeviceMediaInfoKey] objectForKey: DRDeviceMediaBlocksFreeKey] intValue] > 0)
- return writableDiskPresent;
-
- return readOnlyDiskPresent;
- }
- }
-
- return unknown;
- }
-
- bool openTray() { return [device->device isValid] && [device->device ejectMedia]; }
-
- Array<int> getAvailableWriteSpeeds() const
- {
- Array<int> results;
-
- if ([device->device isValid])
- for (id kbPerSec in [[[device->device status] objectForKey: DRDeviceMediaInfoKey] objectForKey: DRDeviceBurnSpeedsKey])
- results.add ([kbPerSec intValue] / kilobytesPerSecond1x);
-
- return results;
- }
-
- bool setBufferUnderrunProtection (const bool shouldBeEnabled)
- {
- if ([device->device isValid])
- {
- device->underrunProtection = shouldBeEnabled;
- return shouldBeEnabled && [[[device->device status] objectForKey: DRDeviceCanUnderrunProtectCDKey] boolValue];
- }
-
- return false;
- }
-
- int getNumAvailableAudioBlocks() const
- {
- return [[[[device->device status] objectForKey: DRDeviceMediaInfoKey]
- objectForKey: DRDeviceMediaBlocksFreeKey] intValue];
- }
-
- ScopedPointer<OpenDiskDevice> device;
-
- private:
- DiskState lastState;
- AudioCDBurner& owner;
- };
-
- //==============================================================================
- AudioCDBurner::AudioCDBurner (const int deviceIndex)
- {
- pimpl = new Pimpl (*this, deviceIndex);
- }
-
- AudioCDBurner::~AudioCDBurner()
- {
- }
-
- AudioCDBurner* AudioCDBurner::openDevice (const int deviceIndex)
- {
- ScopedPointer<AudioCDBurner> b (new AudioCDBurner (deviceIndex));
-
- if (b->pimpl->device == nil)
- b = nullptr;
-
- return b.release();
- }
-
- StringArray AudioCDBurner::findAvailableDevices()
- {
- StringArray s;
-
- for (NSDictionary* dic in [DRDevice devices])
- if (NSString* name = [dic valueForKey: DRDeviceProductNameKey])
- s.add (nsStringToJuce (name));
-
- return s;
- }
-
- AudioCDBurner::DiskState AudioCDBurner::getDiskState() const
- {
- return pimpl->getDiskState();
- }
-
- bool AudioCDBurner::isDiskPresent() const
- {
- return getDiskState() == writableDiskPresent;
- }
-
- bool AudioCDBurner::openTray()
- {
- return pimpl->openTray();
- }
-
- AudioCDBurner::DiskState AudioCDBurner::waitUntilStateChange (int timeOutMilliseconds)
- {
- const int64 timeout = Time::currentTimeMillis() + timeOutMilliseconds;
- DiskState oldState = getDiskState();
- DiskState newState = oldState;
-
- while (newState == oldState && Time::currentTimeMillis() < timeout)
- {
- newState = getDiskState();
- Thread::sleep (100);
- }
-
- return newState;
- }
-
- Array<int> AudioCDBurner::getAvailableWriteSpeeds() const
- {
- return pimpl->getAvailableWriteSpeeds();
- }
-
- bool AudioCDBurner::setBufferUnderrunProtection (const bool shouldBeEnabled)
- {
- return pimpl->setBufferUnderrunProtection (shouldBeEnabled);
- }
-
- int AudioCDBurner::getNumAvailableAudioBlocks() const
- {
- return pimpl->getNumAvailableAudioBlocks();
- }
-
- bool AudioCDBurner::addAudioTrack (AudioSource* source, int numSamps)
- {
- if ([pimpl->device->device isValid])
- {
- pimpl->device->addSourceTrack (source, numSamps);
- return true;
- }
-
- return false;
- }
-
- String AudioCDBurner::burn (AudioCDBurner::BurnProgressListener* listener,
- bool ejectDiscAfterwards,
- bool performFakeBurnForTesting,
- int writeSpeed)
- {
- if ([pimpl->device->device isValid])
- return pimpl->device->burn (listener, ejectDiscAfterwards, performFakeBurnForTesting, writeSpeed);
-
- return "Couldn't open or write to the CD device";
- }
|