Skip to content

Commit

Permalink
update to pixi v8
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Jan 3, 2024
1 parent 78fff13 commit cd064ae
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 647 deletions.
732 changes: 127 additions & 605 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@
"@pixi/extension-scripts": "^1.8.1",
"copyfiles": "^2.4.1",
"mkdirp": "^1.0.4",
"pixi.js": "^7.0.0"
"pixi.js": "^8.0.0-rc"
},
"peerDependencies": {
"@pixi/assets": "^7.0.0",
"@pixi/core": "^7.0.0"
"pixi.js": "^8.0.0"
}
}
4 changes: 2 additions & 2 deletions src/Sound.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utils } from '@pixi/core';
import { path } from 'pixi.js';
import { Filter } from './filters/Filter';
import { HTMLAudioMedia } from './htmlaudio/HTMLAudioMedia';
import { getInstance } from './instance';
Expand Down Expand Up @@ -323,7 +323,7 @@ class Sound
private preferUrl(urls: string[]): string
{
const [file] = urls
.map((url) => ({ url, ext: utils.path.extname(url).slice(1) }))
.map((url) => ({ url, ext: path.extname(url).slice(1) }))
.filter(({ ext }) => supported[ext])
.sort((a, b) => extensions.indexOf(a.ext) - extensions.indexOf(b.ext));

Expand Down
6 changes: 3 additions & 3 deletions src/htmlaudio/HTMLAudioContext.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { utils } from '@pixi/core';
import { EventEmitter } from 'pixi.js';
import { Filter } from '../filters/Filter';
import { IMediaContext } from '../interfaces/IMediaContext';

/**
* The fallback version of WebAudioContext which uses `<audio>` instead of WebAudio API.
* @memberof htmlaudio
* @extends PIXI.util.EventEmitter
* @extends PIXI.EventEmitter
*/
class HTMLAudioContext extends utils.EventEmitter implements IMediaContext
class HTMLAudioContext extends EventEmitter implements IMediaContext
{
/** Current global speed from 0 to 1 */
public speed = 1;
Expand Down
10 changes: 5 additions & 5 deletions src/htmlaudio/HTMLAudioInstance.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { utils, Ticker } from '@pixi/core';
import { IMediaInstance } from '../interfaces/IMediaInstance';
import { EventEmitter, Ticker } from 'pixi.js';
import { PlayOptions } from '../Sound';
import { HTMLAudioMedia } from './HTMLAudioMedia';
import { Filter } from '../filters/Filter';
import { IMediaInstance } from '../interfaces/IMediaInstance';
import { HTMLAudioMedia } from './HTMLAudioMedia';

let id = 0;

/**
* Instance which wraps the `<audio>` element playback.
* @memberof htmlaudio
* @extends PIXI.util.EventEmitter
* @extends PIXI.EventEmitter
*/
class HTMLAudioInstance extends utils.EventEmitter implements IMediaInstance
class HTMLAudioInstance extends EventEmitter implements IMediaInstance
{
/** Extra padding, in seconds, to deal with low-latecy of HTMLAudio. */
public static readonly PADDING: number = 0.1;
Expand Down
6 changes: 3 additions & 3 deletions src/htmlaudio/HTMLAudioMedia.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utils } from '@pixi/core';
import { EventEmitter } from 'pixi.js';
import { Filter } from '../filters/Filter';
import { IMedia } from '../interfaces/IMedia';
import { LoadedCallback, Sound } from '../Sound';
Expand All @@ -8,9 +8,9 @@ import { HTMLAudioInstance } from './HTMLAudioInstance';
/**
* The fallback version of Sound which uses `<audio>` instead of WebAudio API.
* @memberof htmlaudio
* @extends PIXI.util.EventEmitter
* @extends PIXI.EventEmitter
*/
class HTMLAudioMedia extends utils.EventEmitter implements IMedia
class HTMLAudioMedia extends EventEmitter implements IMedia
{
public parent: Sound;
private _source: HTMLAudioElement;
Expand Down
16 changes: 8 additions & 8 deletions src/soundAsset.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { utils, extensions, ExtensionType } from '@pixi/core';
import { AssetExtension, LoadAsset, LoaderParser, LoaderParserPriority } from '@pixi/assets';
import { supported, extensions as exts, mimes } from './utils/supported';
import { AssetExtension, ExtensionType, LoaderParser, LoaderParserPriority, ResolvedAsset, extensions, path } from 'pixi.js';
import { Options, Sound } from './Sound';
import { getInstance } from './instance';
import { extensions as exts, mimes, supported } from './utils/supported';

/** Get the alias for the sound */
const getAlias = (asset: LoadAsset) =>
const getAlias = (asset: ResolvedAsset) =>
{
const url = asset.src;

return (asset as any)?.alias?.[0] ?? utils.path.basename(url, utils.path.extname(url));
return (asset as any)?.alias?.[0] ?? path.basename(url, path.extname(url));
};

/**
Expand All @@ -23,6 +22,7 @@ const soundAsset = {
remove: async (formats) => formats.filter((ext) => formats.includes(ext)),
},
loader: {
name: 'sound',
extension: {
type: [ExtensionType.LoadParser],
priority: LoaderParserPriority.High,
Expand All @@ -31,13 +31,13 @@ const soundAsset = {
/** Should we attempt to load this file? */
test(url: string): boolean
{
const ext = utils.path.extname(url).slice(1);
const ext = path.extname(url).slice(1);

return !!supported[ext] || mimes.some((mime) => url.startsWith(`data:${mime}`));
},

/** Load the sound file, this is mostly handled by Sound.from() */
async load(url: string, asset: LoadAsset<Omit<Options, 'url' | 'preload'>>): Promise<Sound>
async load(url: string, asset: ResolvedAsset<Omit<Options, 'url' | 'preload'>>): Promise<Sound>
{
// We'll use the internal Sound.from to load the asset
const sound = await new Promise<Sound>((resolve, reject) => Sound.from({
Expand All @@ -64,7 +64,7 @@ const soundAsset = {
},

/** Remove the sound from the library */
async unload(_sound: Sound, asset: LoadAsset): Promise<void>
async unload(_sound: Sound, asset: ResolvedAsset): Promise<void>
{
getInstance().remove(getAlias(asset));
},
Expand Down
15 changes: 9 additions & 6 deletions src/utils/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseTexture } from '@pixi/core';
import { CanvasSource, ICanvas, TextureSource } from 'pixi.js';
import { Sound } from '../Sound';
import { WebAudioMedia } from '../webaudio/WebAudioMedia';

Expand Down Expand Up @@ -28,7 +28,7 @@ interface RenderOptions
* @param options - Custom rendering options
* @return Result texture
*/
function render(sound: Sound, options?: RenderOptions): BaseTexture
function render(sound: Sound, options?: RenderOptions): TextureSource
{
const canvas: HTMLCanvasElement = document.createElement('canvas');

Expand All @@ -40,11 +40,13 @@ function render(sound: Sound, options?: RenderOptions): BaseTexture
canvas.width = options.width;
canvas.height = options.height;

const baseTexture = BaseTexture.from(canvas);
const textureSource = new CanvasSource({
resource: canvas as ICanvas,
});

if (!(sound.media instanceof WebAudioMedia))
{
return baseTexture;
return textureSource;
}

const media: WebAudioMedia = sound.media as WebAudioMedia;
Expand Down Expand Up @@ -80,8 +82,9 @@ function render(sound: Sound, options?: RenderOptions): BaseTexture
context.fillRect(i, (1 + min) * amp, 1, Math.max(1, (max - min) * amp));
}

return baseTexture;
return textureSource;
}

export type { RenderOptions };
export { render };
export type { RenderOptions };

8 changes: 4 additions & 4 deletions src/webaudio/WebAudioContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utils } from '@pixi/core';
import { EventEmitter } from 'pixi.js';
import { Filterable } from '../Filterable';
import { IMediaContext } from '../interfaces';

Expand Down Expand Up @@ -42,9 +42,9 @@ class WebAudioContext extends Filterable implements IMediaContext

/**
* Handle global events
* @type {PIXI.utils.EventEmitter}
* @type {PIXI.EventEmitter}
*/
public events: utils.EventEmitter;
public events: EventEmitter;

/** The instance of the AudioContext for WebAudio API. */
private _ctx: AudioContext;
Expand Down Expand Up @@ -92,7 +92,7 @@ class WebAudioContext extends Filterable implements IMediaContext

this.compressor = compressor;
this.analyser = analyser;
this.events = new utils.EventEmitter();
this.events = new EventEmitter();

// Set the defaults
this.volume = 1;
Expand Down
10 changes: 5 additions & 5 deletions src/webaudio/WebAudioInstance.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { utils, Ticker } from '@pixi/core';
import { IMediaInstance } from '../interfaces';
import { EventEmitter, Ticker } from 'pixi.js';
import { PlayOptions } from '../Sound';
import { Filter } from '../filters/Filter';
import { IMediaInstance } from '../interfaces';
import { WebAudioMedia } from './WebAudioMedia';
import { WebAudioUtils } from './WebAudioUtils';
import { Filter } from '../filters/Filter';

let id = 0;

/**
* A single play instance that handles the AudioBufferSourceNode.
* @memberof webaudio
* @extends PIXI.utils.EventEmitter
* @extends PIXI.EventEmitter
*/
class WebAudioInstance extends utils.EventEmitter implements IMediaInstance
class WebAudioInstance extends EventEmitter implements IMediaInstance
{
/**
* The current unique ID for this instance.
Expand Down
4 changes: 2 additions & 2 deletions src/webaudio/WebAudioMedia.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { settings } from '@pixi/core';
import { DOMAdapter } from 'pixi.js';
import { Filter } from '../filters/Filter';
import { IMedia } from '../interfaces/IMedia';
import { LoadedCallback, Sound } from '../Sound';
Expand Down Expand Up @@ -141,7 +141,7 @@ class WebAudioMedia implements IMedia
private async _loadUrl(callback?: LoadedCallback): Promise<void>
{
const url: string = this.parent.url;
const response = await settings.ADAPTER.fetch(url);
const response = await DOMAdapter.get().fetch(url);

this._decode(await response.arrayBuffer(), callback);
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export function suite(useLegacy = false): void
{
for (const name in manifest)
{
Assets.add(name, manifest[name]);
Assets.add({ alias: name, src: manifest[name] });
const s = await Assets.load<Sound>(name);
const ClassRef = useLegacy
? htmlaudio.HTMLAudioMedia
Expand Down

0 comments on commit cd064ae

Please sign in to comment.