workspace instanceof WorkspaceEntity ? $cmd->workspace : $this->wsRepo->ofId($cmd->workspace); $user = $cmd->user instanceof UserEntity ? $cmd->user : $this->userRepo->ofId($cmd->user); if ( !is_null($ws->getTotalCreditCount()->value) && (float) $ws->getTotalCreditCount()->value <= 0 ) { throw new InsufficientCreditsException(); } $ext = pathinfo($cmd->file->getClientFilename(), PATHINFO_EXTENSION); // Save input file to CDN $stream = $cmd->file->getStream(); $stream->rewind(); $name = Uuid::uuid4()->toString() . '.' . $ext; $this->cdn->write("/" . $name, $stream->getContents()); $inputFile = new FileEntity( new Storage($this->cdn->getAdapterLookupKey()), new ObjectKey($name), new Url($this->cdn->getUrl($name)), new Size($cmd->file->getSize()) ); // Create service $service = $this->factory->create( VoiceIsolatorServiceInterface::class, $cmd->model ); // Generate isolated voice // $stream->rewind(); $resp = $service->generateIsolatedVoice( $cmd->model, $this->streamFactory->createStreamFromResource( $this->cdn->readStream($name) ), $cmd->params ); // $resp->audioContent->rewind(); $content = ''; while (!$resp->audioContent->eof()) { $content .= $resp->audioContent->read(1); } // Save output file to CDN $name = Uuid::uuid4()->toString() . '.mp3'; $this->cdn->write($name, $content); $outputFile = new FileEntity( new Storage($this->cdn->getAdapterLookupKey()), new ObjectKey($name), new Url($this->cdn->getUrl($name)), new Size(strlen($content)) ); /** @var Count */ $cost = $resp->cost; $entity = new IsolatedVoiceEntity( $ws, $user, $cmd->model, $inputFile, $outputFile, new Title($cmd->file->getClientFilename()), RequestParams::fromArray($cmd->params), $cost ); $this->repo->add($entity); // Deduct credit from workspace $ws->deductCredit($cost); // Dispatch event $event = new CreditUsageEvent($ws, $cost); $this->dispatcher->dispatch($event); return $entity; } }