-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9281b0f
commit 1cf0115
Showing
8 changed files
with
190 additions
and
153 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/main/java/com/mpalourdio/projects/flhacker/FlhackerApplication.java
This file was deleted.
Oops, something went wrong.
75 changes: 0 additions & 75 deletions
75
src/main/java/com/mpalourdio/projects/flhacker/utils/AudiofileHandler.java
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
src/main/java/com/mpalourdio/projects/flhacker/utils/CliHandler.java
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/com/mpalourdio/projects/flhacker/FlhackerApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.mpalourdio.projects.flhacker | ||
|
||
import com.mpalourdio.projects.flhacker.utils.AudiofileHandler | ||
import com.mpalourdio.projects.flhacker.utils.CliHandler | ||
import org.apache.commons.cli.CommandLine | ||
import org.springframework.boot.CommandLineRunner | ||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
import org.springframework.context.annotation.Bean | ||
|
||
@SpringBootApplication | ||
class FlhackerApplication { | ||
|
||
@Bean | ||
fun run(): CommandLineRunner { | ||
return CommandLineRunner { args -> | ||
AudiofileHandler.setUp() | ||
val cmd: CommandLine? = CliHandler.run(args) | ||
try { | ||
AudiofileHandler.extractResizeSaveArtwork(cmd?.getOptionValue(CliHandler.FILE_CMD_LONG_OPTION)) | ||
AudiofileHandler.generateAsciiArt() | ||
} finally { | ||
AudiofileHandler.tearDown() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun main(args: Array<String>) { | ||
runApplication<FlhackerApplication>(*args) | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/kotlin/com/mpalourdio/projects/flhacker/utils/AudiofileHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.mpalourdio.projects.flhacker.utils | ||
|
||
import io.github.seujorgenochurras.image.ascii.AsciiParser | ||
import io.github.seujorgenochurras.image.ascii.ParserBuilder | ||
import io.github.seujorgenochurras.image.ascii.algorithm.pixel.bright.Algorithms | ||
import io.github.seujorgenochurras.image.ascii.algorithm.pixel.color.DefaultColorType | ||
import org.apache.commons.io.FileUtils | ||
import org.jaudiotagger.audio.AudioFileIO | ||
import org.jaudiotagger.audio.exceptions.CannotReadException | ||
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException | ||
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException | ||
import org.jaudiotagger.tag.TagException | ||
import java.awt.Image | ||
import java.awt.image.BufferedImage | ||
import java.io.File | ||
import java.io.IOException | ||
import javax.imageio.ImageIO | ||
|
||
object AudiofileHandler { | ||
private val TMP_DIR: String = System.getProperty("java.io.tmpdir") | ||
private val TMP_RESIZED_ARTWORK = "$TMP_DIR/resized.jpg" | ||
private const val TARGET_SIZE = 80 | ||
private val TMP_RESIZED_FILE = File(TMP_RESIZED_ARTWORK) | ||
|
||
@JvmStatic | ||
fun setUp() { | ||
FileUtils.deleteQuietly(TMP_RESIZED_FILE) | ||
} | ||
|
||
@JvmStatic | ||
fun tearDown() { | ||
setUp() | ||
} | ||
|
||
@JvmStatic | ||
@Throws( | ||
CannotReadException::class, | ||
TagException::class, | ||
InvalidAudioFrameException::class, | ||
ReadOnlyFileException::class, | ||
IOException::class | ||
) | ||
fun extractResizeSaveArtwork(audioFilePath: String?) { | ||
val audioFile = File(audioFilePath) | ||
val extractedArtwork = AudioFileIO.read(audioFile) | ||
.tag | ||
.firstArtwork | ||
.image | ||
|
||
val scaledArtwork = | ||
(extractedArtwork as BufferedImage).getScaledInstance(TARGET_SIZE, TARGET_SIZE, Image.SCALE_DEFAULT) | ||
val resized = BufferedImage(TARGET_SIZE, TARGET_SIZE, BufferedImage.TYPE_INT_RGB) | ||
resized.graphics.drawImage(scaledArtwork, 0, 0, null) | ||
|
||
ImageIO.write(resized, "png", TMP_RESIZED_FILE) | ||
} | ||
|
||
@JvmStatic | ||
fun generateAsciiArt() { | ||
val symbols = arrayOf(" ", ".", "-", "I", "W", "@") | ||
val parserConfig = ParserBuilder.startBuild() | ||
.parserAlgorithm(Algorithms.HUMAN_EYE_ALGORITHM) | ||
.scaled() | ||
.height(TARGET_SIZE) | ||
.width(TARGET_SIZE) | ||
.scale | ||
.symbols(*symbols) | ||
.colorAlgorithm(DefaultColorType.ANSI) | ||
.build() | ||
|
||
val asciiArt = AsciiParser.parse(TMP_RESIZED_ARTWORK, parserConfig) | ||
|
||
println(asciiArt) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/com/mpalourdio/projects/flhacker/utils/CliHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package com.mpalourdio.projects.flhacker.utils | ||
|
||
import org.apache.commons.cli.* | ||
import kotlin.system.exitProcess | ||
|
||
object CliHandler { | ||
const val FILE_CMD_LONG_OPTION: String = "file" | ||
|
||
fun run(args: Array<String?>?): CommandLine? { | ||
val options = Options() | ||
val input = | ||
Option("f", FILE_CMD_LONG_OPTION, true, "audio file path which contains the artwork to print") | ||
|
||
input.isRequired = true | ||
options.addOption(input) | ||
|
||
val parser: CommandLineParser = DefaultParser() | ||
val formatter = HelpFormatter() | ||
val cmd: CommandLine? | ||
|
||
try { | ||
cmd = parser.parse(options, args) | ||
} catch (e: ParseException) { | ||
println(e.message) | ||
formatter.printHelp("utility-name", options) | ||
exitProcess(1) | ||
} | ||
|
||
return cmd | ||
} | ||
} |