Skip to content

Commit

Permalink
added audio support for dialog cards
Browse files Browse the repository at this point in the history
  • Loading branch information
sr258 committed Jul 3, 2021
1 parent 70a0650 commit fe48bfc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

This is a command line utility that allows you to mass create H5P content from input files using the command line. It is written in TypeScript and runs on NodeJS, meaning it's platform independent. Currently, it supports the *Flashcards* and *Dialog Cards* content type, but you can use the infrastructure provided here to add functionality for other content types. Pull requests are welcomed!

**Warning: This project is work-in-progress. Expect changes.**

## Run
* Install [NodeJS](https://nodejs.org/)
* [clone this repository](https://help.github.com/articles/cloning-a-repository/) into a directory on your computer
Expand Down
31 changes: 28 additions & 3 deletions src/dialogcards-creator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContentCreator } from "./content-creator";
import { H5pPackage } from "./h5p-package";
import { H5pAudio } from "./models/h5p-audio";
import { H5PDialogCardsContent } from "./models/h5p-dialog-cards-content";
import { H5pImage } from "./models/h5p-image";

Expand All @@ -10,6 +11,7 @@ export class DialogCardsCreator extends ContentCreator<H5PDialogCardsContent> {
front: string;
back: string;
image?: string;
audio?: string;
}>,
private mode: "repetition" | "normal"
) {
Expand All @@ -35,11 +37,12 @@ export class DialogCardsCreator extends ContentCreator<H5PDialogCardsContent> {
contentObject.dialogs = new Array();

let imageCounter = 0;
let audioCounter = 0;

for (const line of this.data) {
const card = {
text: line.front,
answer: line.back
answer: line.back,
};
if (line.image) {
try {
Expand All @@ -59,6 +62,24 @@ export class DialogCardsCreator extends ContentCreator<H5PDialogCardsContent> {
card["image"] = undefined;
}
}
if (line.audio) {
try {
let ret = await H5pAudio.fromDownload(line.audio);
let filename = this.getFilenameForAudio(
audioCounter++,
ret.extension
);
this.h5pPackage.addContentFile(filename, ret.buffer);
ret.audio.path = filename;
card["audio"] = [ret.audio];
console.log(
`Downloaded audio from ${line.audio}. (${ret.buffer.byteLength} bytes)`
);
} catch (exc) {
console.error(exc);
card["audio"] = undefined;
}
}
contentObject.dialogs.push(card);
}
contentObject.mode = this.mode;
Expand All @@ -68,11 +89,15 @@ export class DialogCardsCreator extends ContentCreator<H5PDialogCardsContent> {
contentObject.behaviour = {
disableBackwardsNavigation: false,
randomCards: true,
scaleTextNotCard: false
scaleTextNotCard: false,
};
}

private getFilenameForImage(counter: number, extension: string) {
return `images/${counter}.${extension}`;
return `images/${counter}${extension}`;
}

private getFilenameForAudio(counter: number, extension: string) {
return `audios/${counter}${extension}`;
}
}
37 changes: 37 additions & 0 deletions src/models/h5p-audio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import axios from "axios";

import { extname } from "path";
import { toBuffer } from "../helpers";
import { H5pContent } from "./h5p-content";
import { H5pCopyrightInformation } from "./h5p-copyright-information";

export class H5pAudio extends H5pContent {
/**
* Downloads the audio at the URL and fills an H5pAudio objects with some metadata data of the image.
* @param url The url to download from.
* @returns the H5PAudio object, the buffer containing the raw audio data and the file extension of the URL
*/
public static async fromDownload(
url: string
): Promise<{ audio: H5pAudio; buffer: Buffer; extension: string }> {
let response = await axios.get(url, { responseType: "arraybuffer" });
if (response.status !== 200) {
throw new Error(`Error: Could not download audio at ${url}!`);
}
let a = new H5pAudio();
a.mime = response.headers["content-type"].replace(
"audio/mp3",
"audio/mpeg"
);
a.copyright.license = "U";
return {
audio: a,
buffer: toBuffer(response.data),
extension: extname(url),
};
}

public path: string;
public mime: string;
public copyright: H5pCopyrightInformation = new H5pCopyrightInformation();
}
2 changes: 2 additions & 0 deletions src/models/h5p-dialog-cards-content.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { H5pAudio } from "./h5p-audio";
import { H5pContent } from "./h5p-content";
import { H5pImage } from "./h5p-image";

Expand All @@ -6,6 +7,7 @@ export class H5PDialogCardsContent extends H5pContent {
public mode: "normal" | "repetition";
public description: string;
public dialogs: {
audio?: H5pAudio;
text: string;
answer: string;
image?: H5pImage;
Expand Down
4 changes: 2 additions & 2 deletions tests/dialog1.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
front;back;image
front;back;image;audio
question 1;answer 1;https://homepages.cae.wisc.edu/~ece533/images/cat.png
question 2;answer 2;
question 2;answer 2;;https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3
question 3;answer 3;
question 4;answer 4;
question 5;answer 5;
Expand Down

0 comments on commit fe48bfc

Please sign in to comment.